01: /*
02: * Copyright 2006-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.core.web.format;
17:
18: import java.security.GeneralSecurityException;
19:
20: import org.kuali.rice.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()
37: .decrypt(target);
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()
49: .encrypt(target);
50: } catch (GeneralSecurityException e) {
51: throw new RuntimeException(
52: "Unable to encrypt secure field.");
53: }
54:
55: return encryptedValue;
56: }
57: }
|