Revision 209
Added by bojilova over 24 years ago
src/edu/ucsb/nceas/metacat/MetaCatSession.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A Class that tracks sessions for MetaCatServlet users. |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Matt Jones |
|
7 |
* |
|
8 |
* '$Author$' |
|
9 |
* '$Date$' |
|
10 |
* '$Revision$' |
|
11 |
*/ |
|
12 |
|
|
13 |
package edu.ucsb.nceas.metacat; |
|
14 |
|
|
15 |
import javax.servlet.http.HttpSession; |
|
16 |
import javax.servlet.http.HttpServletRequest; |
|
17 |
import java.util.Properties; |
|
18 |
import java.io.FileInputStream; |
|
19 |
import java.rmi.*; |
|
20 |
import SrbJavaGlueInterface; |
|
21 |
|
|
22 |
/** |
|
23 |
* A Class that implements session tracking for MetaCatServlet users. |
|
24 |
* User's login data are stored in the session object. |
|
25 |
* User authentication is made through SRB RMI Connection. |
|
26 |
*/ |
|
27 |
public class MetaCatSession { |
|
28 |
|
|
29 |
HttpSession session = null; |
|
30 |
// JNI (Java Native Interface) routines for SRB |
|
31 |
static String srbHost; |
|
32 |
static String srbPort; |
|
33 |
// temporary need to read user & password from srbProps.properties |
|
34 |
static String srbUser; |
|
35 |
static String srbPass; |
|
36 |
static SrbJavaGlueInterface srbJG; |
|
37 |
static { |
|
38 |
try { |
|
39 |
Properties SRBProps = new Properties(); |
|
40 |
// SRB properties that tells about the location of SRB RMI Server |
|
41 |
// srbProps.properties should reside on metacat server |
|
42 |
FileInputStream in = new FileInputStream("srbProps.properties"); |
|
43 |
SRBProps.load(in); |
|
44 |
in.close(); |
|
45 |
srbHost = (String)SRBProps.get("host"); |
|
46 |
srbPort = (String)SRBProps.get("port"); |
|
47 |
// temporally reading user & password from srbProps.properties |
|
48 |
// for existing MCAT account like higgins/neetar |
|
49 |
srbUser = (String)SRBProps.get("user"); |
|
50 |
srbPass = (String)SRBProps.get("password"); |
|
51 |
// should handle missing RMIhost name here |
|
52 |
String RMIhost = (String)SRBProps.get("RMIhost"); |
|
53 |
String name = "//" + RMIhost + "/SrbJavaGlue"; |
|
54 |
srbJG = (SrbJavaGlueInterface)Naming.lookup(name); |
|
55 |
} catch (Exception e) { |
|
56 |
System.out.println ("MetaCatSession static: " + e.getMessage()); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
/** Construct a MetaCatSession |
|
61 |
* |
|
62 |
* @param request the request made from the client |
|
63 |
* @param username the username entered when login |
|
64 |
* @param password the password entered when login |
|
65 |
*/ |
|
66 |
public MetaCatSession (HttpServletRequest request, |
|
67 |
String username, String password) { |
|
68 |
|
|
69 |
// create a new HTTPSession object |
|
70 |
this.session = getSession(request, username, password); |
|
71 |
} |
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
/** Get new HttpSession and store username & password in it */ |
|
76 |
private HttpSession getSession(HttpServletRequest request, |
|
77 |
String username, String password) { |
|
78 |
|
|
79 |
// get the current session object, create one if necessary |
|
80 |
HttpSession sess = request.getSession(true); |
|
81 |
// if it is still in use unvalidate and get a new one |
|
82 |
if ( !sess.isNew() ) { |
|
83 |
sess.invalidate(); |
|
84 |
sess = request.getSession(true); |
|
85 |
} |
|
86 |
// store username & password in the session for later use if necessary |
|
87 |
sess.putValue("username", username); |
|
88 |
sess.putValue("password", password); |
|
89 |
|
|
90 |
return sess; |
|
91 |
} |
|
92 |
|
|
93 |
/** Try to make user authentication through SRB Connection */ |
|
94 |
public boolean userAuth(String password) |
|
95 |
throws RemoteException { |
|
96 |
int srbconn = 0; |
|
97 |
int err; |
|
98 |
|
|
99 |
// try SRB Connection |
|
100 |
// password should be used, not srbPass read from srbProps |
|
101 |
// integer value of the SBR Connection ID is returned only |
|
102 |
try { |
|
103 |
srbconn = srbJG.clConnectJ( srbHost, srbPort, srbPass ); |
|
104 |
} catch (RemoteException e) { |
|
105 |
throw new RemoteException("MetaCatSession.userAuth() - " + |
|
106 |
"Error on clConnectJ(): " + e.getMessage()); |
|
107 |
} |
|
108 |
|
|
109 |
// check if successfull |
|
110 |
if ( srbconn < 0 ) { |
|
111 |
// srb connection is already finished from clConnectJ() routine |
|
112 |
return false; |
|
113 |
//throw new RemoteException("MetaCatSession.userAuth() failure: " + |
|
114 |
// "SRB Connection failed"); |
|
115 |
} |
|
116 |
|
|
117 |
// we don't need that connection. close it. |
|
118 |
try { |
|
119 |
err = srbJG.clFinishJ( srbconn ); |
|
120 |
} catch (RemoteException e) { |
|
121 |
throw new RemoteException("MetaCatSession.userAuth() - " + |
|
122 |
"Error on clFinishJ(): " + e.getMessage()); |
|
123 |
} |
|
124 |
|
|
125 |
// store SRB Connection in the session for later use if necessary |
|
126 |
this.session.putValue("srbconnection", new Integer(srbconn)); |
|
127 |
return true; |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Invalidate this HTTPSession object. |
|
132 |
* All objects stored in the session are unbound |
|
133 |
*/ |
|
134 |
public void disconnect() { |
|
135 |
|
|
136 |
this.session.invalidate(); |
|
137 |
} |
|
138 |
|
|
139 |
} |
|
0 | 140 |
Also available in: Unified diff
New- class that encapsulates session creation and user authentication