Filter Class: package com.ibm.impersonationwithfilter; import java.io.ioexception; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.portlet.portletexception; import javax.portlet.renderrequest; import javax.portlet.renderresponse; import javax.portlet.filter.filterchain; import javax.portlet.filter.filterconfig; import javax.portlet.filter.renderfilter; import com.ibm.portal.impersonation.exceptions.impersonationexception; import com.ibm.portal.portlet.service.portletservicehome; import com.ibm.portal.portlet.service.impersonation.impersonationservice; import com.ibm.portal.um.user; import com.ibm.portal.um.exceptions.pumaexception; import com.ibm.portal.um.portletservice.pumahome; public class ImpersonationFilter implements RenderFilter { private boolean userimpersonated = false;
private User actualuser; private User originaluser; private String auser; private String ouser; PortletServiceHome pshimpersonate; PumaHome pumahome; public void destroy() { public void init(filterconfig filterconfig) throws PortletException { public void dofilter(renderrequest request, RenderResponse response, FilterChain filterchain) throws IOException, PortletException { try { Context ctx = new InitialContext(); pshimpersonate = (PortletServiceHome) ctx.lookup(impersonationservice.jndi_name); PortletServiceHome pshome; pshome = (PortletServiceHome) ctx.lookup(pumahome.jndi_name); pumahome = (PumaHome) pshome.getportletservice(pumahome.class);
catch (NamingException e) { // TODO Auto-generated catch block e.printstacktrace(); try { actualuser = pumahome.getprofile(request).getcurrentuser(); int temp = actualuser.tostring().indexof(","); auser = actualuser.tostring().substring(4, temp); catch (PumaException e1) { // TODO Auto-generated catch block e1.printstacktrace(); com.ibm.portal.portlet.service.portletservicehome psh = null; ImpersonationService impersonationservice; javax.naming.context ctx = null; try { ctx = new javax.naming.initialcontext(); catch (NamingException e) { // TODO Auto-generated catch block e.printstacktrace();
try { psh = (com.ibm.portal.portlet.service.portletservicehome) ctx.lookup(impersonationservice.jndi_name); if (psh!= null) { impersonationservice = (ImpersonationService) psh.getportletservice(impersonationservice.class); if (impersonationservice!= null) { userimpersonated = impersonationservice.isuserimpersonated(); originaluser = impersonationservice.getoriginaluser(); int temp = actualuser.tostring().indexof(","); ouser = originaluser.tostring().substring(4, temp); catch (javax.naming.namingexception ne) { // impersonation is not present catch (com.ibm.portal.portlet.service.portletserviceunavailableexception psue) { // impersonation is not present catch (ImpersonationException e) { response.setcontenttype("text/html"); if (userimpersonated) { response.getwriter().print("<b>you are in impersonation mode! <br><br>this portlet contains some of the sensitive user information which is not displayed</b><br>");
response.getwriter().print("the impersonated user is = " + auser); response.getwriter().print("<br>"); response.getwriter().print("the original user is = " + ouser); response.getwriter().print("<br><h3 style='margin-bottom: 3px'>Welcome!</H3>"); Details</H3>"); response.getwriter().print("<h3 style='margin-bottom: 3px'>Profile response.getwriter().print("<b>name</b> : " + auser); else { response.getwriter().print("<br><h3 style='margin-bottom: 3px'>Welcome!</H3>"); response.getwriter().print("<h3 style='margin-bottom: 3px'>Profile Details</H3><DIV style='margin: 12px; margin-bottom: 36px'>"); response.getwriter().print("<b>name</b> : " + auser); filterchain.dofilter(request, response); Portlet class package com.ibm.impersonationwithfilter; import java.io.ioexception;
import javax.portlet.actionrequest; import javax.portlet.actionresponse; import javax.portlet.genericportlet; import javax.portlet.portletexception; import javax.portlet.portletrequest; import javax.portlet.portletrequestdispatcher; import javax.portlet.portletsession; import javax.portlet.renderrequest; import javax.portlet.renderresponse; A sample portlet based on GenericPortlet public class ImpersonationWithFilterPortlet extends GenericPortlet { public static final String JSP_FOLDER = "/_ImpersonationWithFilter/jsp/"; // JSP folder name public static final String VIEW_JSP = "ImpersonationWithFilterPortletView"; // JSP file name to be rendered on the view mode public static final String SESSION_BEAN = "ImpersonationWithFilterPortletSessionBean"; // Bean name for the portlet session public static final String FORM_SUBMIT = "ImpersonationWithFilterPortletFormSubmit"; // Action name for submit form public static final String FORM_TEXT = "ImpersonationWithFilterPortletFormText"; // Parameter name for the text input
@see javax.portlet.portlet#init() public void init() throws PortletException{ super.init(); Serve up the <code>view</code> mode. @see javax.portlet.genericportlet#doview(javax.portlet.renderrequest, javax.portlet.renderresponse) public void doview(renderrequest request, RenderResponse response) throws PortletException, IOException { // Set the MIME type for the render response response.setcontenttype(request.getresponsecontenttype()); // Check if portlet session exists ImpersonationWithFilterPortletSessionBean sessionbean = getsessionbean(request); if( sessionbean==null ) { response.getwriter().println("<b>no PORTLET SESSION YET</b>"); return; // Invoke the JSP to render
PortletRequestDispatcher rd = getportletcontext().getrequestdispatcher(getjspfilepath(request, VIEW_JSP)); rd.include(request,response); Process an action request. @see javax.portlet.portlet#processaction(javax.portlet.actionrequest, javax.portlet.actionresponse) public void processaction(actionrequest request, ActionResponse response) throws PortletException, java.io.ioexception { if( request.getparameter(form_submit)!= null ) { // Set form text in the session bean ImpersonationWithFilterPortletSessionBean sessionbean = getsessionbean(request); if( sessionbean!= null ) sessionbean.setformtext(request.getparameter(form_text)); Get SessionBean. @param request PortletRequest @return ImpersonationWithFilterPortletSessionBean
private static ImpersonationWithFilterPortletSessionBean getsessionbean(portletrequest request) { PortletSession session = request.getportletsession(); if( session == null ) return null; ImpersonationWithFilterPortletSessionBean sessionbean = (ImpersonationWithFilterPortletSessionBean)session.getAttribute(SESSION_BEAN); if( sessionbean == null ) { sessionbean = new ImpersonationWithFilterPortletSessionBean(); session.setattribute(session_bean,sessionbean); return sessionbean; Returns JSP file path. @param request Render request @param jspfile JSP file name @return JSP file path private static String getjspfilepath(renderrequest request, String jspfile) { String markup = request.getproperty("wps.markup"); if( markup == null ) markup = getmarkup(request.getresponsecontenttype());
return JSP_FOLDER + markup + "/" + jspfile + "." + getjspextension(markup); Convert MIME type to markup name. @param contenttype MIME type @return Markup name private static String getmarkup(string contenttype) { if( "text/vnd.wap.wml".equals(contenttype) ) return "wml"; else return "html"; Returns the file extension for the JSP file @param markupname Markup name @return JSP extension private static String getjspextension(string markupname) { return "jsp";
ImpersonationWithFilterPortletSessionBean package com.ibm.impersonationwithfilter; A sample Java bean that stores portlet instance data in portlet session. public class ImpersonationWithFilterPortletSessionBean { Last text for the text form private String formtext = ""; Set last text for the text form. @param formtext last text for the text form. public void setformtext(string formtext) { this.formtext = formtext; Get last text for the text form. @return last text for the text form public String getformtext() { return this.formtext; Exception class package com.ibm.portal.impersonation.exceptions; public class ImpersonationException extends Throwable{ private static final long serialversionuid = 1L; Resource bundle
# en Resource Bundle # # filename: ImpersonationWithFilterPortletResource_en.properties # Portlet Info resource bundle example javax.portlet.title=impersonationwithfilter javax.portlet.short-title=impersonationwithfilter javax.portlet.keywords=impersonationwithfilter jsp <%@page session="false" contenttype="text/html" pageencoding="iso-8859-1" import="java.util.,javax.portlet.,com.ibm.impersonationwithfilter." %> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> <%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portletclient-model" prefix="portlet-client-model" %> <portlet:defineobjects/> <portlet-client-model:init> <portlet-client-model:require module="ibm.portal.xml."/> <portlet-client-model:require module="ibm.portal.portlet."/> </portlet-client-model:init> <% com.ibm.impersonationwithfilter.impersonationwithfilterportletsessionbe an sessionbean = (com.ibm.impersonationwithfilter.impersonationwithfilterportletsessionbean)re nderrequest.getportletsession().getattribute(com.ibm.impersonationwithfilter. ImpersonationWithFilterPortlet.SESSION_BEAN); %> <DIV style="margin: 6px"> <img src='<%=renderresponse.encodeurl(renderrequest.getcontextpath() + "/_ImpersonationWithFilter/jsp/html/monkey.jpg")%>' alt="monkey" /> <DIV style="margin: 12px; margin-bottom: 36px"> <b>ssn</b> : 898-14-8326 <br> <b>date of Birth</b> : Jul 4, 1910 </DIV> </DIV>