Sample Remember Me cookie Portlet

This sample Portlet demonstrate how to use Remember Me cookie service to find out userId for the user on anonymous page.


public class RememberMePortlet extends javax.portlet.GenericPortlet {
PortletServiceHome psh;
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome)ctx.lookup(RememberMeCookieService.JNDI_NAME);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());

try {
RememberMeCookieService rememberMeService = psh.getPortletService(RememberMeCookieService.class);
if(rememberMeService.isRememberMeCookieEnabled()){
System.out.println("RememberMeCookie is enabled");
System.out.println("User Name " + rememberMeService.getUserID(request));
System.out.println("Is Remember Me Cookie Set " + rememberMeService.isCookieSet(request));
response.getWriter().println("Invalidate remember me cookie");
}else{
System.out.println("RememberMeCookie is disabled");
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
}


You can download this portlet from here

No comments: