Coverage Report - org.kuali.rice.kns.web.servlet.PostDataLoadEncryptionServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
PostDataLoadEncryptionServlet
0%
0/44
0%
0/8
7
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * This is a servlet that can be used to invoke the PostDataLoadEncryptionService.
 39  
  * 
 40  
  * It is not recommended to leave this Servlet running at all times.  It is really only intended
 41  
  * to be made available during initial data load and then removed (from the web.xml of the
 42  
  * application) after data load and encryption is complete.
 43  
  * 
 44  
  * This was done as a Servlet for now because Rice does not have a batch runner yet similar
 45  
  * to what KFS has (which is where a lot of the code below was borrowed from).
 46  
  * 
 47  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
 }