001 /*
002 * Copyright 2006-2008 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.kns.web.format;
017
018 import java.security.GeneralSecurityException;
019
020 import org.kuali.rice.kns.service.KNSServiceLocator;
021
022 /**
023 * This formatter calls the encryption service to encrypt/decrypt values.
024 *
025 *
026 */
027 public class EncryptionFormatter extends Formatter {
028 private static final long serialVersionUID = -4109390572922205211L;
029
030 protected Object convertToObject(String target) {
031 if (Formatter.isEmptyValue(target))
032 return null;
033
034 String decryptedValue = null;
035 try {
036 decryptedValue = KNSServiceLocator.getEncryptionService().decrypt(target);
037 }
038 catch (GeneralSecurityException e) {
039 throw new RuntimeException("Unable to decrypt value.");
040 }
041
042 return decryptedValue;
043 }
044
045 public Object format(Object target) {
046 String encryptedValue = null;
047 try {
048 encryptedValue = KNSServiceLocator.getEncryptionService().encrypt(target);
049 }
050 catch (GeneralSecurityException e) {
051 throw new RuntimeException("Unable to encrypt secure field.");
052 }
053
054 return encryptedValue;
055 }
056 }