<commit_before>/**
 * ***************************************************************
 * Agent.GUI is a framework to develop Multi-agent based simulation 
 * applications based on the JADE - Framework in compliance with the 
 * FIPA specifications. 
 * Copyright (C) 2010 <NAME> and DAWIS
 * http://www.dawis.wiwi.uni-due.de
 * http://sourceforge.net/projects/agentgui/
 * http://www.agentgui.org 
 *
 * GNU Lesser General Public License
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation,
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * **************************************************************
 */
package agentgui.core.gui.options.https;

import java.awt.Dialog;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import agentgui.core.application.Language;

/**
 * This class allows the user to manage a TrustStore.
 * 
 * @author <NAME> <<EMAIL>>
 * @version 1.0
 * @since 05-04-2016
 */
public class TrustStoreController {
	
	private DefaultTableModel tableModel;
	protected KeyStore trustStore;
	private FileInputStream trustStoreInputStream = null;
	private File trustStoreFile;
	private FileOutputStream trustStoreOutputStream;
	private String trustStorePassword;

	/**
	 * returns the key store object for direct interaction
	 *
	 * @return the key store object
	 */
	public KeyStore getKeyStore(){
		return trustStore;
	}
		
	/**
	 * Gets the trust store password.
	 *
	 * @return the trust store password
	 */
	public String getTrustStorePassword() {
		return trustStorePassword;
	}

	/**
	 * Sets the trust store password.
	 *
	 * @param trustStorePassword the new trust store password
	 */
	public void setTrustStorePassword(String trustStorePassword) {
		this.trustStorePassword = trustStorePassword;
	}

	private Dialog ownerDialog;
	private boolean initialized = false;

	/*
	 * generates a blank controller, which still needs to initialize the trustStore
	 */
	public TrustStoreController(Dialog ownerDialog){
		this(ownerDialog, null, null, false);
	}
	
	
	/**
	 * This Initializes the TrustStoreController.
	 */
	public TrustStoreController(Dialog ownerDialog, File trustStoreFile, String trustStorePassword, boolean edit) {
		this.ownerDialog = ownerDialog;
		try {
			trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
			if (trustStoreFile != null && trustStorePassword != null) {
				if(edit){
					openTrustStore(trustStoreFile, trustStorePassword);
				} else {
					createTrustStore(trustStoreFile, trustStorePassword);
				}
			}
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method allows the user to create a TrustStore and protect its
	 * integrity with a password.
	 *
	 * @param trustStoreFile the TrustStore file
	 * @param trustStorePassword the TrustStore password
	 */
	public void createTrustStore(File trustStoreFile, String trustStorePassword) {
		openTrustStore(trustStoreFile, trustStorePassword);
		saveTrustStore();
	}

	/**
	 * Initializes the truststore controller.
	 * @param trustStoreFile the trust store file
	 * @param trustStorePassword the trust store password
	 */
	public void init(File trustStoreFile, String trustStorePassword) {
		this.trustStorePassword = <PASSWORD>StorePassword;
		this.trustStoreFile = trustStoreFile;
		try {
			trustStore.load(null, trustStorePassword.toCharArray());
			initialized = true;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method open a TrustStore. It returns true if the password is correct.
	 *
	 * @param trustStoreFile the TrustStore name
	 * @param trustStorePassword the TrustStore password
	 * @return true, if successful
	 */
	public boolean openTrustStore(File trustStoreFile, String trustStorePassword) {
		boolean successful = false;
		init(trustStoreFile, trustStorePassword);
		this.trustStoreInputStream = null;

		try {
			if (trustStoreFile.exists()) {
				trustStoreInputStream = new FileInputStream(trustStoreFile);
			}
			// --- Loads the TrustStore from the given InputStream -------------
			trustStore.load(trustStoreInputStream, trustStorePassword.toCharArray());
			successful = true;<commit_msg>Add option to directly pass InputStream to TrustStoreController<commit_after>/**
 * ***************************************************************
 * Agent.GUI is a framework to develop Multi-agent based simulation 
 * applications based on the JADE - Framework in compliance with the 
 * FIPA specifications. 
 * Copyright (C) 2010 <NAME> and DAWIS
 * http://www.dawis.wiwi.uni-due.de
 * http://sourceforge.net/projects/agentgui/
 * http://www.agentgui.org 
 *
 * GNU Lesser General Public License
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation,
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * **************************************************************
 */
package agentgui.core.gui.options.https;

import java.awt.Dialog;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import agentgui.core.application.Language;

/**
 * This class allows the user to manage a TrustStore.
 * 
 * @author <NAME> <<EMAIL>>
 * @version 1.0
 * @since 05-04-2016
 */
public class TrustStoreController {
	
	private DefaultTableModel tableModel;
	protected KeyStore trustStore;
	private InputStream trustStoreInputStream = null;
	private File trustStoreFile;
	private FileOutputStream trustStoreOutputStream;
	private String trustStorePassword;

	/**
	 * returns the key store object for direct interaction
	 *
	 * @return the key store object
	 */
	public KeyStore getKeyStore(){
		return trustStore;
	}
		
	/**
	 * Gets the trust store password.
	 *
	 * @return the trust store password
	 */
	public String getTrustStorePassword() {
		return trustStorePassword;
	}

	/**
	 * Sets the trust store password.
	 *
	 * @param trustStorePassword the new trust store password
	 */
	public void setTrustStorePassword(String trustStorePassword) {
		this.trustStorePassword = trustStorePassword;
	}

	private Dialog ownerDialog;
	private boolean initialized = false;

	/*
	 * generates a blank controller, which still needs to initialize the trustStore
	 */
	public TrustStoreController(Dialog ownerDialog){
		this(ownerDialog, null, null, false);
	}
	
	
	/**
	 * This Initializes the TrustStoreController.
	 */
	public TrustStoreController(Dialog ownerDialog, File trustStoreFile, String trustStorePassword, boolean edit) {
		this.ownerDialog = ownerDialog;
		try {
			trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
			if (trustStoreFile != null && trustStorePassword != null) {
				if(edit){
					openTrustStore(trustStoreFile, trustStorePassword);
				} else {
					createTrustStore(trustStoreFile, trustStorePassword);
				}
			}
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method allows the user to create a TrustStore and protect its
	 * integrity with a password.
	 *
	 * @param trustStoreFile the TrustStore file
	 * @param trustStorePassword the TrustStore password
	 */
	public void createTrustStore(File trustStoreFile, String trustStorePassword) {
		openTrustStore(trustStoreFile, trustStorePassword);
		saveTrustStore();
	}

	/**
	 * Initializes the truststore controller.
	 * @param trustStoreFile the trust store file
	 * @param trustStorePassword the trust store password
	 */
	public void init(File trustStoreFile, String trustStorePassword) {
		this.trustStorePassword = <PASSWORD>;
		this.trustStoreFile = trustStoreFile;
		try {
			trustStore.load(null, trustStorePassword.toCharArray());
			initialized = true;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * This method open a TrustStore. It returns true if the password is correct.
	 *
	 * @param trustStoreFile the TrustStore name
	 * @param trustStorePassword the TrustStore password
	 * @return true, if successful
	 */
	public boolean openTrustStore(File trustStoreFile, String trustStorePassword) {
		if (trustStoreFile.exists()) {
			try {
				trustStoreInputStream = new FileInputStream(trustStoreFile);
				init(trustStoreFile, trustStorePassword);
				return openTrustStoreFromStream(trustStoreInputStream, trustStorePassword);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * This method opens a TrustStore directly from stream. It returns true if the password is correct. It will NOT initialize this controller, because the file for write interaction is missing
	 *
	 * @param trustStoreInputStream the TrustStore name
	 * @param trustStorePassword the TrustStore password
	 * @return true, if successful
	 */
	public boolean openTrustStoreFromStream(InputStream trustStoreInputStream, String trustStorePassword) {
		boolean successful = false;
		this.trustStoreInputStream = trustStoreInputStream;

		try {
			// --- Loads the TrustStore from the given InputStream -------------
			trustStore.load(trustStoreInputStream, trustStorePassword.toCharArray());
			successful = true;
{'fraction_non_alphanumeric': 0.10522220300881895, 'fraction_numerical': 0.005706380771226007, 'mean_word_length': 4.3900279589934765, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4331535', 'n_tokens_mistral': 3341, 'n_tokens_neox': 2971, 'n_words': 1006}