View Javadoc
1   /**
2    * Copyright 2005-2015 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.web.bind;
17  
18  import org.kuali.rice.core.api.CoreApiServiceLocator;
19  
20  import java.beans.PropertyEditor;
21  import java.beans.PropertyEditorSupport;
22  import java.security.GeneralSecurityException;
23  
24  /**
25   * Property editor which encrypts values for display and decrypts on binding, uses the
26   * {@link org.kuali.rice.core.api.encryption.EncryptionService} to perform the encryption
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class UifEncryptionPropertyEditorWrapper extends PropertyEditorSupport{
31  
32      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifEncryptionPropertyEditorWrapper.class);
33  
34      PropertyEditor propertyEditor;
35  
36      /**
37       * @param propertyEditor
38       */
39      public UifEncryptionPropertyEditorWrapper(PropertyEditor propertyEditor) {
40          super();
41          this.propertyEditor = propertyEditor;
42      }
43  
44      @Override
45      public String getAsText() {
46          try {
47              if (propertyEditor != null) {
48                  if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
49                      return CoreApiServiceLocator.getEncryptionService().encrypt(propertyEditor.getAsText());
50                  }
51              }
52              if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
53                  return CoreApiServiceLocator.getEncryptionService().encrypt(getValue());
54              }
55              return null;
56          } catch (GeneralSecurityException e) {
57              LOG.error("Unable to encrypt value");
58              throw new RuntimeException("Unable to encrypt value.");
59          }
60      }
61  
62      @Override
63      public void setAsText(String text) throws IllegalArgumentException {
64          try {
65              String value = "";
66              if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
67                  value = CoreApiServiceLocator.getEncryptionService().decrypt(text);
68              }
69              if (propertyEditor != null) {
70                  propertyEditor.setAsText(value);
71              } else {
72                  setValue(value);
73              }
74          } catch (GeneralSecurityException e) {
75              LOG.error("Unable to decrypt value: " + text);
76              throw new RuntimeException("Unable to decrypt value.");
77          }
78      }
79  
80  }