001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020package pt.ua.dicoogle.utils;
021
022import java.io.BufferedOutputStream;
023import java.io.DataOutputStream;
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028
029/**
030 *
031 * @author Luís A. Bastião Silva <bastiao@ua.pt>
032 */
033public class KeysManager
034{
035    private static final String LOCAL_PATH = "Server_Keystore";
036    
037    public static String getServerKeyPath()
038    {
039        File keyPath = new File(LOCAL_PATH);
040        if(keyPath.exists() && keyPath.canRead()){
041            return keyPath.getAbsolutePath();        
042        }
043        
044        //COPY FILE FROM JAR
045        InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Server_Keystore");
046        if (stream!=null)
047        {            
048            try
049            {
050                DataOutputStream out = new DataOutputStream(
051                        new BufferedOutputStream(
052                        new FileOutputStream(keyPath)));
053                int c;
054                while((c = stream.read()) != -1)
055                {
056                        out.writeByte(c);
057                }
058                stream.close();
059                out.close();
060            }
061            catch(IOException e)
062            {
063                System.err.println("Error Writing/Reading Streams.");
064            }
065        }
066        else
067        {
068            //DebugManager.getInstance().debug("Missing the KeyStore file that contains the SSL keys of server");
069            System.err.println("Missing the KeyStore file that contains the SSL keys of server");
070        }
071    
072        return keyPath.getAbsolutePath();
073}
074
075
076    public static String getClientKeyPath()
077    {
078        File keyPath = new File(LOCAL_PATH);
079        if(keyPath.exists() && keyPath.canRead()){
080            return keyPath.getAbsolutePath();        
081        }
082
083        //Last shot
084        InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Client_Truststore");
085            
086        if (stream!=null)
087        {
088            try
089            {
090                DataOutputStream out = new DataOutputStream(
091                        new BufferedOutputStream(
092                        new FileOutputStream(keyPath)));
093                int c;
094                while((c = stream.read()) != -1)
095                {
096                        out.writeByte(c);
097                }
098                stream.close();
099                out.close();
100
101            }
102            catch(IOException e)
103            {
104                    System.err.println("Error Writing/Reading Streams.");
105            }
106        }
107        else
108        {
109            //DebugManager.getInstance().debug("Missing the TrustStore file that confirms the server certificate");
110            System.err.println("Missing the TrustStore file that confirms the server certificate");
111            System.exit(-3);
112        }
113    
114
115    return keyPath.getAbsolutePath();
116    }
117}