View Javadoc

1   /**
2    * Copyright 2005-2011 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   * A property editor 
26   * 
27   */
28  public class UifEncryptionPropertyEditorWrapper extends PropertyEditorSupport {
29      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifKnsFormatterPropertyEditor.class);
30  
31  	PropertyEditor propertyEditor;
32  
33      /**
34       * @param formatter
35       */
36      public UifEncryptionPropertyEditorWrapper(PropertyEditor propertyEditor) {
37  	    super();
38  	    this.propertyEditor = propertyEditor;
39      }
40  
41  	@Override
42  	public String getAsText() {
43  		try {
44  			if (propertyEditor != null) {
45  				return CoreApiServiceLocator.getEncryptionService().encrypt(propertyEditor.getAsText());
46  			}
47  			return CoreApiServiceLocator.getEncryptionService().encrypt(getValue());
48  		} catch (GeneralSecurityException e) {
49  			LOG.error("Unable to encrypt value");
50              throw new RuntimeException("Unable to encrypt value.");
51  		}
52  	}
53  
54  	@Override
55  	public void setAsText(String text) throws IllegalArgumentException {
56  		try {
57  			String value = CoreApiServiceLocator.getEncryptionService().decrypt(text);
58  			if (propertyEditor != null) {
59  				propertyEditor.setAsText(value);
60  			} else {
61  				setValue(value);
62  			}
63  		} catch (GeneralSecurityException e) {
64  			LOG.error("Unable to decrypt value: " + text);
65              throw new RuntimeException("Unable to decrypt value.");
66  		}
67  	}
68  
69  }