1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.labs.encryption;
17
18 import org.kuali.rice.core.api.CoreApiServiceLocator;
19 import org.kuali.rice.core.api.encryption.EncryptionService;
20 import org.kuali.rice.krad.util.GlobalVariables;
21 import org.kuali.rice.krad.web.controller.UifControllerBase;
22 import org.kuali.rice.krad.web.form.UifFormBase;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.validation.BindingResult;
25 import org.springframework.web.bind.annotation.ModelAttribute;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.servlet.ModelAndView;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.security.GeneralSecurityException;
32
33
34
35
36
37
38 @Controller
39 @RequestMapping(value = "/encryption")
40 public class EncryptionController extends UifControllerBase {
41
42 public static final String INPUT_FIELD = "input";
43 public static final String ENCRYPTION_ERROR = "labs.encryption.error";
44 private EncryptionService encryptionService;
45
46
47
48
49 @Override
50 protected EncryptionForm createInitialForm() {
51 EncryptionForm encryptionForm = new EncryptionForm();
52 encryptionForm.setEncryptionServiceName(getEncryptionService().getClass().getSimpleName());
53 return encryptionForm;
54 }
55
56
57
58
59
60
61
62
63
64 @RequestMapping(params = "methodToCall=encrypt")
65 public ModelAndView encrypt(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
66 HttpServletRequest request, HttpServletResponse response) {
67 EncryptionForm encryptionForm = (EncryptionForm) form;
68
69 try {
70 encryptionForm.setEncryptedText(getEncryptionService().encrypt(
71 encryptionForm.getInput()));
72 encryptionForm.setDecryptedText(encryptionForm.getInput());
73 }
74 catch (GeneralSecurityException gse) {
75 GlobalVariables.getMessageMap().putError(INPUT_FIELD, ENCRYPTION_ERROR, gse.toString());
76 }
77
78 return getModelAndView(form);
79 }
80
81
82
83
84
85
86
87
88
89 @RequestMapping(params = "methodToCall=decrypt")
90 public ModelAndView decrypt(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
91 HttpServletRequest request, HttpServletResponse response) {
92 EncryptionForm encryptionForm = (EncryptionForm) form;
93
94 try {
95 encryptionForm.setEncryptedText(encryptionForm.getInput());
96 encryptionForm.setDecryptedText(getEncryptionService().decrypt(
97 encryptionForm.getInput()));
98 }
99 catch (GeneralSecurityException gse) {
100 GlobalVariables.getMessageMap().putError(INPUT_FIELD, ENCRYPTION_ERROR, gse.toString());
101 }
102
103 return getModelAndView(form);
104 }
105
106
107
108
109
110
111 private EncryptionService getEncryptionService() {
112 if (encryptionService == null) {
113 encryptionService = CoreApiServiceLocator.getEncryptionService();
114 }
115
116 return encryptionService;
117 }
118 }