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.








Friday, December 16, 2011

opening PDF as popup in jsr 168 portlet,popup in jsr 168

Since JSR 168 has only support for html so we need a servlet to be called from the jsp.

Step1- Create a servlet and in doGet use this code

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession httpsession =request.getSession();
Map map = (Map)httpsession.getAttribute("resultMap");
if(map != null)
{
GeneratePDF.generatePDFReciept(map, response);
}
httpsession.removeAttribute("resultMap");
}


Here I am passing map to GeneratePDF method and in this method i am using itext API to generate PDF from the map values.

step2:-  use the following code in the process action and set attribute in the HttpSession.

                  HttpServletRequest httpServletRequest = (HttpServletRequest)request;
   while(httpServletRequest instanceof HttpServletRequestWrapper){
     HttpServletRequestWrapper httpServletRequestWrapper =
  (HttpServletRequestWrapper)httpServletRequest;
     System.out.println("HttpServletRequestWrapper " + httpServletRequestWrapper);
     httpServletRequest =  (HttpServletRequest)httpServletRequestWrapper.getRequest();
   }
 
    HttpSession httpsession = httpServletRequest.getSession();
 
    httpsession.setAttribute("resultMap", receiptmap);


3:-Now invoke the servlet as popup from the portlet jsp by simply  calling javascript on clicking the button or link, as follows:-

function servletCall()
{

 window.open("http://<%=renderRequest.getServerName()%>:<%=renderRequest.getServerPort()%><%=renderRequest.getContextPath()%>/PDFPopup","PrintWindow","toolbar=0 scrollbars=yes");

}

Now you can view your data in popup  :)

Tuesday, November 1, 2011

calling body onload javascript in portlet jsr 168.

Since we are not calling body tag in jsp in jsr 168 portlet so simply create a script tag and call function as :-

<script>
function checkEnter()
{
document.body.onkeypress = enterKey
}
checkEnter();

</script>

Simply add this in your jsp and in check enter function you can call your own functions as I am calling enterKey Functions,
Here I am checking for the enter key press :
<script>
function enterKey(evt) {  
  var evt = (evt) ? evt : event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if (charCode == 13) {
    setOperationName('POST','RECEIPT');
    return false;
  }
  return true;
}
</script>

Tuesday, October 18, 2011

personalization of theme ,showing group related links in a theme

Below code personalizes the theme , and you can show only the related links for groups, in this code i have created a url mapping for change password portlet and personalised it by fetching group name by PUMA api, u have to use this code in the banner_toolbar.jspf in ur theme.

Monday, June 27, 2011

PUMA Api code to reset password, Verify old password in websphere portal

When you are not using LDAP and you have to change user password with verifying old password the only option left is by using WAS Programatic Login.you can reset new password by Puma API and verify the old password by WAS Authentication, the below code is working fine for me.

PortletServiceHome psh = null;

try {
if (psh == null) {
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
}
} catch (NamingException e) {


e.printStackTrace();

}
HashMap userAttrs = new HashMap();

String oldPwd = request.getParameter("OLD_PASSWORD");
String pwd1 = request.getParameter("PASSWORD");
String pwd2 = request.getParameter("PASSWORD_CONFIRM");

try
{

PumaHome service=(PumaHome) psh.getPortletService(PumaHome.class);
PumaLocator pLocator=service.getLocator();
PumaController pc = service.getController();
com.ibm.wps.puma.User vuser = (com.ibm.wps.puma.User)request.getAttribute("com.ibm.portal.puma.request-user");
String userid = vuser.getUserID();

if( pwd1 != null && oldPwd != null )
{

//verify password

LoginContext lc = null;
Subject subject = null;
System.out.println("old pwd::"+oldPwd);
try {
lc = new LoginContext("WSLogin",
new WSCallbackHandlerImpl(userid, oldPwd));

} catch (LoginException le) {
System.out.println("Cannot create LoginContext. " + le.getMessage());

} catch(SecurityException se) {

System.out.println("Security Exception. " + se.getMessage());
}

try {
lc.login();
subject = lc.getSubject();
System.out.println("loged in");
passwordVerification = true;
com.ibm.websphere.security.auth.WSSubject.setRunAsSubject(subject);
} catch(LoginException le) {
passwordVerification = false;
System.out.println("Fails to create Subject. " + le.getMessage());

}



if(passwordVerification)
{
userAttrs.put("uid", userid);
userAttrs.put("password", pwd1);

List userList = pLocator.findUsersByAttribute("uid", userid);
User editUser = (User)userList.get(0);
pc.setAttributes(editUser, userAttrs);

passwordChanged = true;

}

Thursday, June 9, 2011

WebSphere Portal Project Architecture,Portal Project Flow Diagram

This Diagram Shows a Portal project flow Diagram, Project flow can vary according to requirements.


Tuesday, June 7, 2011

Inter Portlet Communication in JSR -168 by RAD 7.5,Portlet Wiring.

1.First create a new portlet project or use existing one,The source and target portlet should be in same workspace.
2.Create Portlet IPC1 & IPC2.
3.Create Source and Target Portlets as shown below.


 Select The Data Type For Source Portlet, as shown below.




Similarly make a cooperative target portlet, as shown.



As for source portlet, select Data Type for target portlet.

Then in the source portlet JSP use the Cooperative Source Trigger to create an event on click,





After setting the event use
  if(actionValue != null && "TargetAction".equals(actionValue)) {
           
          String input = request.getParameter("inputProperty");
           
            session.setAttribute("input", input,session.PORTLET_SCOPE);
         }
in target portlet process action to fetch the value that you have set in the scope.

After that you can use bean or session to render the value from process action  to targer portlet jsp.

Then deploy the portlet, and add them on a page, Then edit page layout and do the WIRING between source and target portlets. as shown below.
 Now your Inter Portlet communication is done.you can put the value in the source portlet and after clicking submit value will be shown on target portlet.



On submitting the output will be:-



For any help or issue please contact me at sanjeetsinghmerhal@gmail.com.
Please follow me if material is useful for you.
Thanks