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