View Javadoc

1   /*
2    * Copyright 2006-2008 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.kns.web.format;
17  
18  import java.security.GeneralSecurityException;
19  
20  import org.kuali.rice.kns.service.KNSServiceLocator;
21  
22  /**
23   * This formatter calls the encryption service to encrypt/decrypt values.
24   * 
25   * 
26   */
27  public class EncryptionFormatter extends Formatter {
28      private static final long serialVersionUID = -4109390572922205211L;
29  
30      protected Object convertToObject(String target) {
31          if (Formatter.isEmptyValue(target))
32              return null;
33  
34          String decryptedValue = null;
35          try {
36              decryptedValue = KNSServiceLocator.getEncryptionService().decrypt(target);
37          }
38          catch (GeneralSecurityException e) {
39              throw new RuntimeException("Unable to decrypt value.");
40          }
41  
42          return decryptedValue;
43      }
44  
45      public Object format(Object target) {
46          String encryptedValue = null;
47          try {
48              encryptedValue = KNSServiceLocator.getEncryptionService().encrypt(target);
49          }
50          catch (GeneralSecurityException e) {
51              throw new RuntimeException("Unable to encrypt secure field.");
52          }
53  
54          return encryptedValue;
55      }
56  }