Monday, 20 October 2014

Play with Portlet/Portal state manager ,Accessor api and Engine urls websphere portal8

Overview :- The main concept is to construct urls to pages outside port lets and inside portlets without using NavigationModelProvider with given unique name.

PortalStateManagerService :-  This service is used to create urls in portal theme and skins

PortletStateManagerService :-  This service is used to create urls in portlets please find exp below.

Example let you navigate from one page to other page{This will link porletStateManager, URLEngine, Accessor Api, Dom model}:- 

package com.ibm.modelspiportlet;

import java.io.*;
import java.util.*;
import java.util.logging.Logger;

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.state.Constants;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.PortletStateManager;
import com.ibm.portal.state.URLFactory;
import com.ibm.portal.state.accessors.exceptions.CannotInsertSelectionNodeException;
import com.ibm.portal.state.accessors.exceptions.MissingUniqueNameException;
import com.ibm.portal.state.accessors.exceptions.StateNotInRequestException;
import com.ibm.portal.state.accessors.exceptions.UnknownUniqueNameException;
import com.ibm.portal.state.accessors.portlet.PortletAccessorController;
import com.ibm.portal.state.accessors.portlet.PortletAccessorFactory;
import com.ibm.portal.state.accessors.selection.SelectionAccessorController;
import com.ibm.portal.state.accessors.selection.SelectionAccessorFactory;
import com.ibm.portal.state.exceptions.CannotCloneDocumentModelException;
import com.ibm.portal.state.exceptions.CannotCreateDocumentException;
import com.ibm.portal.state.exceptions.CannotInstantiateAccessorException;
import com.ibm.portal.state.exceptions.CannotInstantiateURLFactoryException;
import com.ibm.portal.state.exceptions.InvalidConstantException;
import com.ibm.portal.state.exceptions.OutputMediatorException;
import com.ibm.portal.state.exceptions.PostProcessorException;
import com.ibm.portal.state.exceptions.StateException;
import com.ibm.portal.state.exceptions.StateManagerException;
import com.ibm.portal.state.exceptions.UnknownAccessorTypeException;
import com.ibm.portal.state.service.PortletStateManagerService;

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

public static final String JSP_FOLDER = "/_ModelSpiPortlet4/jsp/"; // JSP

public static final String VIEW_JSP = "ModelSpiPortlet4PortletView"; // JSP

public static final Logger logger = Logger.getLogger(ModelSpiPortlet4Portlet.class.getName());

PortletServiceHome serviceHome = null;
protected PortletStateManagerService service;

private static final String JNDI_NAME =
   "portletservice/com.ibm.portal.state.service.PortletStateManagerService";


/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException {
super.init();
try {
     // lookup the portlet state manager service
    Context ctx = new InitialContext();
    serviceHome = (PortletServiceHome)
       ctx.lookup(JNDI_NAME);
     service = (PortletStateManagerService)
       serviceHome.getPortletService(PortletStateManagerService.class);
   } catch (NameNotFoundException e) {
     throw new PortletException(e);
   } catch (NamingException e) {
     throw new PortletException(e);
   }
}

/**
* 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());

// 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 {
String finalUrl = null;
try {
PortletStateManager psm = service.getPortletStateManager(request, response);
URLFactory urlFactory = psm.getURLFactory();
                        //Construct engine url and then add state
EngineURL url = urlFactory.newURL(Constants.SMART_COPY);
                        // Get the instance of Abstract class SelectionAcessorfactory for all operations
SelectionAccessorFactory selectionFactory = (SelectionAccessorFactory) psm.getAccessorFactory(SelectionAccessorFactory.class);
            //Using accessor we will get the control to modify the state 
            SelectionAccessorController selectionCtrl = selectionFactory.getSelectionAccessorController(url.getState());
            //Set Unique name were to navigate
            selectionCtrl.setSelection("inter.page");
           //Fianlly get string format of url
            finalUrl = url.writeDispose(new StringWriter()).toString();
            urlFactory.dispose();
            logger.info("Url name :- "+finalUrl);
            response.sendRedirect(finalUrl);

} catch (StateManagerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotInstantiateURLFactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidConstantException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotCloneDocumentModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotCreateDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StateNotInRequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownAccessorTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotInstantiateAccessorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotInsertSelectionNodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MissingUniqueNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownUniqueNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PostProcessorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OutputMediatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 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";
}

}

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...