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: bojilova $'
|
9
|
* '$Date: 2000-08-10 14:48:27 -0700 (Thu, 10 Aug 2000) $'
|
10
|
* '$Revision: 337 $'
|
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.util.PropertyResourceBundle;
|
19
|
import java.io.FileInputStream;
|
20
|
import java.io.PrintWriter;
|
21
|
import java.rmi.*;
|
22
|
import SrbJavaGlueInterface;
|
23
|
import RMIControllerInterface;
|
24
|
|
25
|
/**
|
26
|
* A Class that implements session tracking for MetaCatServlet users.
|
27
|
* User's login data are stored in the session object.
|
28
|
* User authentication is made through SRB RMI Connection.
|
29
|
*/
|
30
|
public class MetaCatSession {
|
31
|
|
32
|
static String debug = null;
|
33
|
HttpSession session = null;
|
34
|
// JNI (Java Native Interface) routines for SRB
|
35
|
static String srbHost;
|
36
|
static String srbPort;
|
37
|
static String RMIhost;
|
38
|
static RMIControllerInterface rmicon;
|
39
|
static SrbJavaGlueInterface srbJG;
|
40
|
static {
|
41
|
try {
|
42
|
PropertyResourceBundle SRBProps = null;
|
43
|
// SRB properties that tells about the location of SRB RMI Server
|
44
|
// srbProps.properties should reside on metacat server
|
45
|
SRBProps = (PropertyResourceBundle)
|
46
|
PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
|
47
|
srbHost = (String)SRBProps.handleGetObject("host");
|
48
|
srbPort = (String)SRBProps.handleGetObject("port");
|
49
|
// should handle missing RMIhost name here
|
50
|
RMIhost = (String)SRBProps.handleGetObject("RMIhost");
|
51
|
String name = "//" + RMIhost + "/RMIController";
|
52
|
rmicon = (RMIControllerInterface)Naming.lookup(name);
|
53
|
name = "//" + RMIhost + "/SrbJavaGlue";
|
54
|
srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
|
55
|
} catch (Exception e) {
|
56
|
System.err.println("MetaCatSession static: " + e.getMessage());
|
57
|
System.exit(0);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
/** Construct a MetaCatSession
|
62
|
*
|
63
|
* @param request the request made from the client
|
64
|
* @param username the username entered when login
|
65
|
* @param password the password entered when login
|
66
|
*/
|
67
|
public MetaCatSession (HttpServletRequest request,
|
68
|
String username, String password)
|
69
|
throws IllegalStateException {
|
70
|
// create a new HTTPSession object
|
71
|
this.session = getSession(request, username, password);
|
72
|
}
|
73
|
|
74
|
|
75
|
|
76
|
/** Get new HttpSession and store username & password in it */
|
77
|
private HttpSession getSession(HttpServletRequest request,
|
78
|
String username, String password)
|
79
|
throws IllegalStateException {
|
80
|
|
81
|
// get the current session object, create one if necessary
|
82
|
HttpSession sess = request.getSession(true);
|
83
|
// if it is still in use unvalidate and get a new one
|
84
|
if ( !sess.isNew() ) {
|
85
|
sess.invalidate();
|
86
|
sess = request.getSession(true);
|
87
|
}
|
88
|
// store username & password in the session for later use if necessary
|
89
|
sess.setMaxInactiveInterval(-1);
|
90
|
sess.setAttribute("username", username);
|
91
|
sess.setAttribute("password", password);
|
92
|
|
93
|
return sess;
|
94
|
}
|
95
|
|
96
|
/** Try to make user authentication through SRB RMI Connection */
|
97
|
public boolean userAuth(String username, String password)
|
98
|
throws RemoteException {
|
99
|
int srbconn = 0;
|
100
|
|
101
|
// look up for SRBJavaGlue
|
102
|
try {
|
103
|
String name = "//" + RMIhost + "/SrbJavaGlue";
|
104
|
srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
|
105
|
} catch (Exception e) {
|
106
|
throw new
|
107
|
RemoteException("MetaCatSession look up for SrbJavaGlue failed");
|
108
|
}
|
109
|
|
110
|
// try SRB RMI Connection
|
111
|
// integer value of the SBR Connection ID is returned only
|
112
|
try {
|
113
|
srbconn = srbJG.rmiConnectJ( srbHost, srbPort, password, username );
|
114
|
} catch (RemoteException e) {
|
115
|
rmicon.restart();
|
116
|
throw new RemoteException("MetaCatSession.userAuth() - " +
|
117
|
"Error on rmiConnectJ(): " + e.getMessage());
|
118
|
}
|
119
|
|
120
|
// check if successfull
|
121
|
if ( srbconn <= 0 ) {
|
122
|
rmicon.restart();
|
123
|
return false;
|
124
|
}
|
125
|
|
126
|
// we don't need that connection. close it.
|
127
|
try {
|
128
|
int err = srbJG.rmiFinishJ( srbconn );
|
129
|
} catch (RemoteException e) {}
|
130
|
|
131
|
// store SRB Connection in the session for later use if necessary
|
132
|
this.session.putValue("srbconnection", new Integer(srbconn));
|
133
|
return true;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Invalidate this HTTPSession object.
|
138
|
* All objects stored in the session are unbound
|
139
|
*/
|
140
|
public void disconnect() {
|
141
|
|
142
|
this.session.invalidate();
|
143
|
}
|
144
|
|
145
|
}
|