HTTP Session Management

This example demonstrates the HTTP Session Management capabilities of Novell exteNd WSSDK. For the purpose of the example, a Document Management System (DMS) is used. DMS allows different users to manage their documents. Each user can login into the account and then store or retrieve a document.

1 DocumentManager Interface

The DocumentManager interface defines the methods for basic document management operations like store, delete, retrieve, etc.

package dms;
                                                                           
public interface DocumentManager extends java.rmi.Remote {
    void storeDocument(byte[] bytes, String fileName) 
    throws java.rmi.RemoteException;
    String[] listDocuments() throws java.rmi.RemoteException;
    byte[] retrieveDocument(String fileName) 
    throws java.rmi.RemoteException;
    void deleteDocument(String fileName) 
    throws java.rmi.RemoteException;
    void login(String userID, String password) 
    throws java.rmi.RemoteException;
}

2 Sample Client

The client takes part in session by setting the SESSION_MAINTAIN property on the stub. It logs into the account, lists the document stored, retrieves the document and then deletes the document.

package dms;
                                                                           
import java.io.*;
                                                                           
import javax.xml.rpc.Stub;
import javax.naming.InitialContext;
                                                                           
public class Client {
    private static String _userID = "docmgt-user";
    private static String _pswd   = "docmgt-pswd";
    //
    private DocumentManager _docMgr = null;
    //
    public static void main(String[] args) throws Exception {
    |   Client client = new Client();
    |   client.run(args);
    }
                                                                           
    private void run(String[] args) throws Exception {
    |                                                                      
    |   //get the remote object 
    |   _docMgr = getDocumentManager(args);
    |                                                                      
    |   //login into user's account
    |   _docMgr.login(_userID, _pswd);
    |                                                                      
    |   String[] files  = null;
    |                                                                      
    |   //list the documents for user
    |   files  = _docMgr.listDocuments();       
    |   listDocuments(files);
    |                                                                      
    |   //get bytes of the file to send
    |   String fileName = "LICENSE.html";
    |   byte[] bytes = getBytes(fileName);
    |                                                                      
    |   //store the document
    |   _docMgr.storeDocument(bytes, fileName);
    |                                                                      
    |   //list the documents for user
    |   files  = _docMgr.listDocuments();       
    |   listDocuments(files);
    |                                                                      
    |   //retrieve the document
    |   bytes = _docMgr.retrieveDocument(fileName);     
    |                                                                      
    |   //save document as file
    |   try {
    |   |   FileOutputStream fos = new FileOutputStream("lic.html");
    |   |   fos.write(bytes);
    |   |   fos.close();
    |   } catch(Throwable e) {
    |   |   e.printStackTrace();
    |   }
    |                                                                      
    |   //delete document
    |   _docMgr.deleteDocument(fileName);
    |                                                                      
    |   //list the documents for user
    |   files  = _docMgr.listDocuments();       
    |   listDocuments(files);
    |                                                                      
    }
                                                                           
    /**Get the proxy for web service*/
    private static DocumentManager getDocumentManager(String[] args) 
    throws Exception 
    {
    |   // get the initial context
    |   InitialContext ctx = new InitialContext();
    |                                                                      
    |   // lookup the service
    |   DocumentManagerService svc = (DocumentManagerService)
    |       ctx.lookup("xmlrpc:soap:dms.DocumentManagerService");
    |                                                                      
    |   //get the stub for port
    |   DocumentManager docMgr = svc.getDocumentManagerPort();
    |                                                                      
    |   // set the end point address
    |   ((Stub)docMgr)._setProperty(
    |       javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, 
    |       args.length > 0 ? args[0] :"http://localhost:9090/dms");
    |                                                                      
    |   //turn on the session with server
    |   //Please note that the client has to explicitly ask to 
    |   //take part in the session with the server.
    |   ((Stub)docMgr)._setProperty(
    |       javax.xml.rpc.Stub.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);
    |                                                                      
    |   //return remote interface
    |   return docMgr;
    |                                                                      
    }
                                                                           
    /**Print the file names to the console*/
    private void listDocuments(String[] files) throws Exception {
    |   if (files ==null || files.length == 0) {
    |   |   System.out.println("No document found!");
    |   } else {
    |   |   for(int i=0; i < files.length; i++) 
    |   |       System.out.println(files[i]);
    |   }
    }
                                                                           
    /**Get the bytes from a file*/
    static byte[] getBytes(String fileName) throws Exception {
    |   FileInputStream fis = new FileInputStream(fileName);
    |   ByteArrayOutputStream baos = new ByteArrayOutputStream();
    |   int c;
    |   while((c = fis.read()) != -1) baos.write(c);
    |   return baos.toByteArray();
    }
}

3 Server Implementation

The server allows the users to login, hence it is session aware. As can be noted, the server accesses the HttpSession through the javax.xml.rpc.server.ServletEndpointContext implementation by calling the getEndpointContext method. It stores the login information in the session and checks if the user is logged in every time client invokes a document management operation.

When deploying the DMS web service, please note that you need a proper Web Application Container, which supports session management. The jwebserv mini container that ship with Novell exteNd WSSDK does not support session management and you will not be able to deploy this example into jwebserv.

package dms;
                                                                           
import java.io.*;
import java.util.*;
                                                                           
import javax.servlet.http.*;
                                                                           
                                                                           
public class Server extends DocumentManager_ServiceSkeleton {
                                                                           
    private final String _loggedin = "user_logged_in";
                                                                           
    /**Check if user is logged in. If not logged in throw Error*/
    private void isLoggedIn() {
    |   HttpSession session = getSession();
    |   Object login = session.getAttribute(_loggedin);
    |   if (login == null)
    |       throw new Error("Please login first!");
    }
                                                                           
    /**Get the HTTP session*/
    private HttpSession getSession() {
    |   return getEndpointContext().getHttpSession();
    }
                                                                           
    private final String _uid_prop = "uid";
                                                                           
    /**Get the userID of the user of the session*/
    private String getUserID() {
    |   return (String) getSession().getAttribute(_uid_prop);
    }
                                                                           
    /**Store the bytes into a file*/
    public void storeDocument(byte[] bytes, String docName) 
    throws java.rmi.RemoteException 
    {
    |   try {
    |   |   //check if the user is logged in
    |   |   isLoggedIn();
    |   |                                                                  
    |   |   //get the user id of the logged in user
    |   |   String userID = getUserID();
    |   |                                                                  
    |   |   //Get the file name for the document to be stored
    |   |   String fileName = getFileName(docName, userID);
    |   |                                                                  
    |   |   //write the bytes to the file
    |   |   FileOutputStream fos = new FileOutputStream(fileName);
    |   |   fos.write(bytes);
    |   |   fos.close();
    |   } catch(IOException ioe) {
    |   |   ioe.printStackTrace();
    |   |   throw new Error("error storing file:"+ioe.getMessage());
    |   }
    }
                                                                           
    /**List the document for a user*/
    public String[] listDocuments() throws java.rmi.RemoteException
    {
    |   isLoggedIn();
    |   String userID = getUserID();
    |                                                                      
    |   //Get the directory name where user's documents are stored.
    |   String dirName = getUserDirectory(userID);
    |   File dir = new File(dirName);
    |                                                                      
    |   //Get the file names of all the files in the directory.
    |   File[] files = dir.listFiles();
    |   String[] fileNames = new String[files.length];
    |   for (int i =0; i < files.length; i++)
    |       fileNames[i] = files[i].getName();
    |   return fileNames;
    }
                                                                           
    /**Retrieve the document and return the bytes from it*/
    public byte[] retrieveDocument(String docName) 
    throws java.rmi.RemoteException
    {
    |   String fileName = null;
    |   try {
    |   |   isLoggedIn();
    |   |   String userID = getUserID();
    |   |                                                                  
    |   |   //Get the file name of the document
    |   |   fileName = getFileName(docName, userID);        
    |   |                                                                  
    |   |   //Read the file and return the bytes read
    |   |   FileInputStream fis = new FileInputStream(fileName);
    |   |   ByteArrayOutputStream baos = new ByteArrayOutputStream();
    |   |   int c;
    |   |   while((c = fis.read()) != -1) baos.write(c);
    |   |   byte[] bytes = baos.toByteArray();
    |   |   fis.close();
    |   |   return bytes;
    |   } catch(IOException ioe) {
    |   |   ioe.printStackTrace();
    |   |   throw new Error("error reading file "+fileName+":"+ 
    |   |       ioe.getMessage());
    |   }
    }
                                                                           
    /**Delete the document*/
    public void deleteDocument(String docName) 
    throws java.rmi.RemoteException
    {
    |   String fileName = null;
    |   isLoggedIn();
    |   String userID = getUserID();
    |   //
    |   fileName = getFileName(docName, userID);
    |   File file = new File(fileName);
    |   file.delete();
    }
                                                                           
    /**Login into the user account*/
    public void login(String userID, String password) 
    throws java.rmi.RemoteException
    {
    |   //get the password of the user
    |   String pswd = _userProfiles.getProperty(userID+".password");
    |                                                                      
    |   //check if password matches
    |   if (!pswd.equals(password)) 
    |       throw new Error("password did not match");
    |                                                                      
    |   //get the HTTP session
    |   HttpSession session = getSession();
    |                                                                      
    |   //store the userID in the session
    |   session.setAttribute(_loggedin, userID);
    |   session.setAttribute(_uid_prop, userID);
    }
                                                                           
                                                                           
                                                                           
    /**Get the file name of the document for a user*/
    private String getFileName(String name, String userID) {
    |   //Get the user directory name 
    |   String fileName = getUserDirectory(userID);
    |   //return the document file name
    |   return fileName+File.separator+name;
    }
                                                                           
    /**Get the user directory name*/
    private String getUserDirectory(String userID) {
    |   String fileName = _dbPath+File.separator+userID;
    |   File file = new File(fileName);
    |   if (!file.exists()) file.mkdir();
    |   return fileName;
    }
                                                                           
    /**name of the directory where user directories exists*/
    private final static String _dbPath;
    static {
    |   String dbPath = System.getProperty("dbpath");
    |                                                                      
    |   //if not specified then user the pwd
    |   if (dbPath == null) dbPath = ".";
    |   _dbPath = dbPath;
    }
                                                                           
    private static Properties _userProfiles = new Properties();
                                                                           
    static {
    |   try {
    |   |   //read the properties file which contains information
    |   |   //about users. It has 3 properties for each user.
    |   |   //.password, .firstName and .lastName
    |   |   //where  is the userID of the user.
    |   |   InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("users.properties");
    |   |   _userProfiles.load(is);
    |   } catch(Throwable e) {
    |   |   e.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc. All rights reserved.