Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OjbKualiHashFieldConversion |
|
| 4.0;4 |
1 | /* | |
2 | * Copyright 2007 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.core.framework.persistence.ojb.conversion; | |
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.rice.core.api.CoreApiServiceLocator; | |
23 | import org.kuali.rice.core.api.encryption.EncryptionService; | |
24 | ||
25 | ||
26 | /** | |
27 | * This class calls core service to hash values going to the database. | |
28 | * | |
29 | * | |
30 | */ | |
31 | ||
32 | 0 | public class OjbKualiHashFieldConversion implements FieldConversion { |
33 | ||
34 | /** | |
35 | * @see FieldConversion#javaToSql(Object) | |
36 | */ | |
37 | public Object javaToSql(Object source) { | |
38 | 0 | Object converted = source; |
39 | 0 | if ( converted != null ) { |
40 | // don't convert if already a hashed value | |
41 | 0 | if ( converted.toString().endsWith( EncryptionService.HASH_POST_PREFIX ) ) { |
42 | 0 | converted = StringUtils.stripEnd( converted.toString(), EncryptionService.HASH_POST_PREFIX ); |
43 | } else { | |
44 | try { | |
45 | 0 | converted = CoreApiServiceLocator.getEncryptionService().hash(converted); |
46 | 0 | } catch (GeneralSecurityException e) { |
47 | 0 | throw new RuntimeException("Unable to hash value to db: " + e.getMessage()); |
48 | 0 | } |
49 | } | |
50 | } | |
51 | ||
52 | 0 | return converted; |
53 | } | |
54 | ||
55 | /** | |
56 | * @see FieldConversion#sqlToJava(Object) | |
57 | */ | |
58 | public Object sqlToJava(Object source) { | |
59 | 0 | if ( source == null ) { |
60 | 0 | return ""; |
61 | } | |
62 | 0 | return source.toString() + EncryptionService.HASH_POST_PREFIX; |
63 | } | |
64 | } |