Wednesday, February 15, 2012

Logging to the portal directly from the url, Passing parameter to portlet directly from url..

If you want to login to the portal directly from the url and want to pass some parameter directly to the portlet from the url follow the below steps:-

Step1:-create custom login portlet and in the portlet get the username and password and the argument that u want to pass, Here I am considering parameter as .param1.param2,param3.

the code is as below :-

package com.ibm.customloginportlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.log4j.Logger;
import com.ibm.portal.auth.exceptions.AuthenticationException;
import com.ibm.portal.auth.exceptions.AuthenticationFailedException;
import com.ibm.portal.auth.exceptions.PasswordInvalidException;
import com.ibm.portal.auth.exceptions.PortletLoginDisabledException;
import com.ibm.portal.auth.exceptions.SessionTimeOutException;
import com.ibm.portal.auth.exceptions.SystemLoginException;
import com.ibm.portal.auth.exceptions.UserAlreadyLoggedInException;
import com.ibm.portal.auth.exceptions.UserIDInvalidException;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.portlet.service.login.LoginHome;
import com.ibm.portal.portlet.service.login.LoginService;
import com.ibm.websphere.security.WSSecurityException;
import com.ipops.vo.CRMRequestDataVo;


/**
 * Custom Login Portlet
 */
public class CustomLoginPortlet extends GenericPortlet {

private static final String  VIEW_JSP       = "/jsp/CustomLoginPortletView.jsp";
private static final String  VIEW_JSP_FAILED     = "/jsp/LoginFailed.jsp";
private static Logger logger = Logger.getLogger (CustomLoginPortlet.class);
LoginHome loginHome;
public void init() throws PortletException{
  try
     {
          InitialContext ctx =  new InitialContext();
          PortletServiceHome psh = (PortletServiceHome) ctx.lookup(LoginHome.JNDI_NAME);
          loginHome = (LoginHome) psh.getPortletService(LoginHome.class);
          }
     catch (NamingException e)
     {
         e.printStackTrace();
         }
     catch (PortletServiceUnavailableException e)
     {
     logger.error("PortletServiceUnavailableException",e);
 }
       }


public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
     {
  response.setContentType("text/html");
  CRMRequestDataVo  vo =getRequestData(request);
  PortletSession session = request.getPortletSession();
  PortletRequestDispatcher dispatcher = null;
  String pageType = (String)session.getAttribute("pageType");
  if(pageType != null && pageType.equalsIgnoreCase("failed"))
  {
dispatcher = getPortletConfig().getPortletContext().getRequestDispatcher(VIEW_JSP_FAILED);
  }
  else
  {
request.setAttribute("CRMData", vo);
dispatcher = getPortletConfig().getPortletContext().getRequestDispatcher(VIEW_JSP);
 
  }
  dispatcher.include(request, response);
  session.removeAttribute("pageType");
    }


public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException
   {
 PortletSession session = request.getPortletSession();
try {
     String userId = request.getParameter("param1");
     String password = request.getParameter("param2");
     LoginService loginService = loginHome.getLoginService(request, response);
 Map contextMap = new HashMap();
 contextMap.put(LoginService.DO_RESUME_SESSION_KEY, new Boolean(false));
 loginService.login(userId, password.toCharArray(), contextMap, null);
 response.sendRedirect("/wps/myportal/orderDisplay?orderID="+request.getParameter("param3"));
 
   }
     catch (PasswordInvalidException e)
     {
     logger.error("PasswordInvalidException:",e);
     }
     catch (UserIDInvalidException e)
     {
     logger.error("UserIDInvalidException:",e);
     }
     catch (AuthenticationFailedException e)
     {
     logger.error("AuthenticationFailedException:",e);
     }
     catch (AuthenticationException e)
     {
     logger.error("AuthenticationException:",e);
     }
     catch (SessionTimeOutException e)
     {
     logger.error("SessionTimeOutException:",e);
      }
     catch (PortletLoginDisabledException e)
     {
     logger.error("PortletLoginDisabledException:",e);
     }
     catch (UserAlreadyLoggedInException e)
     {
     logger.error("UserAlreadyLoggedInException:",e);
      }
     catch (SystemLoginException e)
     {
     logger.error("UserAlreadyLoggedInException:",e);
     }
     catch (LoginException e)
     {
     session.setAttribute("pageType","failed");
     logger.error("LoginException:",e);
     }
     catch (WSSecurityException e)
     {
     logger.error("WSSecurityException:",e);
     }
     catch (com.ibm.portal.auth.exceptions.LoginException e)
     {
     logger.error("com.ibm.portal.auth.exceptions.LoginException:",e);
     }
     catch (Exception e)
     {
     logger.error("Exception:",e);
 response.sendRedirect("/wps/portal/");
     }
 }

private CRMRequestDataVo getRequestData(PortletRequest request){

   CRMRequestDataVo  vo = null;
   HttpServletRequest httpServletRequest = (HttpServletRequest)request;
   while(httpServletRequest instanceof HttpServletRequestWrapper)
   {
     HttpServletRequestWrapper httpServletRequestWrapper = (HttpServletRequestWrapper)httpServletRequest;
     httpServletRequest =  (HttpServletRequest)httpServletRequestWrapper.getRequest();
   
   }

   if(httpServletRequest.getParameter("param1") != null && httpServletRequest.getParameter("param2") != null && httpServletRequest.getParameter("param3") != null  )
     {
     vo = new CRMRequestDataVo();
     vo.setUserName(httpServletRequest.getParameter("param1"));
     vo.setPassword(httpServletRequest.getParameter("param2"));
     vo.setOrderID(httpServletRequest.getParameter("param3"));
     }else
     {
     PortletSession psession = request.getPortletSession();
     psession.setAttribute("pageType","failed");
     }
     return vo;
 
 }


}


In the above code I am using 2 jsps, if the vo is not null then i am passing vo to CustomLoginPortletView.jsp.

The jsp code is :-
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@page import="com.ipops.vo.*" %>
<portlet:defineObjects />

<form method="post" name="AutoLogin" action="<portlet:actionURL/>">
<%CRMRequestDataVo vo = (CRMRequestDataVo)renderRequest.getAttribute("CRMData"); %>

<input type = "hidden" name="param1" value="<%=vo.getUserName()%>">
<input type = "hidden" name="param2" value="<%=vo.getPassword()%>">
<input type = "hidden" name="param3" value="<%=vo.getOrderID()%>">

</form>

<script language="javaScript" type="text/javascript" >
function autoSubmit()
{

document.AutoLogin.submit();
}
autoSubmit();
</script>

after getting the parameter from the processAction we are redirecting the URL.

Step2:-create a URL Mapping and map it to the portlet where you need the inputs, In my case i am passing param3 as input param.

Step3:- Now logout from the portal and simply put this in the URL:
http://localhost:10040/wps/portal/CRMLogin?param1=wpsadmin&param2=wpsadmin&param3=981139

you will get loged in to the portal and in the portlet you can fetch the param3 and can do further action as per req.








No comments:

Post a Comment