Accessing underlying HttpServletRequest from PortletRequest in WebSphere Portal Server

I wanted to figure out if i can access HttpServletRequest object that was passed to the portal server inside the Portlet, and it seems that i can do that using a method like this.

Note:This code is tested only in WebSphere Portal and i am not sure if it will work in the same way in any other standard compliant Portlet Container.


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;
}


The getHttpServletRequest() method takes PortletRequest method and first casts it into the HttpServletRequest object and then checks if its object of HttpServletRequestWrapper type if yes tries to get the wrapped object by calling its getRequest() method, it does this iteratively until it finds the HttpServletRequest object that is not instanceof HttpServletRequestWrapper


[5/9/10 15:48:20:960 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.ws.portletcontainer.core.impl.RenderRequestImpl@302a302a wrapping com.ibm.wps.engine.PortalRequestWrapper@4be84be8 wrapping com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.wps.engine.PortalRequestWrapper@4be84be8 wrapping com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>


This is output that i see when i call getHttpServletRequest() method from the doView() method of my sample portlet

3 comments:

  1. hey

    thanks. very nice post.it helped me a lot.thanks again

    ReplyDelete
  2. See my article "Passing query parameters to JSR-286 portlets" here.

    I know this is a supported way to do it. The solution presented here depends on a particular implementation which may change in the future, i.e. no guarantees.

    ReplyDelete