Tuesday, 11 October 2011

Sample Code for LanguageListProvider in websphere portal server

 This Portlet service provides  the list of portal supported locales.

Sample Code:

package com.ibm.mylanguagelistprovider;
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.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.LanguageListProvider;

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

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

    public static final String VIEW_JSP      = "MyLanguageListProviderPortletView";         // JSP file name to be rendered on the view mode
    PortletServiceHome psh;
    /**
     * @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.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());
        LanguageListProvider provider = (LanguageListProvider) psh.getPortletService(LanguageListProvider.class);
        try {
            LanguageList list = provider.getLanguageList(request);
            Iterator it = list.iterator();
            TreeMap lanmap=new TreeMap();
            Language language = null;
                 while (it.hasNext()) {
                     language = (Language)it.next();
                     String key=language.getTitle(Locale.ENGLISH);
                     String val=language.getLocale().toString();
                     lanmap.put(key,val);          
                 }
            request.setAttribute("result",lanmap);
        } 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";
    }

}


Screen Shot

Click here to download the source code





No comments:

Post a Comment