View Javadoc
1   /**
2    * Copyright 2010-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.common.util.encrypt.provider;
17  
18  import static com.google.common.base.Optional.absent;
19  import static com.google.common.base.Optional.fromNullable;
20  import static java.lang.String.format;
21  import static org.apache.commons.lang3.StringUtils.trimToNull;
22  import static org.kuali.common.util.base.Precondition.checkNotBlank;
23  import static org.kuali.common.util.encrypt.EncryptionStrength.DEFAULT_ENCRYPTION_STRENGTH;
24  import static org.kuali.common.util.log.Loggers.newLogger;
25  
26  import org.kuali.common.util.encrypt.EncryptionContext;
27  import org.kuali.common.util.encrypt.EncryptionStrength;
28  import org.slf4j.Logger;
29  
30  import com.google.common.base.Optional;
31  
32  public abstract class AbstractEncryptionContextProvider implements EncryptionContextProvider {
33  
34  	private static final Logger logger = newLogger();
35  
36  	public AbstractEncryptionContextProvider(String passwordKey, String strengthKey) {
37  		this.passwordKey = checkNotBlank(passwordKey, "passwordKey");
38  		this.strengthKey = checkNotBlank(strengthKey, "strengthKey");
39  	}
40  
41  	private final String passwordKey;
42  	private final String strengthKey;
43  
44  	@Override
45  	public Optional<EncryptionContext> getEncryptionContext() {
46  		Optional<String> password = getOptionalString(passwordKey);
47  		if (!password.isPresent()) {
48  			logger.debug(format("[%s, key=%s] encryption password not found", this.getClass().getSimpleName(), passwordKey));
49  			return absent();
50  		} else {
51  			EncryptionStrength strength = getEncryptionStrength();
52  			logger.debug(format("[%s, key=%s %s] encryption password located", this.getClass().getSimpleName(), passwordKey, strength));
53  			return Optional.of(new EncryptionContext(password.get(), strength));
54  		}
55  	}
56  
57  	protected Optional<String> getOptionalString(String key) {
58  		return fromNullable(trimToNull(getValueFromSource(key)));
59  	}
60  
61  	protected abstract String getValueFromSource(String key);
62  
63  	protected EncryptionStrength getEncryptionStrength() {
64  		Optional<String> optional = getOptionalString(strengthKey);
65  		if (optional.isPresent()) {
66  			return EncryptionStrength.valueOf(optional.get().toUpperCase());
67  		} else {
68  			return DEFAULT_ENCRYPTION_STRENGTH;
69  		}
70  	}
71  
72  }