Saturday, 8 October 2011

Sample Code for ClientListProvider in websphere portal server

ClientListProvider provides details of client.Client can be serverside and clientside.                                   

ServerSide Client :
  • HTTPClient
  • URL
  • JMETER
ClientSide Client is browser

package com.ibm.myclientlistprovider;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.portal.ModelException;
import com.ibm.portal.admin.Client;
import com.ibm.portal.admin.ClientList;
import com.ibm.portal.model.ClientListHome;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.ClientListProvider;
import com.ibm.wps.pb.utils.portlet.PortletUtils;

/**
 * A sample portlet based on GenericPortlet
 */
public class MyClientListProviderPortlet extends GenericPortlet {

    public static final String JSP_FOLDER    = "/_MyClientListProvider/jsp/";    // JSP folder name

    public static final String VIEW_JSP      = "MyClientListProviderPortletView";         // JSP file name to be rendered on the view mode
    PortletServiceHome psh;
    /**
     * @see javax.portlet.Portlet#init()
     */
    public void init() throws PortletException{
        super.init();
         Context ctx;
        try {
            ctx = new InitialContext();
            psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.ClientListProvider");
        } 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());
        ClientListProvider provider = (ClientListProvider) psh.getPortletService(ClientListProvider.class);
        ClientList list;
        try {
            list = provider.getClientList(request);
            String useragent=PortletUtils.getHttpServletRequest(request).getHeader("User-Agent");
            Client client=(Client) list.getLocator().getClient(useragent);
            AgentList agent=new AgentList();
            agent.setManufacture(client.getManufacturer());
            agent.setMarkupVersion(client.getMarkupVersion());
            agent.setModel(client.getModel());
            agent.setUserAgentPattern(client.getUserAgentPattern());
            agent.setVersion(client.getVersion());
            agent.setMark(client.getMarkup().toString());
            ArrayList<String> capabilities=new ArrayList<String>();
            Iterator<String> itr=client.getCapabilities();
            while(itr.hasNext())
            {
                capabilities.add(itr.next().toString());
            }
            agent.setCapabilities(capabilities);
            request.setAttribute("clientdetail", agent);
        } catch (ModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      

        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }

    /**
     * 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 {
    }

    /**
     * Returns JSP file path.
     *
     * @param request Render request
     * @param jspFile JSP file name
     * @return JSP file path
     */
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
    }

    /**
     * Convert MIME type to markup name.
     *
     * @param contentType MIME type
     * @return Markup name
     */
    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
        else
            return "html";
    }

    /**
     * Returns the file extension for the JSP file
     *
     * @param markupName Markup name
     * @return JSP extension
     */
    private static String getJspExtension(String markupName) {
        return "jsp";
    }

}

ScreenShot:





Click here to download samplecode

No comments:

Post a Comment