Friday, 16 March 2012

Working with portlet container in websphere application server 7.0-Part3

A portlet only delivers fragment output whereas a servlet typically delivers document output. However, you can use the PortletServingServlet servlet, which is similar to the FileServingServlet servlet, to address portlets like servlets.

 When a portlet is directly addressed by a URL, the portlet output can be modified by filters, called PortletDocumentFilters, embedded into a filter chain. A pre-defined, default filter converts the portlet fragment output of the portlet into a valid document. In this way, portlets can be displayed in the browser as full documents like servlets.

Instead of specifying the filter directly in the web.xml, you register PortletDocumentFilters by an extension point in the plugin.xml, located in the root directory of the JAR file containing the PortletDocumentFilter .

<plugin id="portletwindowfilter" name="WS_Server" provider-name="IBM" version="1.0.0">
  <extension point="com.ibm.ws.portletcontainer.portlet-document-filter-config">
    <portlet-document-filter class-name="sample.WindowFrameFilter" order="1200" />
  </extension>
</plugin>


com.ibm.wsspi.portletcontainer.util.FilterRequestHelper -- provides easy access to control the filter flow.


com.ibm.wsspi.portletcontainer.util.PortletURLHelper -- analyzes a servlet request URL to identify portlet information according to the URL Addressability pattern. This way, the helper enables the filter to easily determine if a portlet action URL is requested or which portlet mode is called.

In this example,filter applies   portlet window states to MyPortlet with border.
 
Sample Code:

package sample;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.wsspi.portletcontainer.InvalidURLException;
import com.ibm.wsspi.portletcontainer.util.FilterRequestHelper;
import com.ibm.wsspi.portletcontainer.util.PortletURLHelper;

// This filter creates a window frame around the portlet content
public class WindowFrameFilter implements Filter
{

    public void init(FilterConfig config) throws ServletException
    {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {

    // do nothing if document cannot be found
    // as this filter is not converting the fragment into a document
        if (!FilterRequestHelper.isDocument(request))
        {
        chain.doFilter(request, response);
            return;
        }
       
        // analyze request url
      HttpServletRequest servletRequest = (HttpServletRequest) request;
      HttpServletResponse servletResponse = (HttpServletResponse) response;
      String portleturl = null;
        try {
            PortletURLHelper urlHelper = new PortletURLHelper("","",servletRequest.getPathInfo());

        // do nothing if action called
            if (urlHelper.isAction())
            {
                chain.doFilter(request, response);
                return;
            }

        portleturl = createPortletURL(servletRequest, urlHelper);
        }
        catch (InvalidURLException e)
        {  
              servletResponse.sendError(
                  e.getStatusCode(),
                  e.getMessage());
              return;
        }

        // if render is called, embed fragment into window frame
        createWindowFrame(request, response, portleturl, chain);
    }
   
    private void createWindowFrame(ServletRequest request, ServletResponse response, String portleturl, FilterChain chain)
        throws IOException, ServletException
    {
    //This filter is not converting the fragment into a document
        //FilterRequestHelper.setDocument(servletRequest);
              PrintWriter writer = response.getWriter();
        writer.println("<TABLE BORDER='5'>");
        writer.println("<TR BGCOLOR='BBBBFF'><TD>");
     
    if (portleturl != null)
    {
        writer.println("<TABLE WIDTH='100%'>");
        writer.println("<TR><TD>");
        writer.println("<B><span id='portlet_title_1'>Portlet</span></B>");
        writer.println("</TD>");
        writer.println("<TD ALIGN='right'>");
        writer.println("<a href='" + portleturl + "/state=normal'>normal</a>");
        writer.println("<a href='" + portleturl + "/state=maximized'>maximized</a>");
        writer.println("<a href='" + portleturl + "/state=minimized'>minimized</a>");
        writer.println("</TD></TR>");
        writer.println("</TABLE>");
    }
    else
    {
        writer.println("<B><span id='portlet_title_1'>Portlet</span></B>");
    }
       
        writer.println("</TD></TR><TR><TD>");

        chain.doFilter(request, response);

    writer.println("</TD></TR></TABLE>");

    writer.println("<script type='text/javascript'>");
    writer.println("document.getElementById('portlet_title_1').firstChild.nodeValue = '" +
               FilterRequestHelper.getDynamicTitle(request) +
               "';");
        writer.println("</script>");
    }

    private String createPortletURL(HttpServletRequest servletRequest, PortletURLHelper urlHelper)
        throws IOException, ServletException
    {
    StringBuffer buffer = new StringBuffer();
    buffer.append(servletRequest.getScheme());
    buffer.append("://");
    buffer.append(servletRequest.getServerName());
    buffer.append(":");
    buffer.append(servletRequest.getServerPort());
    buffer.append(servletRequest.getContextPath());
    buffer.append(servletRequest.getServletPath());
    buffer.append("/");
    buffer.append(urlHelper.getPortletWindowId());
    buffer.append("/ver=");
    buffer.append(urlHelper.getVersion());

    return buffer.toString();
    }

    public void destroy() {
    }

}

ScreenShot

Click here to download the sample code

No comments:

Post a Comment