001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.encrypt.provider;
017
018import static com.google.common.base.Optional.absent;
019import static com.google.common.base.Optional.fromNullable;
020import static java.lang.String.format;
021import static org.apache.commons.lang3.StringUtils.trimToNull;
022import static org.kuali.common.util.base.Precondition.checkNotBlank;
023import static org.kuali.common.util.encrypt.EncryptionStrength.DEFAULT_ENCRYPTION_STRENGTH;
024import static org.kuali.common.util.log.Loggers.newLogger;
025
026import org.kuali.common.util.encrypt.EncryptionContext;
027import org.kuali.common.util.encrypt.EncryptionStrength;
028import org.slf4j.Logger;
029
030import com.google.common.base.Optional;
031
032public abstract class AbstractEncryptionContextProvider implements EncryptionContextProvider {
033
034        private static final Logger logger = newLogger();
035
036        public AbstractEncryptionContextProvider(String passwordKey, String strengthKey) {
037                this.passwordKey = checkNotBlank(passwordKey, "passwordKey");
038                this.strengthKey = checkNotBlank(strengthKey, "strengthKey");
039        }
040
041        private final String passwordKey;
042        private final String strengthKey;
043
044        @Override
045        public Optional<EncryptionContext> getEncryptionContext() {
046                Optional<String> password = getOptionalString(passwordKey);
047                if (!password.isPresent()) {
048                        logger.debug(format("[%s, key=%s] encryption password not found", this.getClass().getSimpleName(), passwordKey));
049                        return absent();
050                } else {
051                        EncryptionStrength strength = getEncryptionStrength();
052                        logger.debug(format("[%s, key=%s %s] encryption password located", this.getClass().getSimpleName(), passwordKey, strength));
053                        return Optional.of(new EncryptionContext(password.get(), strength));
054                }
055        }
056
057        protected Optional<String> getOptionalString(String key) {
058                return fromNullable(trimToNull(getValueFromSource(key)));
059        }
060
061        protected abstract String getValueFromSource(String key);
062
063        protected EncryptionStrength getEncryptionStrength() {
064                Optional<String> optional = getOptionalString(strengthKey);
065                if (optional.isPresent()) {
066                        return EncryptionStrength.valueOf(optional.get().toUpperCase());
067                } else {
068                        return DEFAULT_ENCRYPTION_STRENGTH;
069                }
070        }
071
072}