Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libAuthenticationService / src / com / iver / cit / gvsig / authentication / Authentication.java @ 4217

History | View | Annotate | Download (6.68 KB)

1
package com.iver.cit.gvsig.authentication;
2

    
3
import java.sql.ResultSet;
4
import java.sql.SQLException;
5
import java.sql.Statement;
6
import java.util.Hashtable;
7

    
8
import com.iver.cit.gvsig.authentication.db.DatabaseHandler;
9
import com.iver.cit.gvsig.authentication.db.QueryBuilder;
10
import com.iver.cit.gvsig.authentication.exception.DBConnectionException;
11
import com.iver.cit.gvsig.authentication.exception.InvalidUserException;
12
import com.iver.cit.gvsig.authentication.utils.Logger;
13

    
14
/**
15
 *  Provides the functionality to process the authentication and the authorization in the DB. 
16
 * @author laura
17
 */
18
public class Authentication {
19
        
20
        private DatabaseHandler dbHandler;
21
        private Logger m_log;
22
        
23
        public Authentication()        {
24
                dbHandler = new DatabaseHandler(); 
25
        }
26
        
27
        /**
28
     * Tries to log in the db with the info sent by the user.
29
     * @param userName
30
     * @param password
31
     * @return
32
     */
33
    public boolean login(String userName, String password)
34
    {
35
            Statement stm = null;
36
                ResultSet rs = null;
37
                String query = null;
38
                boolean found;                
39
               
40
            try {
41
                
42
                    dbHandler.initConnection("default");
43
                        query = QueryBuilder.getLoginQuery(userName, password);
44
                        stm = dbHandler.getConnection().createStatement();
45
                        rs = stm.executeQuery(query);
46
                        found = rs.next();                        
47

    
48
                        return found;
49
                        
50
            } catch (SQLException e) {
51
                    m_log.error("DatabaseHandler.login()." ,e);
52
                        e.printStackTrace();
53
                        return false;
54
                } catch (DBConnectionException e) {
55
                        m_log.error("DatabaseHandler.login()." ,e);
56
                        e.printStackTrace();
57
                        return false;
58
                }finally{
59
                        if (dbHandler.getConnection() != null){
60
                                try{
61
                                        stm.close();
62
                                        rs.close(); 
63
                                        dbHandler.releaseConnection("default");
64
                                }catch(Exception fin){
65
                                        m_log.error("",fin);
66
                                        fin.printStackTrace(); 
67
                                        return false;
68
                                }
69
                        }
70
                }
71
    }
72
        
73
    /**
74
     * gets the gvSIG config file
75
     * @param userName
76
     * @param password
77
     * @return a String with the gvSIG confog file
78
     * @throws InvalidUserException
79
     */
80
    public String getGvSigConfig(String userName, String password)
81
                    throws InvalidUserException 
82
    {
83
            if (login(userName, password)){
84
                    return getGvSigConfig();
85
            }
86
            else{
87
                    throw new InvalidUserException("Authentication ERROR: Invalid user trying to get gvSIG file");
88
            }
89
    }
90
    
91
        /**
92
     * Gets the gvSig configuration file associated to the user role.
93
     * First checks if the user/password is correct.
94
     * @param userName
95
     * @param password
96
     * @return
97
     */
98
    private String getGvSigConfig()
99
    {
100
            Statement stm = null;
101
                ResultSet rs = null;
102
                String query = null;        
103
               
104
            try {
105
                
106
                    dbHandler.initConnection("default");
107
                        query = QueryBuilder.getGvSigConfigQuery();
108
                        stm = dbHandler.getConnection().createStatement();
109
                        rs = stm.executeQuery(query);
110
                        return "";
111
                        
112
            } catch (SQLException e) {
113
                    m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
114
                        e.printStackTrace();
115
                        return "";
116
                } catch (DBConnectionException e) {
117
                        m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
118
                        e.printStackTrace();
119
                        return "";
120
                }finally{
121
                        if (dbHandler.getConnection() != null){
122
                                try{
123
                                        dbHandler.releaseConnection("default");
124
                                        rs.close();
125
                                        stm.close();
126
                                }catch(Exception fin){
127
                                        m_log.error("",fin);
128
                                        fin.printStackTrace(); 
129
                                        return "";
130
                                }
131
                        }
132
                }
133
    }    
134
  
135
    /**
136
     * Returns mapserver map file for this user
137
     * @param userName
138
     * @param password
139
     * @return
140
     * @throws InvalidUserException
141
     */
142
    public String getMapFile(String userName, String password)
143
                    throws InvalidUserException
144
    {
145
            if (login(userName, password)){
146
                    return getMapFile();
147
            }
148
            else{
149
                    throw new InvalidUserException("Authentication ERROR: Invalid user trying to get gvSIG file");
150
            }
151
    }    
152
        /**
153
     * Gets the mapserver map file associated to the user role.
154
     * First checks if the user/password is correct.
155
     * @param userName
156
     * @param password
157
     * @return
158
     */
159
    private String getMapFile()
160
    {
161
            Statement stm = null;
162
                ResultSet rs = null;
163
                String query = null;        
164
               
165
            try {
166
                
167
                    dbHandler.initConnection("default");
168
                        query = QueryBuilder.getMapConfigQuery();
169
                        stm = dbHandler.getConnection().createStatement();
170
                        rs = stm.executeQuery(query);                
171
                        return "";
172
                        
173
            } catch (SQLException e) {
174
                    m_log.error("Authentication.getMapFile()." ,e);
175
                        e.printStackTrace();
176
                        return "";
177
                } catch (DBConnectionException e) {
178
                        m_log.error("Authentication.getMapFile()." ,e);
179
                        e.printStackTrace();
180
                        return "";
181
                }finally{
182
                        if (dbHandler.getConnection() != null){
183
                                try{
184
                                        dbHandler.releaseConnection("default");
185
                                        rs.close();
186
                                        stm.close();
187
                                }catch(Exception fin){
188
                                        m_log.error("",fin);
189
                                        fin.printStackTrace(); 
190
                                        return "";
191
                                }
192
                        }
193
                }
194
    }     
195
        /**
196
     * Gets all the configuration associated to this user/password
197
     * returns the gvSIG config file and the mapserver map file 
198
     * First checks if the user/password is correct.
199
     * @param userName
200
     * @param password
201
     * @return
202
     */
203
    public Hashtable getConfig(String userName, String password)
204
                    throws InvalidUserException
205
    {
206
            Hashtable ht = new Hashtable();
207
            String query;
208
               Statement stm = null;
209
                ResultSet rs = null;
210
                String mapserverFile = "";
211
                String gvSigFile = "";
212
            try {
213
                        if(login(userName, password)){
214

    
215
                                dbHandler.initConnection("default");
216
                                query = QueryBuilder.getMapConfigQuery();
217
                                stm = dbHandler.getConnection().createStatement();
218
                                rs = stm.executeQuery(query);        
219
                                if (rs.first()){ 
220
                                        mapserverFile = rs.getString(0);
221
                                }
222
                                rs.close();
223
                                stm.close();
224
                                
225
                                query = QueryBuilder.getGvSigConfigQuery();
226
                                stm = dbHandler.getConnection().createStatement();
227
                                rs = stm.executeQuery(query);
228
                                if (rs.first()){ 
229
                                        gvSigFile = rs.getString(0);
230
                                }
231
                                                                                              
232
                                ht.put("gvsig_config", gvSigFile);
233
                                ht.put("mapserver_file", mapserverFile);
234
                        }                    
235
                    else{
236
                            throw new InvalidUserException ("Authentication Error: Invalid user.");                         
237
                    }
238
                    return ht;
239
 
240
               } catch (SQLException e) {
241
                    m_log.error("Authentication.getConfig()." ,e);
242
                        e.printStackTrace();
243
                        return null;
244
                } catch (DBConnectionException e) {
245
                        m_log.error("Authentication.getConfig()." ,e);
246
                        e.printStackTrace();
247
                        return null;
248
                } catch (Exception e) {
249
                        m_log.error("Authentication.getConfig()." ,e);
250
                        e.printStackTrace();
251
                        return null;                                
252
                }finally{
253
                        if (dbHandler.getConnection() != null){
254
                                try{
255
                                        dbHandler.releaseConnection("default");
256
                                        rs.close();
257
                                        stm.close();
258
                                }catch(Exception fin){
259
                                        m_log.error("",fin);
260
                                        fin.printStackTrace(); 
261
                                        return null;
262
                                }
263
                        }
264
                }                    
265

    
266
    }     
267
}