Thursday, October 18, 2012

Getting HttpServletRequest and HttpServletResponse from portlet request



How to get HttpServletRequest and HttpServletResponse from portlet request
We can only have portletRequest in the portlet. If we are setting anything in the HttpServletRequest it is not accessible in the portlet. IBM provides an utility class which gives which converts the “portletRequest” to HttpServletRequest.

There are some scenarios when you want to get httpServletRequest or httpServletReponse from portletRequest.
PortletUtil class available in the com.ibm.ws.portletcontainer.portlet package in “com.ibm.ws.portletcontainer_6.1.0.jar” at “WebSphere/AppServer/plugins/” on server, through which you can directly get httpServletRequest, httpServletResponse, ServletContext and many more.

Code to get HttpServletRequest from portletRequest/renderRequest/actionRequest and HttpServletResponse from portletResponse/renderResponse/actionResponse:

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//from wp.engine.impl.jar
import com.ibm.wps.engine.ExtendedLocaleRequest;
//from wp.engine.impl.jar
import com.ibm.wps.engine.PortalRequestWrapper;
// from WAS_HOME\plugins\com.ibm.ws.portletcontainer_6.1.0.jar
import com.ibm.ws.portletcontainer.portlet.PortletUtils;

public class RequestRevealer {
public static HttpServletResponse getResponse (PortletResponse  portletResponse) {
        HttpServletResponse httpServletResponse;
            If (portletResponse  instanceof ActionResponse) {
            httpServletResponse = PortletUtils.getHttpServletResponse((ActionResponse)                                                                portletResponse);
            } else if (portletResponse  instanceof RenderResponse) {
            httpServletResponse = PortletUtils.getHttpServletResponse((RenderResponse)                                                                            portletResponse);
            } else {
            throw new RuntimeException ("Wrong type...:"+ portletResponse);
            }
            return httpServletResponse;
}
    public static HttpServletRequest getRequest (PortletRequest  portletRequest) {
            HttpServletRequest  httpServletRequest;
            If (portletRequest instanceof ActionRequest) {
httpServletRequest =                                               PortletUtils.getHttpServletRequest((ActionRequest)portletRequest);
            } else if (portletRequest instanceof RenderRequest) {
            httpServletRequest = PortletUtils.getHttpServletRequest((RenderRequest)                                                                        portletRequest);
            } else {
            throw new RuntimeException ("Wrong type...:"+ portletRequest);
            }
        PortalRequestWrapper  portalRequestWrapper = (PortalRequestWrapper)                                                                                                               httpServletRequest;
ExtendedLocaleRequest  extendedLocaleRequest =
             (ExtendedLocaleRequest) portalRequestWrapper.getRequest();
HttpServletRequest  response = (HttpServletRequest)                                                                                                                                     extendedLocaleRequest.getRequest();
            return response;
                        }
            }
There are couple of utility methods available in PortletUtil which gives you ServletContext, ServletConfig, PortletWindow, PortletApplicationDefinition etc.

Tuesday, October 16, 2012

Creating Shared Library in WebSphere Application Server



Creating a Shared library:

One of the most common way of sharing the files among the multiple applications on a websphere server is Shared Library. The following are the steps involved in creating shared library and registering to a server. In portal world, all the common files pertaining to theme, portlets and third party application are stored in shared library. Before the eventing in JSR 286, the typical implementation of eventing was using shared library or dynacache, where a hashmap is created to save the values that are need to shared across portlets.

 Creating shared library:

 1. Login to Websphere Application server console
 2. Navigate to Environment -> Shared Libraries
3. Provide a specific name to you shared libraries in name text box.
4. Provide the path of the libraries where you have stored all your jars in Classpath like: /tmp/myApplicationjars
 5. Click apply and save it

Assigning to a server:

 1. Go to servers -> Applicaiton servers -> websphere portal -> Java and Process management  -> classloader
 2. Click Shared library references
 3. Add the library you created in above stepts.
 4. Save and restart the portal

 The reason we want to restart the server is the class loader can then load all the newly added class files. Now, you can add the shared jars to build path for proper compilation and deployment.

Sunday, October 14, 2012

Getting Query String of portal page URL from inside the portlet



Getting Query String of portal page URL from inside the portlet:
Below article will explain how we can get query string of portal page url  inside a portlet .
To achieve this we can create a QueryStringPortlet portlet like this.
public class QueryStringPortlet extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
            // Set the MIME type for the render response
            response.setContentType(request.getResponseContentType());
            System.out.println("Start Point of QueryStringPortlet.doView()");
            HttpServletRequest httpServletRequest = getHttpServletRequest(request);
            String queryString = httpServletRequest.getQueryString();
            System.out.println("Value of Query String =" + queryString);
            request.setAttribute("queryString",queryString);
            // Invoke the JSP to render
            PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher                           (getJspFilePath(request, VIEW_JSP));
            rd.include(request,response);
            }
Accessing underlying HttpServletRequest from PortletRequest in WebSphere Portal Server
private HttpServletRequest getHttpServletRequest(PortletRequest request){
            HttpServletRequest httpServletRequest = (HttpServletRequest)request;
            while(httpServletRequest instanceof HttpServletRequestWrapper){
                        HttpServletRequestWrapper httpServletRequestWrapper =
                                    (HttpServletRequestWrapper)httpServletRequest;
System.out.println("HttpServletRequestWrapper: " + httpServletRequestWrapper);
httpServletRequest =  (HttpServletRequest)httpServletRequestWrapper.getRequest();                 }
return httpServletRequest;
            }

In  doView() method use getHttpServletRequest() method to get underlying HttpServletRequest object from the PortletRequest and then calls its httpServletRequest.getQueryString() method to get the query String. Once we have the query string we can set it in the request object and forwarding control to JSP for rendering output.


The jsp code should be like below:

<h2>Query String url ======  <%=renderRequest.getAttribute("queryString") %></h2>

Deployment and configuration of this portlet.
Below steps to be performed to deploy and configure this portlet through admin console. 

1.      Deploy this portlet 
2.      Create a page like queryString 
3.      Add this QueryStringPortlet on the page queryString 
4.      Create a url mapping for this page as queryString 
5.      Pass some query strign like queryString?value=Kameshwar 
6.      The query string will be printed on the portlet like below screen