ua.com.ecotep.unianalysis.security.ConfigParamsContext.java Source code

Java tutorial

Introduction

Here is the source code for ua.com.ecotep.unianalysis.security.ConfigParamsContext.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.com.ecotep.unianalysis.security;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author Andrey
 */
public class ConfigParamsContext implements ConfigParams {

    public ConfigParamsContext() {
    }

    @Override
    public ServerConfigParams getServerConfigParams() throws Exception {
        ServerConfigParams scp;
        ConfigEncoder ce = new ConfigEncoderImpl();

        File f = new File("unianalysis.xml");
        Properties properties = new Properties();

        properties.loadFromXML(new FileInputStream(f));
        String serverDBPath = null;
        String serverDBUser = null;
        String serverDBPass = null;

        for (String key : properties.stringPropertyNames()) {
            if (key.equals("serverpath")) {
                serverDBPath = ce.decode(Base64.decodeBase64(properties.getProperty(key).getBytes()));
            }
            if (key.equals("user")) {
                serverDBUser = ce.decode(Base64.decodeBase64(properties.getProperty(key).getBytes()));
            }
            if (key.equals("md5")) {
                serverDBPass = ce.decode(Base64.decodeBase64(properties.getProperty(key).getBytes()));
            }
        }
        scp = new ServerConfigParamsImpl(serverDBPath, serverDBUser, serverDBPass);
        return scp;
    }

    @Override
    public void setServerConfigParams(ServerConfigParams params) throws Exception {
        DataOutputStream out = null;
        ConfigEncoder ce = new ConfigEncoderImpl();
        File file = new File("unianalysis.xml");
        Properties properties = new Properties();
        if (!file.exists()) {
            file.createNewFile();
            properties.setProperty("serverpath", new String(Base64.encodeBase64(ce.encode(params.getDbserver()))));
            properties.setProperty("user", new String(Base64.encodeBase64(ce.encode(params.getDbusername()))));
            properties.setProperty("md5", new String(Base64.encodeBase64(ce.encode(params.getDbpassword()))));
            properties.storeToXML(new FileOutputStream(file), "");
        }
    }
}