Project

General

Profile

1 209 bojilova
/**
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 237 bojilova
import java.util.PropertyResourceBundle;
19 209 bojilova
import java.io.FileInputStream;
20 294 bojilova
import java.io.PrintWriter;
21 209 bojilova
import java.rmi.*;
22
import SrbJavaGlueInterface;
23 294 bojilova
import RMIControllerInterface;
24 209 bojilova
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 294 bojilova
    static String debug = null;
33 209 bojilova
    HttpSession session = null;
34
    // JNI (Java Native Interface) routines for SRB
35
    static String srbHost;
36
    static String srbPort;
37 294 bojilova
    static String RMIhost;
38
    static RMIControllerInterface rmicon;
39 234 bojilova
    static SrbJavaGlueInterface srbJG;
40 209 bojilova
    static {
41 294 bojilova
        try {
42 237 bojilova
            PropertyResourceBundle SRBProps = null;
43 209 bojilova
            // SRB properties that tells about the location of SRB RMI Server
44
            // srbProps.properties should reside on metacat server
45 237 bojilova
            SRBProps = (PropertyResourceBundle)
46 252 bojilova
                        PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
47 237 bojilova
            srbHost = (String)SRBProps.handleGetObject("host");
48
            srbPort = (String)SRBProps.handleGetObject("port");
49 209 bojilova
            // should handle missing RMIhost name here
50 294 bojilova
            RMIhost = (String)SRBProps.handleGetObject("RMIhost");
51
            String name = "//" + RMIhost + "/RMIController";
52
            rmicon = (RMIControllerInterface)Naming.lookup(name);
53
            name = "//" + RMIhost + "/SrbJavaGlue";
54 209 bojilova
            srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
55
        } catch (Exception e) {
56 294 bojilova
            System.err.println("MetaCatSession static: " + e.getMessage());
57
            System.exit(0);
58 209 bojilova
        }
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 294 bojilova
                            String username, String password)
69
                            throws IllegalStateException {
70 209 bojilova
      // 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 229 bojilova
                            String username, String password)
79
                            throws IllegalStateException {
80 333 bojilova
81 209 bojilova
      // 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 333 bojilova
      sess.setMaxInactiveInterval(-1);
90 337 bojilova
      sess.setAttribute("username", username);
91
      sess.setAttribute("password", password);
92 209 bojilova
93
      return sess;
94
    }
95
96 212 bojilova
    /** Try to make user authentication through SRB RMI Connection */
97 294 bojilova
    public boolean userAuth(String username, String password)
98 209 bojilova
                    throws RemoteException  {
99
      int srbconn = 0;
100 294 bojilova
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 209 bojilova
110 212 bojilova
      // try SRB RMI Connection
111 209 bojilova
      // integer value of the SBR Connection ID is returned only
112 294 bojilova
      try {
113
        srbconn = srbJG.rmiConnectJ( srbHost, srbPort, password, username );
114 209 bojilova
      } catch (RemoteException e) {
115 294 bojilova
        rmicon.restart();
116 209 bojilova
        throw new RemoteException("MetaCatSession.userAuth() - " +
117 294 bojilova
                                  "Error on rmiConnectJ(): " + e.getMessage());
118 209 bojilova
      }
119
120
      // check if successfull
121 294 bojilova
      if ( srbconn <= 0 ) {
122
        rmicon.restart();
123 209 bojilova
        return false;
124
      }
125
126
      // we don't need that connection. close it.
127
      try {
128 294 bojilova
        int err = srbJG.rmiFinishJ( srbconn );
129
      } catch (RemoteException e) {}
130 209 bojilova
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 234 bojilova
136 209 bojilova
    /**
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
}