001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.web.bind;
017    
018    import org.kuali.rice.core.api.CoreApiServiceLocator;
019    
020    import java.beans.PropertyEditor;
021    import java.beans.PropertyEditorSupport;
022    import java.security.GeneralSecurityException;
023    
024    /**
025     * Property editor which encrypts values for display and decrypts on binding, uses the
026     * {@link org.kuali.rice.core.api.encryption.EncryptionService} to perform the encryption
027     *
028     * @author Kuali Rice Team (rice.collab@kuali.org)
029     */
030    public class UifEncryptionPropertyEditorWrapper extends PropertyEditorSupport{
031    
032        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifEncryptionPropertyEditorWrapper.class);
033    
034        PropertyEditor propertyEditor;
035    
036        /**
037         * @param propertyEditor
038         */
039        public UifEncryptionPropertyEditorWrapper(PropertyEditor propertyEditor) {
040            super();
041            this.propertyEditor = propertyEditor;
042        }
043    
044        @Override
045        public String getAsText() {
046            try {
047                if (propertyEditor != null) {
048                    if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
049                        return CoreApiServiceLocator.getEncryptionService().encrypt(propertyEditor.getAsText());
050                    }
051                }
052                if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
053                    return CoreApiServiceLocator.getEncryptionService().encrypt(getValue());
054                }
055                return null;
056            } catch (GeneralSecurityException e) {
057                LOG.error("Unable to encrypt value");
058                throw new RuntimeException("Unable to encrypt value.");
059            }
060        }
061    
062        @Override
063        public void setAsText(String text) throws IllegalArgumentException {
064            try {
065                String value = "";
066                if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
067                    value = CoreApiServiceLocator.getEncryptionService().decrypt(text);
068                }
069                if (propertyEditor != null) {
070                    propertyEditor.setAsText(value);
071                } else {
072                    setValue(value);
073                }
074            } catch (GeneralSecurityException e) {
075                LOG.error("Unable to decrypt value: " + text);
076                throw new RuntimeException("Unable to decrypt value.");
077            }
078        }
079    
080    }