Friday 14 October 2011

Sample Code for PortalURLGenerationService in Websphere Portal Server


URL Generation API only lets you create render URLs because render URLs are safe; you can even send them around in email. Action URLs require security control; therefore, you can only create them using the Navigational State SPI discussed below. Render URLs should be associated with HTTP GET requests, whereas action URLs should be associated with HTTP POST requests. The URL Generation API is not meant for WSRP-based remote portlets.
PortalURLGenerationService is used to retrieve a PortalURLWriter based on the current state information encoded in the request.
PortalURLWriter has two different kinds of methods:
·         Those that generate URLs to other content nodes, such as portal pages
·         Those that create URLs to portlets, either the current one or a different portlet

PortalURLWriter Methods
boolean
isInPortalState(Constants.PortalState portalState)
Returns true, if the given portal state is the same as the portal state of the current request.
java.io.Writer
writeContentNodeRenderURL(java.io.Writer writer, ObjectID contentNode)
Creates a render URL that points to the specified content node
java.io.Writer
writeContentNodeRenderURL(java.io.Writer writer, java.lang.String contentNode)
Creates a render URL that points to the specified content node
java.io.Writer
writePortletRenderURL(java.io.Writer writer, Constants.PortalState portalState)
Creates a render URL that points to the current portlet window with the given portal state.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, ObjectID contentNode, ObjectID portletWindow)
Creates a render URL that points to the specified portlet window.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, ObjectID contentNode, ObjectID portletWindow, PortletMode newPortletMode)
Creates a render URL that points to the specified portlet window with the specified portlet mode.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, ObjectID contentNode, ObjectID portletWindow, PortletMode newPortletMode, WindowState newWindowState, Constants.PortalState portalState, java.util.Map<java.lang.String,java.lang.String[]> newRenderParameters, java.util.Locale newLocale)
Creates a render URL that points to the specified portlet window.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, ObjectID contentNode, ObjectID portletWindow, WindowState newWindowState)
Creates a render URL that points to the specified portlet window with the new window state.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, java.lang.String contentNode, java.lang.String portletWindow)
Creates a render URL that points to the specified portlet window.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, java.lang.String contentNode, java.lang.String portletWindow, PortletMode newPortletMode)
Creates a render URL that points to the specified portlet window with the specified portlet mode.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, java.lang.String contentNode, java.lang.String portletWindow, PortletMode newPortletMode, WindowState newWindowState, Constants.PortalState portalState, java.util.Map<java.lang.String,java.lang.String[]> newRenderParameters, java.util.Locale newLocale)
Creates a render URL that points to the specified portlet window.
java.io.Writer
writePortletRenderURL(java.io.Writer writer, java.lang.String contentNode, java.lang.String portletWindow, WindowState newWindowState)
Creates a render URL that points to the specified portlet window with the new window state.

In this example, I have created a page with unique name of  test.IBMPortletUtils. In that page, I added  two portlets.
One of portlet unique name is “test.IBMPortletUtils.Portlet”.
 
I have Created a page and add MyPortalURLGenerationService Portlet .
In the page, on click “Go to IBMPortletUtils Page”, it yields to IBMPortletUtils page.

Sample Code:
package com.ibm.myportalurlgenerationservice;
import java.io.*;
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.portlet.service.url.PortalURLGenerationService;
import com.ibm.portal.portlet.service.url.PortalURLWriter;
/**
 * A sample portlet based on GenericPortlet
 */
public class MyPortalURLGenerationServicePortlet extends GenericPortlet {
private static final String JNDI_NAME =
                          "portletservice/com.ibm.portal.portlet.service.url.PortalURLGenerationService";
PortletServiceHome psh;
           
            /**
             * @see javax.portlet.Portlet#init()
             */
            public void init() throws PortletException{
                        super.init();
                        try {
                                    Context ctx = new InitialContext();
                                    psh = (PortletServiceHome) ctx.lookup(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)
             */
            public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
                        // Set the MIME type for the render response
                         response.setContentType(request.getResponseContentType());
                         PortalURLGenerationService portalUrlGenerationService = (PortalURLGenerationService) psh.getPortletService(PortalURLGenerationService.class);
                         PortalURLWriter portalUrlWriter = portalUrlGenerationService.getPortalURLWriter(request,response);
                         String uniquenamepage="test.IBMPortletUtils";
                         String uniquenameportlet="test.IBMPortletUtils.Portlet";
                         PrintWriter out=response.getWriter();
                         try
                         {
                                     StringWriter mywriter = new StringWriter();
                                     out.print("<a href="+portalUrlWriter.writePortletRenderURL(mywriter,
                                                             uniquenamepage,uniquenameportlet,WindowState.MAXIMIZED).toString()
                                                             +">"+"Go to IBMPortletUtils Page</a>");
                                   
                         }
                         catch(Exception e)
                         {
                                     out.print("Error occured while generating url");
                         }
                         finally
                         {
                                     out.close();
                                     portalUrlWriter.dispose();
                         }
            }
}

Click here to download the source code 

No comments:

Post a Comment