Usecase scenario :- There might be a scenario were in portlet action phase you need to redirect to a different page. The page which we will be redirecting will be know dynamically only.
To redirect to different page we need oid of its page or friendly url of it. But these cannot be retrieved are known before. So we can have uniqenames of pages as list. Then decide to navigate to which page and pick corresponding uniquename.
Example overview :-
Below example shows retrieving ObjectId of different page from given unique name. And then redirecting to the page via portlet action send redirect phase.
To redirect to different page we need oid of its page or friendly url of it. But these cannot be retrieved are known before. So we can have uniqenames of pages as list. Then decide to navigate to which page and pick corresponding uniquename.
Example overview :-
Below example shows retrieving ObjectId of different page from given unique name. And then redirecting to the page via portlet action send redirect phase.
package com.ibm.modelspiportlet;
import java.io.IOException;
import java.util.logging.Logger;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.ibm.portal.ObjectID;
import com.ibm.portal.identification.Identification;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.serialize.SerializationException;
/**
* A sample portlet based on GenericPortlet
*/
public class ModelSpiPortlet2Portlet extends GenericPortlet {
public static final String JSP_FOLDER = "/_ModelSpiPortlet2/jsp/"; // JSP folder name
public static final String VIEW_JSP = "ModelSpiPortlet2PortletView"; // JSP file name to be rendered on the view mode
public ObjectID oidForName =null;
public PortletServiceHome psh;
public Identification idf;
public static Logger logger = Logger.getLogger(ModelSpiPortlet2Portlet.class.getName());
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
try{
// initialization
Context ctx = new InitialContext();
// portal:uniquename prefix is required for unique name lookup
Name uniqueName = new CompositeName("portal:uniquename");
// Add unique name assigned of the page to which you want to navigate
uniqueName.add("custom.modelSpi");
//lookup for objectId from unique name
oidForName = (ObjectID) ctx.lookup(uniqueName);
idf = (Identification)ctx.lookup(Identification.JNDI_NAME);
}catch(NamingException exp){
exp.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());
// 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 redirecrtPageId=null;
try {
redirecrtPageId =idf.serialize(oidForName);
logger.info("string representation of object :-"+redirecrtPageId);
} catch (SerializationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("/wps/myportal/?uri=nm:oid:"+redirecrtPageId);
}
/**
* 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