Project

General

Profile

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
 *    Release: @release@
8
 *
9
 *   '$Author: jones $'
10
 *     '$Date: 2000-08-14 13:53:34 -0700 (Mon, 14 Aug 2000) $'
11
 * '$Revision: 349 $'
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
import java.util.PropertyResourceBundle;
20
import java.io.FileInputStream;
21
import java.io.PrintWriter;
22
import java.rmi.*;
23
import SrbJavaGlueInterface;
24
import RMIControllerInterface;
25

    
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
    static String debug = null;
34
    HttpSession session = null;
35
    // JNI (Java Native Interface) routines for SRB
36
    static String srbHost;
37
    static String srbPort;
38
    static String RMIhost;
39
    static RMIControllerInterface rmicon;
40
    static SrbJavaGlueInterface srbJG;
41
    static {
42
        try { 
43
            PropertyResourceBundle SRBProps = null;
44
            // SRB properties that tells about the location of SRB RMI Server
45
            // srbProps.properties should reside on metacat server
46
            SRBProps = (PropertyResourceBundle)
47
                        PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
48
            srbHost = (String)SRBProps.handleGetObject("host");
49
            srbPort = (String)SRBProps.handleGetObject("port");
50
            // should handle missing RMIhost name here
51
            RMIhost = (String)SRBProps.handleGetObject("RMIhost");
52
            String name = "//" + RMIhost + "/RMIController";
53
            rmicon = (RMIControllerInterface)Naming.lookup(name);
54
            name = "//" + RMIhost + "/SrbJavaGlue";
55
            srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
56
        } catch (Exception e) {
57
            System.err.println("MetaCatSession static: " + e.getMessage());
58
            System.exit(0);
59
        }    
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
                            String username, String password)
70
                            throws IllegalStateException {
71
      // 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
                            String username, String password)  
80
                            throws IllegalStateException {
81
                                      
82
      // 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
      sess.setMaxInactiveInterval(-1);
91
      sess.setAttribute("username", username);
92
      sess.setAttribute("password", password);
93
      
94
      return sess; 
95
    }
96

    
97
    /** Try to make user authentication through SRB RMI Connection */
98
    public boolean userAuth(String username, String password)
99
                    throws RemoteException  { 
100
      int srbconn = 0;
101

    
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
      
111
      // try SRB RMI Connection
112
      // integer value of the SBR Connection ID is returned only
113
      try { 
114
        srbconn = srbJG.rmiConnectJ( srbHost, srbPort, password, username );
115
      } catch (RemoteException e) {
116
        rmicon.restart();
117
        throw new RemoteException("MetaCatSession.userAuth() - " +
118
                                  "Error on rmiConnectJ(): " + e.getMessage());
119
      }
120

    
121
      // check if successfull
122
      if ( srbconn <= 0 ) {
123
        rmicon.restart();
124
        return false;
125
      }
126

    
127
      // we don't need that connection. close it.
128
      try {
129
        int err = srbJG.rmiFinishJ( srbconn );
130
      } catch (RemoteException e) {}
131
      
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

    
137
    /**
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
}
(22-22/29)