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
 *
8
 *   '$Author: bojilova $'
9
 *     '$Date: 2000-07-18 11:40:44 -0700 (Tue, 18 Jul 2000) $'
10
 * '$Revision: 294 $'
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.putValue("username", username);
90
      sess.putValue("password", password);
91
      
92
      return sess; 
93
    }
94

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

    
100
      // look up for SRBJavaGlue
101
      try {
102
        String name = "//" + RMIhost + "/SrbJavaGlue";
103
        srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
104
      } catch (Exception e) {
105
        throw new 
106
            RemoteException("MetaCatSession look up for SrbJavaGlue failed");
107
      }
108
      
109
      // try SRB RMI Connection
110
      // integer value of the SBR Connection ID is returned only
111
      try { 
112
        srbconn = srbJG.rmiConnectJ( srbHost, srbPort, password, username );
113
      } catch (RemoteException e) {
114
        rmicon.restart();
115
        throw new RemoteException("MetaCatSession.userAuth() - " +
116
                                  "Error on rmiConnectJ(): " + e.getMessage());
117
      }
118

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

    
125
      // we don't need that connection. close it.
126
      try {
127
        int err = srbJG.rmiFinishJ( srbconn );
128
      } catch (RemoteException e) {}
129
      
130
      // store SRB Connection in the session for later use if necessary
131
      this.session.putValue("srbconnection", new Integer(srbconn));
132
      return true; 
133
    }
134

    
135
    /**
136
     * Invalidate this HTTPSession object. 
137
     * All objects stored in the session are unbound 
138
     */
139
    public void disconnect() {
140
        
141
        this.session.invalidate();
142
    }    
143

    
144
}
(21-21/26)