1 5 package org.exoplatform.portal.portlet.struts; 6 7 import org.apache.commons.logging.Log; 8 import org.exoplatform.services.log.LogUtil; 9 import org.exoplatform.services.portletcontainer.ExoPortletContext; 10 import org.exoplatform.services.portletcontainer.helper.URLEncoder; 11 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.PortletContextImpl; 12 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.PortletSessionImp; 13 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers.CustomRequestWrapper; 14 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers.CustomResponseWrapper; 15 16 import javax.portlet.*; 17 import javax.servlet.RequestDispatcher ; 18 import javax.servlet.ServletContext ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServletRequestWrapper ; 21 import javax.servlet.http.HttpServletResponseWrapper ; 22 import java.io.IOException ; 23 28 public class ExoStrutsPortlet extends GenericPortlet { 29 private static Log log_ = LogUtil.getLog("org.exoplatform.portal.portlet"); 30 private String defaultPage_ ; 31 private String contextPath_ ; 32 33 public void init(PortletConfig config) throws PortletException { 34 super.init(config); 35 defaultPage_ = config.getInitParameter("default-page") ; 36 if(defaultPage_ == null) defaultPage_ = "/index.html" ; 37 contextPath_ = "/" + config.getPortletContext().getPortletContextName() ; 38 } 39 40 public void render (RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException { 41 response.setTitle(getTitle(request)); 42 WindowState state = request.getWindowState(); 43 if ( ! state.equals(WindowState.MINIMIZED)) { 44 PortletMode mode = request.getPortletMode(); 45 if (mode.equals(PortletMode.VIEW)) { 46 doView (request, response); 47 } else if (mode.equals(PortletMode.EDIT)) { 48 doEdit (request, response); 49 } else if (mode.equals(PortletMode.HELP)) { 50 doHelp (request, response); 51 } else { 52 throw new PortletException("unknown portlet mode: " + mode); 53 } 54 } 55 } 56 57 public void doView(RenderRequest request, RenderResponse response) 58 throws PortletException, IOException 59 { 60 doRender(request, response) ; 61 } 62 63 public void doHelp(RenderRequest request, RenderResponse response) 64 throws PortletException, IOException 65 { 66 doRender(request, response) ; 67 } 68 69 public void doEdit(RenderRequest request, RenderResponse response) 70 throws PortletException, IOException 71 { 72 doRender(request, response) ; 73 } 74 75 public void doConfig(RenderRequest request, RenderResponse response) 76 throws PortletException, IOException 77 { 78 doRender(request, response) ; 79 } 80 81 protected void doRender(RenderRequest request, RenderResponse response) throws PortletException, IOException { 82 log_.debug( "$$$ dispatch to servlet acion $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" ); 83 String path = "/struts-controller" ; 84 CustomRequestWrapper requestWrapper = null; 85 86 ExoPortletContext context = (ExoPortletContext) request.getPortletSession().getPortletContext(); 87 boolean isSharedSessionEnable = context.isSessionShared(); 88 requestWrapper = ((CustomRequestWrapper) ((HttpServletRequestWrapper )request).getRequest()); 89 CustomResponseWrapper responseWrapper = (CustomResponseWrapper) ((HttpServletResponseWrapper )response).getResponse(); 90 try { 91 PortletContextImpl portletContext = (PortletContextImpl) getPortletContext() ; 92 ServletContext scontext = portletContext.getWrappedServletContext() ; 93 RequestDispatcher dispatcher = null; 94 95 String requestUri = requestWrapper.getParameter("requestUri") ; 96 log_.debug("URI requested : " + requestUri); 97 if(requestUri == null) { 98 dispatcher = scontext.getRequestDispatcher(defaultPage_) ; 99 } else { 100 requestUri = requestUri.substring(contextPath_.length() , requestUri.length()) ; 101 if (requestUri.endsWith(".do")) { 102 dispatcher = scontext.getRequestDispatcher(path) ; 103 requestWrapper.setAttribute("exo.forward", requestUri) ; 104 } else { 105 dispatcher = scontext.getRequestDispatcher(requestUri) ; 106 } 107 } 108 responseWrapper.flushBuffer(); 109 requestWrapper.setRedirected(true); 110 requestWrapper.setRedirectedPath(path); 111 URLEncoder urlencoder = 112 new StrutsURLEncoder(contextPath_, response.createRenderURL().toString()) ; 113 responseWrapper.setURLEncoder(urlencoder) ; 114 if(isSharedSessionEnable){ 115 PortletSessionImp pS = (PortletSessionImp)request.getPortletSession(); 116 requestWrapper.setSharedSession(pS.getSession()); 117 requestWrapper.setContextPath(request.getContextPath()); 118 } 119 dispatcher.include(requestWrapper, responseWrapper); 120 } catch (ServletException e) { 121 throw new PortletException("Problems occur when using PortletDispatcher", e); 122 } finally { 123 if(requestWrapper != null) 124 requestWrapper.setRedirected(false); 125 if(isSharedSessionEnable){ 126 requestWrapper.setSharedSession(null); 127 } 128 responseWrapper.setURLEncoder(null) ; 129 } 130 log_.debug( "$$$ end dispatch to struts controller $$$$$$$$$$$$$$$$$$$$$$$$" ); 131 } 132 133 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { 134 135 } 136 } 137 | Popular Tags |