Friday, 14 October 2011

Sample Code for PortalFriendlySelectionService in Websphere Portal Server


This portal service allows to work with friendly selections. FriendlyURLFactory provides access to FriendlyURL objects that allow to systematically generate URLs with a friendly prefix. FriendlyURL is similar to EngineURL. 


FriendlyURLFactory  Method Summary
getServerContext()
Returns information about the URL configuration in the scope of the current request.
newURL()
Generates a URL that points to the portal servlet and it based on the navigational state of the current request.
newURL(Constants.Clone type)
Generates a URL that points to the portal servlet and it based on the navigational state of the current request.
newURL(ServerContext serverContext, URLContext allowedContext, Constants.Clone type)
Generates a URL based on the given server context.
newURL(URLContext allowedContext, Constants.Clone type)
Generates a URL that points to the portal servlet and it based on the navigational state of the current request.
In this example, On click of url, it points to “Home” page.
 Note: Friendly URL is created same as URL mapping. To know difference between friendly URL and URL mapping refer to this link

Sample Code

Package com.ibm.myportletfriendlyselectionservice;
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.resolver.friendly.FriendlyURL;
import com.ibm.portal.resolver.friendly.accessors.url.FriendlyURLFactory;
import com.ibm.portal.resolver.friendly.service.PortletFriendlySelectionService;
import com.ibm.portal.resolver.friendly.service.PortletFriendlySelectionServiceHome;
import com.ibm.portal.state.Constants;
/**
 * http://localhost:10039/wps/myportal/Home
 * Portlet service which allows for working with friendly URLs.
 */
public class MyPortletFriendlySelectionServicePortlet extends GenericPortlet {
            /**
             * @see javax.portlet.Portlet#init()
             */
            PortletServiceHome psh;
            public void init() throws PortletException{
                        super.init();
                        try {
                                    Context ctx = new InitialContext();
                                    psh = (PortletServiceHome) ctx.lookup(PortletFriendlySelectionServiceHome.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());
                        PortletFriendlySelectionServiceHome service = (PortletFriendlySelectionServiceHome) psh.getPortletService(PortletFriendlySelectionServiceHome.class);
        // get the service for the current request
                         PrintWriter out=response.getWriter();
        try {
                                     PortletFriendlySelectionService selSvc = service.getPortletFriendlySelectionService(request, response);
                                     FriendlyURL url=selSvc.getURLFactory().newURL(new MyServerContext(),new MyURLContext(),null);
                                     url.setSelection("ibm.portal.Home");
                                     out.print("<a href="+url+">"+"Generate URL</a>");
                                     url.dispose();
                                   
                        } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                     out.print("Error occured while generating url");
                        }
                        finally
                        {
                                    out.close();
                        }
            }
}


Screen Shot

Click here to download the sample code

No comments:

Post a Comment