Revision 4217 trunk/libraries/libAuthenticationService/src/com/iver/cit/gvsig/authentication/Authentication.java

View differences:

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

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

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

  
42
			//releaseConnection("default");
43 48
			return found;
44 49
			
45 50
    	} catch (SQLException e) {
......
53 58
		}finally{
54 59
			if (dbHandler.getConnection() != null){
55 60
				try{
61
					stm.close();
62
					rs.close(); 
56 63
					dbHandler.releaseConnection("default");
57 64
				}catch(Exception fin){
58 65
					m_log.error("",fin);
......
62 69
			}
63 70
		}
64 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 {
65 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
    }     
66 267
}

Also available in: Unified diff