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 */
019package pt.ua.dicoogle.rGUI.client;
020
021
022import java.net.InetAddress;
023import java.net.UnknownHostException;
024import java.rmi.NotBoundException;
025import java.rmi.RemoteException;
026import java.rmi.registry.LocateRegistry;
027import java.rmi.registry.Registry;
028import pt.ua.dicoogle.Main;
029import pt.ua.dicoogle.rGUI.MultihomeSslRMIClientSocketFactory;
030import pt.ua.dicoogle.rGUI.client.windows.ConnectWindow;
031import pt.ua.dicoogle.rGUI.client.windows.MainWindow;
032import pt.ua.dicoogle.rGUI.client.UIHelper.TrayIconCreator;
033
034import pt.ua.dicoogle.rGUI.interfaces.ILogin;
035import pt.ua.dicoogle.rGUI.interfaces.IAdmin;
036import pt.ua.dicoogle.rGUI.interfaces.IUser;
037import pt.ua.dicoogle.utils.KeysManager;
038
039/**
040 * This class is responsible for making the connection to the RMI GUI Server
041 *
042 * After the connection, the user can login to manage the server or to search
043 *
044 * @author Samuel Campos <samuelcampos@ua.pt>
045 */
046@Deprecated
047public class ConnectServer {
048    private String host;
049    private int port;
050    private ILogin login;
051    private Registry reg;
052    private ClientCore core;
053
054    /*
055     * Falta propagar uma excepção que indique uma falha na conexão
056     */
057    public ConnectServer(String host, int port) throws RemoteException, NotBoundException, UnknownHostException{
058        this.host = host;
059        this.port = port;
060        
061        System.out.println("Host: "+host);
062        System.out.println("Port: " + port);
063        
064        String TrustStorePath = KeysManager.getClientKeyPath();
065
066        //define the System property to use a SSL TrustStore
067        System.setProperty("javax.net.ssl.trustStore", TrustStorePath);
068
069        if (Main.isFixedClient())
070        {
071            
072            String keyStorePath = KeysManager.getServerKeyPath();
073
074            //define the System properties to use a SSLKeystore
075            System.setProperty("javax.net.ssl.keyStore", keyStorePath);
076            System.setProperty("javax.net.ssl.keyStorePassword", "dicooglepacs");
077        }
078
079        System.setProperty("sun.rmi.activation.execTimeout", "200");
080        
081
082        //get the Registry with the Remote Objects
083        reg = LocateRegistry.getRegistry(this.host, this.port, new MultihomeSslRMIClientSocketFactory());
084        //reg = LocateRegistry.getRegistry(this.host, this.port);
085        
086
087        login = (ILogin) reg.lookup("login");
088
089        
090        
091 
092        core = ClientCore.getInstance();
093
094        InetAddress serverAddr = InetAddress.getByName(host);
095            
096        core.setServerAddress(serverAddr);
097        
098    }
099    
100    /**
101     * Try to login (Administration and UserSearch) in server
102     *
103     * @param username
104     * @param pass
105     */
106    public boolean login(String username, String passwordHash) throws RemoteException{
107            
108            IAdmin admin = loginAdmin(username, passwordHash);
109            IUser user;
110
111            // if the user is administrator, automactly get the user
112            if(admin != null){
113                core.setAdmin(admin);
114                user = admin.getUser();
115            }
116            else
117                user = loginUser(username, passwordHash);
118
119            if(user != null)
120                core.setUser(user);
121
122
123            if(admin == null && user == null)
124                return false;
125            
126        TrayIconCreator.getInstance();
127
128        MainWindow.getInstance().setVisible(true);
129
130        ConnectWindow.getInstance().setVisible(false);
131        
132        MainWindow.getInstance().toFront();
133        return true;
134    }
135
136    /**
137     *
138     * @param username
139     * @param pass
140     * @return the reference to the administration main object
141     * @throws RemoteException
142     */
143    private IAdmin loginAdmin(String username, String pass) throws RemoteException{
144        if (login != null)
145            return login.LoginAdmin(username, pass);
146        
147        return null;
148    }
149
150    /**
151     *
152     * @param username
153     * @param pass
154     * @return the reference to the user search main object
155     * @throws RemoteException
156     */
157    private IUser loginUser(String username, String pass) throws RemoteException{
158        if (login != null)
159            return login.LoginUser(username, pass);
160        
161        return null;
162    }
163}