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.StandardSystemProperty.USER_HOME;
019import static org.apache.commons.io.FileUtils.readFileToString;
020import static org.apache.commons.lang3.StringUtils.substringBetween;
021import static org.kuali.common.util.base.Exceptions.illegalState;
022
023import java.io.File;
024import java.io.IOException;
025
026import org.kuali.common.util.file.CanonicalFile;
027
028public final class MavenEncryptionContextProvider extends AbstractEncryptionContextProvider {
029
030    private static final File USER_SETTINGS = getDefaultSettingsXmlFile();
031
032    public MavenEncryptionContextProvider(String passwordKey, String strengthKey) {
033        super(passwordKey, strengthKey);
034    }
035
036    @Override
037    protected String getValueFromSource(String key) {
038        if (!USER_SETTINGS.exists()) {
039            return null;
040        }
041        String content = getContent(USER_SETTINGS);
042        String open = "<" + key + ">";
043        String close = "</" + key + ">";
044        return substringBetween(content, open, close);
045    }
046
047    private static String getContent(File file) {
048        try {
049            return readFileToString(file);
050        } catch (IOException e) {
051            throw illegalState(e);
052        }
053    }
054
055    private static File getDefaultSettingsXmlFile() {
056        return new CanonicalFile(USER_HOME.value() + "/.m2/settings.xml");
057    }
058
059}