01: /*
02: * Copyright 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.util;
17:
18: import java.security.GeneralSecurityException;
19:
20: import org.apache.commons.lang.StringUtils;
21: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
22: import org.kuali.core.service.EncryptionService;
23: import org.kuali.rice.KNSServiceLocator;
24:
25: /**
26: * This class calls core service to hash values going to the database.
27: *
28: *
29: */
30:
31: public class OjbKualiHashFieldConversion implements FieldConversion {
32:
33: /**
34: * @see FieldConversion#javaToSql(Object)
35: */
36: public Object javaToSql(Object source) {
37: Object converted = source;
38: if (converted != null) {
39: // don't convert if already a hashed value
40: if (converted.toString().endsWith(
41: EncryptionService.HASH_POST_PREFIX)) {
42: converted = StringUtils.stripEnd(converted.toString(),
43: EncryptionService.HASH_POST_PREFIX);
44: } else {
45: try {
46: converted = KNSServiceLocator
47: .getEncryptionService().hash(converted);
48: } catch (GeneralSecurityException e) {
49: throw new RuntimeException(
50: "Unable to hash value to db: "
51: + e.getMessage());
52: }
53: }
54: }
55:
56: return converted;
57: }
58:
59: /**
60: * @see FieldConversion#sqlToJava(Object)
61: */
62: public Object sqlToJava(Object source) {
63: if (source == null) {
64: return "";
65: }
66: return source.toString() + EncryptionService.HASH_POST_PREFIX;
67: }
68: }
|