View Javadoc

1   /**
2    * Copyright 2005-2013 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.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   * Controller for the encrypt/decrypt utility
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Polulate the encryptionServiceName on the form.
48       */
49      @Override
50      protected EncryptionForm createInitialForm(HttpServletRequest request) {
51          EncryptionForm encryptionForm = new EncryptionForm();
52          encryptionForm.setEncryptionServiceName(getEncryptionService().getClass().getSimpleName());
53          return encryptionForm;
54      }
55  
56      /**
57       * Encrypt the text of the input field.
58       *
59       * <p>
60       * The encryptedText and decryptedText filds are populated when encryption is successful.  An error message
61       * is displayed otherwise.
62       * </p>
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 getUIFModelAndView(form);
79      }
80  
81      /**
82       * Decrypt the text of the input field.
83       *
84       * <p>
85       * The encryptedText and decryptedText filds are populated when decryption is successful.  An error message
86       * is displayed otherwise.
87       * </p>
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 getUIFModelAndView(form);
104     }
105 
106     /**
107      * Get the encryption service
108      *
109      * @return EncryptionService
110      */
111     private EncryptionService getEncryptionService() {
112         if (encryptionService == null) {
113             encryptionService = CoreApiServiceLocator.getEncryptionService();
114         }
115 
116         return encryptionService;
117     }
118 }