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-27 15:43:58 -0700 (Tue, 27 Jun 2000) $'
10
 * '$Revision: 212 $'
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.io.FileInputStream;
19
import java.rmi.*;
20
import SrbJavaGlueInterface;
21

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

    
29
    HttpSession session = null;
30
    // JNI (Java Native Interface) routines for SRB
31
    static String srbHost;
32
    static String srbPort;
33
    static SrbJavaGlueInterface srbJG;
34
    static {
35
        try {
36
            Properties SRBProps = new Properties();
37
            // SRB properties that tells about the location of SRB RMI Server
38
            // srbProps.properties should reside on metacat server
39
            FileInputStream in = new FileInputStream("srbProps.properties");
40
            SRBProps.load(in);
41
            in.close();
42
            srbHost = (String)SRBProps.get("host");
43
            srbPort = (String)SRBProps.get("port");
44
            // should handle missing RMIhost name here
45
            String RMIhost = (String)SRBProps.get("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
                                
72
      // get the current session object, create one if necessary
73
      HttpSession sess = request.getSession(true);
74
      // if it is still in use unvalidate and get a new one
75
      if ( !sess.isNew() ) {
76
        sess.invalidate();
77
        sess = request.getSession(true);
78
      }
79
      // store username & password in the session for later use if necessary
80
      sess.putValue("username", username);
81
      sess.putValue("password", password);
82
      
83
      return sess; 
84
    }
85

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

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

    
109
      // we don't need that connection. close it.
110
      try {
111
        err = srbJG.clFinishJ( srbconn );
112
      } catch (RemoteException e) {
113
        throw new RemoteException("MetaCatSession.userAuth() - " +
114
                                  "Error on clFinishJ(): " + e.getMessage());
115
      }
116
      
117
      // store SRB Connection in the session for later use if necessary
118
      this.session.putValue("srbconnection", new Integer(srbconn));
119
      return true; 
120
    }
121
    
122
    /**
123
     * Invalidate this HTTPSession object. 
124
     * All objects stored in the session are unbound 
125
     */
126
    public void disconnect() {
127
        
128
        this.session.invalidate();
129
    }    
130

    
131
}
(18-18/21)