Wednesday 19 October 2011

Sample Code for PortletPocService in Websphere Portal Server


Using this service,we can obtain POC URL. To know what is POC URL ,please refer this link

Method Summary of PortletPocServiceHome
createContext(PortletRequest request, PortletResponse response)
Constructs the COR context for request and response
getPortletPocService(ActionRequest request, ActionResponse response)
Get a portlet POC service for the current request and response.
getPortletPocService(EventRequest request, EventResponse response)
Get a portlet POC service for the current request and response.
getPortletPocService(PortletRequest request, PortletResponse response)
Get a portlet POC service for the current request and response.
getPortletPocService(RenderRequest request, RenderResponse response)
Get a portlet POC service for the current request and response.
getPortletPocService(ResourceRequest request, ResourceResponse response)
Get a portlet POC service for the current request and response.

Each service needs  PocURLFactory object to create POC URLS.

PocURLFactory is Factory to create POC URLs. Depending on the factory method, the URL will directly point to the portal servlet, if it represents a view that keeps the navigational state of the current interaction with portal.

Method Summary of PocURLFactory
getServerContext()
Returns information about the URL configuration in the scope of the current request.
getServerContext(java.lang.Boolean bLateBinding)
Returns information about the URL configuration in the scope of the current request.
newURL(java.lang.Boolean bLateBinding)
Generates a URL that points to the POC servlet and that does not contain navigational state.
newURL(Constants.Clone type, java.lang.Boolean bLateBinding)
Generates a URL that points to the portal servlet and it based on the navigational state of the current request.
newURL(DataSource ds)
Constructs a URL that points to the POC servlet and that does not contain navigational state.
newURL(StateHolder state, Constants.Clone type, java.lang.Boolean bLateBinding)
Generates a URL that points to the portal servlet and it based on navigational state explicitly.
newURL(StateHolder state, URLContext allowedContext, Constants.Clone type, java.lang.Boolean bLateBinding)
Generates a URL that points to the portal servlet and it based on navigational state explicitly.
newURL(URLContext allowedContext, java.lang.Boolean bLateBinding)
Generates a URL that points to the POC servlet and that does not contain navigational state.
newURL(URLContext allowedContext, Constants.Clone type, java.lang.Boolean bLateBinding)
Generates a URL that points to the portal servlet and it based on the navigational state of the current request.

POCURL

The PocURL is a URL that addresses a piece of content (POC). Depending on the way the actual URL instance has been achieved, such a URL might represent the view of a resource or its binary representation. The binding to the actual resource might happen early (i.e. at URL generation time) or late (only when the URL is invoked). 

Via the writeCopy and writeDispose methods the URL can be written to a stream. writeDispose should be used wherever possible as this methods has a smaller performance impact than writeCopy. The PocURL should be disposed if it is no longer used by calling the dispose() method. The writeDispose however writes the URL to a stream and disposes it in one single step, so if writeDispose is called, the object must not be disposed additionally. 

Note: For  LATE_BINDING, POC URL is constructed as /wps/poc. For EARLY_BINDING ,,POC URL is constructed as /wps/contenthandler.


Method Summary of POCURL
java.lang.String
getDigest()
Returns a string that represents a hash over the request headers that might influence the responses (independent of the request URL).
java.lang.String
getMode()
Returns the mode that is currently associated with the resouce
java.util.Map<java.lang.String,java.lang.String[]>
getParameters()
Returns a modifiable version of the parameters used to address the object, never null, but potentially the empty map.
void
setAddressable(Addressable aAddr)
Associates the URI and parameters of the addressable object
void
setDigest(java.lang.String aDigest)
Associates a digest with the POC URL.
void
setMode(java.lang.String aMode)
Associates a mode with the URI.
void
setProtected(java.lang.Boolean bFlag)
Indicates if the URL points to the private area or to the public area .
void
setSecure(java.lang.Boolean bFlag)
Indicates the security setting for this URL.
void
setURI(java.net.URI aURI)
Associates a POC URI with the URL object.
java.io.Writer
writeCopy(java.io.Writer out, boolean bEscapeXML)
Streams the state represented by this URL object to a writer.
java.io.Writer
writeDispose(java.io.Writer out, boolean bEscapeXML)
Streams the state represented by this URL object to a writer.

URI can be in the following formats:
      uri = "cm:" page-oid |
            "nm:" navigationnode-oid |
            "lm:" [layoutnode-oid "@"] page-oid |
            "pm:" portlet-oid ["@" page-oid

Example:
/wps/mycontenthandler?uri=cm:oid:wps.content.root

In this example, I have created a pocurl pointing to IBMPortletUtils page.

Sample Code:
package com.ibm.mypocservice;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.resolver.PocURL;
import com.ibm.portal.resolver.accessors.url.PocURLFactory;
import com.ibm.portal.resolver.service.PortletPocServiceHome;
import com.ibm.portal.resolver.service.PortletRenderPocService;
import com.ibm.portal.state.exceptions.StateException;
import com.ibm.portal.state.exceptions.StateManagerException;



/**
 * A sample portlet based on GenericPortlet
 */
public class MyPocServicePortlet extends GenericPortlet {
      PortletServiceHome psh;
      /**
       * @see javax.portlet.Portlet#init()
       */
      public void init() throws PortletException{
            super.init();
            try {
                  Context ctx = new InitialContext();
                  psh = (PortletServiceHome) ctx.lookup(PortletPocServiceHome.JNDI_NAME);
                  } catch(NameNotFoundException ex) {
                        ex.printStackTrace();
                  } catch (NamingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
      }

      /**
       * Serve up the <code>view</code> mode.
       *
       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
       */
     
      /**
       *          uri=model-uri
            The identification of the addressed resource, as described further below. The uri parameter can appear only once.
            model-uri = "cm:" page-oid |
            "nm:" navigationnode-oid |
            "lm:" [layoutnode-oid "@"] page-oid |
            "pm:" portlet-oid ["@" page-oid
            /wps/mycontenthandler?uri=lm:oid:_7_0830M4HTFF0SHFCQ_2BV@oid:_6_0830M4HTFF0SHFCQ_4D
             /wps/mycontenthandler?uri=cm:oid:wps.content.root
    
       */
      public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
            // Set the MIME type for the render response
            response.setContentType(request.getResponseContentType());
            PortletPocServiceHome service = (PortletPocServiceHome) psh.getPortletService(PortletPocServiceHome.class);
        // get the POC service for the current request
        try {
            PortletRenderPocService pocSvc = service.getPortletPocService(request, response);
            String uniquenamepage="test.IBMPortletUtils";
            String uniquenameportlet="test.IBMPortletUtils.Portlet";
            URI pocURI= new URI("pm:oid:"+uniquenameportlet+"@oid:"+uniquenamepage);
            // When this resource URL is actually invoked, the portlet receives a callback to the
            //ResourceServingPortlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse) method
                  ResourceURL url=pocSvc.createResourceURL(pocURI);// Calls the serveresource method of this portlet
                  PocURL pocurl=pocSvc.getURLFactory().newURL(PocURLFactory.LATE_BINDING);
                  pocurl.setURI(pocURI);
                  response.getWriter().print("<a href='"+url.toString()+"' target='_blank'>ResourceURL</a><br/>");
                  response.getWriter().print("<a href='"+pocurl.toString()+"'>PocURL For IBMPortletUtils</a>");

            } catch (StateManagerException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (URISyntaxException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (StateException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }

      /**
       * Process an action request.
       *
       * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
       */
      public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
           
      }
     
      /**
       * Process an Resource request.
       *
       * @see javax.portlet.Portlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
       */
      public void serveResource(ResourceRequest request, ResourceResponse response)throws PortletException, java.io.IOException {
            System.out.println("calling serveResource method of MyPocServicePortlet");
      }


}

ScreenShot



 Click here to download the source code

No comments:

Post a Comment