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-06-30 16:53:03 -0700 (Fri, 30 Jun 2000) $'
10
 * '$Revision: 252 $'
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.rmi.*;
21
import SrbJavaGlueInterface;
22

    
23
/**
24
 * A Class that implements session tracking for MetaCatServlet users.
25
 * User's login data are stored in the session object.
26
 * User authentication is made through SRB RMI Connection.
27
 */
28
public class MetaCatSession {
29

    
30
    HttpSession session = null;
31
    // JNI (Java Native Interface) routines for SRB
32
    static String srbHost;
33
    static String srbPort;
34
    static SrbJavaGlueInterface srbJG;
35
    static {
36
        try {
37
            PropertyResourceBundle SRBProps = null;
38
            // SRB properties that tells about the location of SRB RMI Server
39
            // srbProps.properties should reside on metacat server
40
            SRBProps = (PropertyResourceBundle)
41
                        PropertyResourceBundle.getBundle("edu.ucsb.nceas.metacat.srbProps");
42
            srbHost = (String)SRBProps.handleGetObject("host");
43
            srbPort = (String)SRBProps.handleGetObject("port");
44
            // should handle missing RMIhost name here
45
            String RMIhost = (String)SRBProps.handleGetObject("RMIhost");
46
            String name = "//" + RMIhost + "/SrbJavaGlue";
47
            srbJG = (SrbJavaGlueInterface)Naming.lookup(name);
48
        } catch (Exception e) {
49
            System.out.println("MetaCatSession static: " + e.getMessage());
50
        }    
51
    }    
52

    
53
    /** Construct a MetaCatSession
54
     *
55
     * @param request the request made from the client
56
     * @param username the username entered when login
57
     * @param password the password entered when login
58
     */
59
    public MetaCatSession (HttpServletRequest request, 
60
                            String username, String password) {
61
      
62
      // create a new HTTPSession object
63
      this.session = getSession(request, username, password);
64
    }
65

    
66

    
67

    
68
    /** Get new HttpSession and store username & password in it */
69
    private HttpSession getSession(HttpServletRequest request, 
70
                            String username, String password)  
71
                            throws IllegalStateException {
72
                                
73
      // get the current session object, create one if necessary
74
      HttpSession sess = request.getSession(true);
75
      // if it is still in use unvalidate and get a new one
76
      if ( !sess.isNew() ) {
77
        sess.invalidate();
78
        sess = request.getSession(true);
79
      }
80
      // store username & password in the session for later use if necessary
81
      sess.putValue("username", username);
82
      sess.putValue("password", password);
83
      
84
      return sess; 
85
    }
86

    
87
    /** Try to make user authentication through SRB RMI Connection */
88
    public boolean userAuth(String password)
89
                    throws RemoteException  { 
90
      int srbconn = 0;
91
      int err;
92
      
93
      // try SRB RMI Connection
94
      // integer value of the SBR Connection ID is returned only
95
      try {
96
        srbconn = srbJG.clConnectJ( srbHost, srbPort, password );
97
      } catch (RemoteException e) {
98
        throw new RemoteException("MetaCatSession.userAuth() - " +
99
                                  "Error on clConnectJ(): " + e.getMessage());
100
      }
101

    
102
      // check if successfull
103
      if ( srbconn < 0 ) {
104
        // srb connection is already finished from clConnectJ() routine
105
        return false;
106
        //throw new RemoteException("MetaCatSession.userAuth() failure: " +
107
        //                          "SRB Connection failed");
108
      }
109

    
110
      // we don't need that connection. close it.
111
      try {
112
        err = srbJG.clFinishJ( srbconn );
113
      } catch (RemoteException e) {
114
        throw new RemoteException("MetaCatSession.userAuth() - " +
115
                                  "Error on clFinishJ(): " + e.getMessage());
116
      }
117
      
118
      // store SRB Connection in the session for later use if necessary
119
      this.session.putValue("srbconnection", new Integer(srbconn));
120
      return true; 
121
    }
122

    
123
    /**
124
     * Invalidate this HTTPSession object. 
125
     * All objects stored in the session are unbound 
126
     */
127
    public void disconnect() {
128
        
129
        this.session.invalidate();
130
    }    
131

    
132
}
(19-19/24)