1 /**
2 * Copyright 2005-2014 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.kew.routeheader;
17
18 import org.kuali.rice.krad.data.jpa.converters.EncryptionConverter;
19
20 import javax.persistence.AttributeConverter;
21 import javax.persistence.Converter;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 /**
26 * A JPA Converter which supports mixed encrypted and non-encrypted document content XML.
27 *
28 * <p>Leverages behavior from the standard {@link EncryptionConverter} but additionally when converting to the entity
29 * attribute value, it will detect whether or not the value is encrypted XML or plain text XML and decrypt if necessary.
30 * This allows for this situation where encryption is enabled at a later date. Using this mechanism, existing plain text
31 * docs can be loaded (but could potentially be saved back to the database encrypted if encryption is enabled).</p>
32 *
33 * <p>Note that the mixed mode only works one way. If you have been using encryption and then disable it, this
34 * converter will not be able to decrypt your old doc content for you since it will no longer have the encryption key
35 * available to it.</p>
36 *
37 * @author Kuali Rice Team (rice.collab@kuali.org)
38 */
39 @Converter
40 public class DocumentContentEncryptionConverter implements AttributeConverter<String, String> {
41
42 /**
43 * It's XML if it starts with a '<' though there can be some whitespace in front of it as well. Encrypted and
44 * Base64 encoded content will *never* start with a '<' so this should be a safe check.
45 */
46 private static final Pattern IS_XML = Pattern.compile("^\\s*<");
47
48 private static final EncryptionConverter encryptionConverter = new EncryptionConverter();
49
50 @Override
51 public String convertToEntityAttribute(String dataValue) {
52 // can't pass 'null' to Matcher, so let's check that first
53 if (dataValue == null) {
54 return null;
55 }
56 Matcher matcher = IS_XML.matcher(dataValue);
57 if (matcher.lookingAt()) {
58 return dataValue;
59 }
60 return encryptionConverter.convertToEntityAttribute(dataValue);
61 }
62
63 @Override
64 public String convertToDatabaseColumn(String attribute) {
65 return encryptionConverter.convertToDatabaseColumn(attribute);
66 }
67 }