View Javadoc

1   /**
2    * Copyright 2005-2012 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                  return CoreApiServiceLocator.getEncryptionService().encrypt(propertyEditor.getAsText());
49              }
50              return CoreApiServiceLocator.getEncryptionService().encrypt(getValue());
51          } catch (GeneralSecurityException e) {
52              LOG.error("Unable to encrypt value");
53              throw new RuntimeException("Unable to encrypt value.");
54          }
55      }
56  
57      @Override
58      public void setAsText(String text) throws IllegalArgumentException {
59          try {
60              String value = CoreApiServiceLocator.getEncryptionService().decrypt(text);
61              if (propertyEditor != null) {
62                  propertyEditor.setAsText(value);
63              } else {
64                  setValue(value);
65              }
66          } catch (GeneralSecurityException e) {
67              LOG.error("Unable to decrypt value: " + text);
68              throw new RuntimeException("Unable to decrypt value.");
69          }
70      }
71  
72  }