1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.web.servlet; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.Arrays; |
20 | |
import java.util.Collection; |
21 | |
import java.util.HashSet; |
22 | |
import java.util.Properties; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import javax.servlet.ServletException; |
26 | |
import javax.servlet.ServletRequest; |
27 | |
import javax.servlet.ServletResponse; |
28 | |
import javax.servlet.http.HttpServlet; |
29 | |
|
30 | |
import org.apache.commons.lang.StringUtils; |
31 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
32 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
33 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
34 | |
import org.kuali.rice.kns.service.PostDataLoadEncryptionService; |
35 | |
import org.springframework.core.io.FileSystemResource; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class PostDataLoadEncryptionServlet extends HttpServlet { |
51 | |
|
52 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PostDataLoadEncryptionServlet.class); |
53 | |
|
54 | |
private static final String ATTRIBUTES_TO_ENCRYPT_PROPERTIES = "attributesToEncryptProperties"; |
55 | |
private static final String CHECK_OJB_ENCRYPT_CONFIG = "checkOjbEncryptConfig"; |
56 | |
|
57 | |
@Override |
58 | |
public void service(ServletRequest request, ServletResponse response) |
59 | |
throws ServletException, IOException { |
60 | 0 | String attributesToEncryptPropertyFileName = request.getParameter(ATTRIBUTES_TO_ENCRYPT_PROPERTIES); |
61 | 0 | if (StringUtils.isBlank(attributesToEncryptPropertyFileName)) { |
62 | 0 | throw new IllegalArgumentException("No valid " + ATTRIBUTES_TO_ENCRYPT_PROPERTIES + " parameter was passed to this Servlet."); |
63 | |
} |
64 | 0 | boolean checkOjbEncryptConfig = true; |
65 | 0 | String checkOjbEncryptConfigValue = request.getParameter(CHECK_OJB_ENCRYPT_CONFIG); |
66 | 0 | if (!StringUtils.isBlank(checkOjbEncryptConfigValue)) { |
67 | 0 | checkOjbEncryptConfig = Boolean.valueOf(checkOjbEncryptConfigValue); |
68 | |
} |
69 | 0 | execute(attributesToEncryptPropertyFileName, checkOjbEncryptConfig); |
70 | 0 | response.getOutputStream().write(new String("<html><body><p>Successfully encrypted attributes as defined in: " + attributesToEncryptPropertyFileName + "</p></body></html>").getBytes()); |
71 | 0 | } |
72 | |
|
73 | |
public void execute(String attributesToEncryptPropertyFileName, boolean checkOjbEncryptConfig) { |
74 | 0 | PostDataLoadEncryptionService postDataLoadEncryptionService = KNSServiceLocator.getPostDataLoadEncryptionService(); |
75 | 0 | Properties attributesToEncryptProperties = new Properties(); |
76 | |
try { |
77 | 0 | attributesToEncryptProperties.load(new FileSystemResource(attributesToEncryptPropertyFileName).getInputStream()); |
78 | |
} |
79 | 0 | catch (Exception e) { |
80 | 0 | throw new IllegalArgumentException("PostDataLoadEncrypter requires the full, absolute path to a properties file where the keys are the names of the BusinessObject classes that should be processed and the values are the list of attributes on each that require encryption", e); |
81 | 0 | } |
82 | 0 | for (Object businessObjectClassName : attributesToEncryptProperties.keySet()) { |
83 | |
Class businessObjectClass; |
84 | |
try { |
85 | 0 | businessObjectClass = Class.forName((String) businessObjectClassName); |
86 | |
} |
87 | 0 | catch (Exception e) { |
88 | 0 | throw new IllegalArgumentException(new StringBuffer("Unable to load Class ").append(businessObjectClassName).append(" specified by name in attributesToEncryptProperties file ").append(attributesToEncryptProperties).toString(), e); |
89 | 0 | } |
90 | 0 | Set<String> attributeNames = null; |
91 | |
try { |
92 | 0 | attributeNames = new HashSet(Arrays.asList(StringUtils.split((String) attributesToEncryptProperties.get(businessObjectClassName), ","))); |
93 | |
} |
94 | 0 | catch (Exception e) { |
95 | 0 | throw new IllegalArgumentException(new StringBuffer("Unable to load attributeNames Set from comma-delimited list of attribute names specified as value for property with Class name ").append(businessObjectClassName).append(" key in attributesToEncryptProperties file ").append(attributesToEncryptProperties).toString(), e); |
96 | 0 | } |
97 | 0 | postDataLoadEncryptionService.checkArguments(businessObjectClass, attributeNames, checkOjbEncryptConfig); |
98 | 0 | postDataLoadEncryptionService.createBackupTable(businessObjectClass); |
99 | 0 | BusinessObjectService businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
100 | |
try { |
101 | 0 | postDataLoadEncryptionService.prepClassDescriptor(businessObjectClass, attributeNames); |
102 | 0 | Collection objectsToEncrypt = businessObjectService.findAll(businessObjectClass); |
103 | 0 | for (Object businessObject : objectsToEncrypt) { |
104 | 0 | postDataLoadEncryptionService.encrypt((PersistableBusinessObject) businessObject, attributeNames); |
105 | |
} |
106 | 0 | postDataLoadEncryptionService.restoreClassDescriptor(businessObjectClass, attributeNames); |
107 | 0 | LOG.info(new StringBuffer("Encrypted ").append(attributesToEncryptProperties.get(businessObjectClassName)).append(" attributes of Class ").append(businessObjectClassName)); |
108 | |
} |
109 | 0 | catch (Exception e) { |
110 | 0 | postDataLoadEncryptionService.restoreTableFromBackup(businessObjectClass); |
111 | 0 | LOG.error(new StringBuffer("Caught exception, while encrypting ").append(attributesToEncryptProperties.get(businessObjectClassName)).append(" attributes of Class ").append(businessObjectClassName).append(" and restored table from backup"), e); |
112 | 0 | } |
113 | 0 | postDataLoadEncryptionService.dropBackupTable(businessObjectClass); |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | |
} |