Thursday, October 15, 2009

How to use Httpsession for user data sharing in IBM WebSphere Portlet V6.0

1) Basic classes

package com.session;

import java.io.Serializable;
import java.util.Iterator;

public interface SharedSession {

public abstract void setAttribute(Serializable key, Serializable value);

public abstract Serializable getAttribute(Serializable key);

public abstract void removeAttribute(Serializable key);

public abstract Iterator iterator();


public abstract String getId();
}

-------------------------------------------------------------------------------
package com.session;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

public class SharedSessionFactory {
private static ThreadLocal tl = new ThreadLocal() {
protected Object initialValue() {
return null;
}
};

/**
* Used by filter, do not use directly
* @param ss
*/
public static void set(SharedSession ss) {
tl.set(ss);
}

public static SharedSession getSession() {
return (SharedSession)tl.get();
}

/**
*
* @param hs
* @return
* @deprecated Use getSession()
*/
public static SharedSession getSession(HttpSession hs) {
return getSession();
}

/**
* @param fc
* @return
* @deprecated Use getSession()
*/
public static SharedSession getSession(FacesContext fc) {
return getSession();
}


}

-------------------------------------------------------------------------

package com.session.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.session.SharedSession;
import com.session.SharedSessionFactory;
import com.session.impl.SimpleSharedSessionImpl;

public class SharedSessionFilter implements Filter {
private final static String SHARED_SESSION_LABEL = "SharedSessionFilter.SharedSession";

public void destroy() {
// nop
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

System.out.println("SharedSessionFilter.doFilter called");

if (request instanceof HttpServletRequest) {
HttpSession hs = ((HttpServletRequest)request).getSession(false);
if (hs != null) {
SharedSession ss = (SharedSession)hs.getAttribute(SHARED_SESSION_LABEL);
if (ss == null) {
// create new and attach to session
ss = new SimpleSharedSessionImpl(hs);
hs.setAttribute(SHARED_SESSION_LABEL, ss);
}
SharedSessionFactory.set(ss);
}
}
chain.doFilter(request, response);
SharedSessionFactory.set(null);
}

public void init(FilterConfig arg0) throws ServletException {
// nop
}

}

------------------------------------------------------------------------------------------

package com.session.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.session.SharedSession;

public class SimpleSharedSessionImpl implements SharedSession {

private Map attributes = null;
private String id = null;

public SimpleSharedSessionImpl(HttpSession hs) {
super();
attributes = new HashMap();
id = hs.getId();
}

/**
* @see nl.pv.mpb.session.SharedSession#setAttribute(java.io.Serializable, java.io.Serializable)
*/
public void setAttribute(Serializable key, Serializable value) {
attributes.put(key, value);
}

/**
* @see nl.pv.mpb.session.SharedSession#getAttribute(java.io.Serializable)
*/
public Serializable getAttribute(Serializable key) {
return (Serializable)attributes.get(key);
}

/**
* @see nl.pv.mpb.session.SharedSession#removeAttribute(java.io.Serializable)
*/
public void removeAttribute(Serializable key) {
attributes.remove(key);
}

/**
* @see nl.pv.mpb.session.SharedSession#destroy()
*/
public void destroy() {
attributes = null;
}

/**
* @see nl.pv.mpb.session.SharedSession#iterator()
*/
public Iterator iterator() {
return attributes.keySet().iterator();
}

/**
* @see nl.pv.mpb.session.SharedSession#getId()
*/
public String getId() {
return id;
}

public String toString() {
return id + ": " + attributes;
}

}

------------------------------------------------------------------------------------------
Location : C:\IBM\WebSphere\profiles\wp_profile\config\cells\win2k\applications\wps.ear\deployments\wps\wps.war\WEB-INF


sharedsession
com.session.filter.SharedSessionFilter



sharedsession
/portal/*


sharedsession
/myportal/*


-------------------------------------------------------------------------------------------------
Uses :

package com.ibm.testsharedsessionportletone.nl;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.session.SharedSession;
import com.session.SharedSessionFactory;
import com.ibm.faces.portlet.FacesPortlet;

public class TestPortletOne extends FacesPortlet {

public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("value set in session : TestPortletOne");
SharedSession session = SharedSessionFactory.getSession();
session.setAttribute("name", "harish");
super.doView(request, response);
}

public void processAction(ActionRequest arg0, ActionResponse arg1)
throws PortletException {
// TODO Auto-generated method stub
super.processAction(arg0, arg1);
}

}

----------------------------------------------------------------------------------------

package com.ibm.testsharedsessionportlettwo.nl;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.ibm.faces.portlet.FacesPortlet;
import com.session.SharedSession;
import com.session.SharedSessionFactory;

public class TestPortletTwo extends FacesPortlet {

public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {

SharedSession session = SharedSessionFactory.getSession();
session.getAttribute("name");

System.out.println("value get in session : TestPortletTwo :"+session.getAttribute("name"));

super.doView(request, response);
}

public void processAction(ActionRequest arg0, ActionResponse arg1)
throws PortletException {
// TODO Auto-generated method stub
super.processAction(arg0, arg1);
}

}

No comments:

Post a Comment

Followers