Tuesday 11 October 2011

Sample Code for MarkupListProvider in Websphere Portal Server

This service provides  information of  markup which is supported  by portal.

Sample Code:

package com.ibm.markuplistprovider;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.portal.ModelException;
import com.ibm.portal.admin.Language;
import com.ibm.portal.admin.LanguageList;
import com.ibm.portal.admin.Markup;
import com.ibm.portal.admin.MarkupList;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.LanguageListProvider;
import com.ibm.portal.portlet.service.model.MarkupListProvider;

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

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

    public static final String VIEW_JSP      = "MarkupListProviderPortletView";         // JSP file name to be rendered on the view mode
    PortletServiceHome psh;
    PortletServiceHome psh1;
    /**
     * @see javax.portlet.Portlet#init()
     */
    public void init() throws PortletException{
        super.init();
        try {
            Context ctx = new InitialContext();
            psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.MarkupListProvider");
            psh1 = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.LanguageListProvider");
            } 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());
        MarkupListProvider provider = (MarkupListProvider) psh.getPortletService(MarkupListProvider.class);
        try {
            MarkupList list = provider.getMarkupList(request);
            LanguageListProvider lanprovider = (LanguageListProvider) psh.getPortletService(LanguageListProvider.class);
            LanguageList lanlist = lanprovider.getLanguageList(request);
            Language language = null;
            ArrayList<MarkList> arrmlist=new ArrayList<MarkList>();
            for( Iterator lt=list.iterator();lt.hasNext();)
            {
                Markup mark=(Markup)list.getByName(lt.next().toString());   
                MarkList mlist=new MarkList();
                mlist.setTitle(mark.getTitle(Locale.ENGLISH));
                mlist.setMimeType(mark.getMimeType());
                mlist.setName(mark.getName());
                mlist.setActive(String.valueOf(mark.isActive()));
                mlist.setCharset(mark.getCharset(Locale.ENGLISH));
                mlist.setDefaultcharset(mark.getDefaultCharset());
                ArrayList<LanguageBean> arrLlist=new ArrayList<LanguageBean>();
                arrLlist.clear();
                Iterator lanitr = lanlist.iterator();
                while (lanitr.hasNext()) {
                    language = (Language)lanitr.next();   
                    LanguageBean llist=new  LanguageBean();
                    llist.setTitle(language.getTitle(Locale.ENGLISH));
                    llist.setCharset(mark.getCharset(language));
                    arrLlist.add(llist);
                }
                mlist.setArrlist(arrLlist);
                arrmlist.add(mlist);
            }
            request.setAttribute("result", arrmlist);
        } 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 the source code





No comments:

Post a Comment