1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.devtools.pdle; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.commons.logging.Log; |
20 | |
import org.apache.commons.logging.LogFactory; |
21 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
22 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
23 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
24 | |
import org.kuali.rice.kns.service.KNSServiceLocatorInternal; |
25 | |
import org.springframework.core.io.FileSystemResource; |
26 | |
|
27 | |
import javax.servlet.ServletException; |
28 | |
import javax.servlet.ServletRequest; |
29 | |
import javax.servlet.ServletResponse; |
30 | |
import javax.servlet.http.HttpServlet; |
31 | |
import java.io.IOException; |
32 | |
import java.util.Arrays; |
33 | |
import java.util.Collection; |
34 | |
import java.util.HashSet; |
35 | |
import java.util.Properties; |
36 | |
import java.util.Set; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | public class PostDataLoadEncryptionServlet extends HttpServlet { |
77 | |
|
78 | 0 | private static final Log LOG = LogFactory.getLog(PostDataLoadEncryptionServlet.class); |
79 | |
|
80 | |
private static final String ATTRIBUTES_TO_ENCRYPT_PROPERTIES = "attributesToEncryptProperties"; |
81 | |
private static final String CHECK_OJB_ENCRYPT_CONFIG = "checkOjbEncryptConfig"; |
82 | |
|
83 | |
@Override |
84 | |
public void service(ServletRequest request, ServletResponse response) |
85 | |
throws ServletException, IOException { |
86 | 0 | String attributesToEncryptPropertyFileName = request.getParameter(ATTRIBUTES_TO_ENCRYPT_PROPERTIES); |
87 | 0 | if (StringUtils.isBlank(attributesToEncryptPropertyFileName)) { |
88 | 0 | throw new IllegalArgumentException("No valid " + ATTRIBUTES_TO_ENCRYPT_PROPERTIES + " parameter was passed to this Servlet."); |
89 | |
} |
90 | 0 | boolean checkOjbEncryptConfig = true; |
91 | 0 | String checkOjbEncryptConfigValue = request.getParameter(CHECK_OJB_ENCRYPT_CONFIG); |
92 | 0 | if (!StringUtils.isBlank(checkOjbEncryptConfigValue)) { |
93 | 0 | checkOjbEncryptConfig = Boolean.valueOf(checkOjbEncryptConfigValue); |
94 | |
} |
95 | 0 | execute(attributesToEncryptPropertyFileName, checkOjbEncryptConfig); |
96 | 0 | response.getOutputStream().write(new String("<html><body><p>Successfully encrypted attributes as defined in: " + attributesToEncryptPropertyFileName + "</p></body></html>").getBytes()); |
97 | 0 | } |
98 | |
|
99 | |
public void execute(String attributesToEncryptPropertyFileName, boolean checkOjbEncryptConfig) { |
100 | 0 | PostDataLoadEncryptionService postDataLoadEncryptionService = KNSServiceLocatorInternal.getService(PostDataLoadEncryptionService.POST_DATA_LOAD_ENCRYPTION_SERVICE); |
101 | 0 | Properties attributesToEncryptProperties = new Properties(); |
102 | |
try { |
103 | 0 | attributesToEncryptProperties.load(new FileSystemResource(attributesToEncryptPropertyFileName).getInputStream()); |
104 | |
} |
105 | 0 | catch (Exception e) { |
106 | 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); |
107 | 0 | } |
108 | 0 | for (Object businessObjectClassName : attributesToEncryptProperties.keySet()) { |
109 | |
Class businessObjectClass; |
110 | |
try { |
111 | 0 | businessObjectClass = Class.forName((String) businessObjectClassName); |
112 | |
} |
113 | 0 | catch (Exception e) { |
114 | 0 | throw new IllegalArgumentException(new StringBuffer("Unable to load Class ").append(businessObjectClassName).append(" specified by name in attributesToEncryptProperties file ").append(attributesToEncryptProperties).toString(), e); |
115 | 0 | } |
116 | 0 | Set<String> attributeNames = null; |
117 | |
try { |
118 | 0 | attributeNames = new HashSet(Arrays.asList(StringUtils.split((String) attributesToEncryptProperties.get(businessObjectClassName), ","))); |
119 | |
} |
120 | 0 | catch (Exception e) { |
121 | 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); |
122 | 0 | } |
123 | 0 | postDataLoadEncryptionService.checkArguments(businessObjectClass, attributeNames, checkOjbEncryptConfig); |
124 | 0 | postDataLoadEncryptionService.createBackupTable(businessObjectClass); |
125 | 0 | BusinessObjectService businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
126 | |
try { |
127 | 0 | postDataLoadEncryptionService.prepClassDescriptor(businessObjectClass, attributeNames); |
128 | 0 | Collection objectsToEncrypt = businessObjectService.findAll(businessObjectClass); |
129 | 0 | for (Object businessObject : objectsToEncrypt) { |
130 | 0 | postDataLoadEncryptionService.encrypt((PersistableBusinessObject) businessObject, attributeNames); |
131 | |
} |
132 | 0 | postDataLoadEncryptionService.restoreClassDescriptor(businessObjectClass, attributeNames); |
133 | 0 | LOG.info(new StringBuffer("Encrypted ").append(attributesToEncryptProperties.get(businessObjectClassName)).append(" attributes of Class ").append(businessObjectClassName)); |
134 | |
} |
135 | 0 | catch (Exception e) { |
136 | 0 | postDataLoadEncryptionService.restoreTableFromBackup(businessObjectClass); |
137 | 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); |
138 | 0 | } |
139 | 0 | postDataLoadEncryptionService.dropBackupTable(businessObjectClass); |
140 | 0 | } |
141 | 0 | } |
142 | |
|
143 | |
} |