1 6 7 package org.exoplatform.portlet.cocoon; 8 9 10 import javax.portlet.*; 11 import javax.servlet.RequestDispatcher ; 12 import javax.servlet.ServletContext ; 13 import javax.servlet.http.HttpServletRequestWrapper ; 14 import org.exoplatform.portlet.commons.servlet.BufferedServletResponse; 15 import org.exoplatform.services.portletcontainer.ExoPortletContext; 16 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.*; 17 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers.CustomRequestWrapper; 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.util.ArrayList ; 23 import org.apache.commons.logging.Log; 24 import org.exoplatform.container.PortalContainer; 25 import org.exoplatform.container.SessionContainer; 26 import org.exoplatform.services.log.LogService; 27 import org.exoplatform.portal.session.RequestInfo; 28 import org.exoplatform.Constants; 29 31 37 38 public class CocoonPortlet extends GenericPortlet { 39 private String servletName; 40 private Log log; 41 public void init(PortletConfig config) throws PortletException { 42 super.init(config); 43 PortalContainer manager = PortalContainer.getInstance(); 44 LogService logService = 45 (LogService) manager.getComponentInstanceOfType(LogService.class); 46 log = logService.getLog(this.getClass()); 47 48 servletName = config.getInitParameter("servlet-name"); 49 if (servletName == null) 50 servletName = config.getPortletName(); 51 52 log.debug("servletName ["+servletName+"]"); 53 54 55 } 56 57 public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException { 58 doView(request, response); 59 } 60 61 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { 62 WindowState state = request.getWindowState(); 63 String path = "/"; 64 CustomRequestWrapper requestWrapper = null; 66 ExoPortletContext context = (ExoPortletContext) request.getPortletSession().getPortletContext(); 67 boolean isSharedSessionEnable = context.isSessionShared(); 68 try { 69 requestWrapper = ((CustomRequestWrapper) ((HttpServletRequestWrapper ) request).getRequest()); 70 BufferedServletResponse responseWrapper = new BufferedServletResponse((RenderResponseImp) response); 71 PortletContextImpl portletContext = (PortletContextImpl) getPortletContext(); 72 ServletContext scontext = portletContext.getWrappedServletContext(); 73 RequestDispatcher dispatcher = null; 74 String requestUrl = requestWrapper.getParameter("url"); 75 System.out.println("Portlet RequestURI: " + requestUrl); 76 log.debug("Portlet RequestURI: " + requestUrl); 77 dispatcher = scontext.getNamedDispatcher(servletName); 78 if (requestUrl != null) 79 path += requestUrl; 80 log.debug("Real Path: " + scontext.getRealPath(path)); 82 83 responseWrapper.flushBuffer(); 84 requestWrapper.setRedirected(true); 85 requestWrapper.setRedirectedPath(path); 86 if (isSharedSessionEnable) { 87 PortletSessionImp pS = (PortletSessionImp) request.getPortletSession(); 88 requestWrapper.setSharedSession(pS.getSession()); 89 requestWrapper.setContextPath(request.getContextPath()); 90 } 91 dispatcher.include(requestWrapper, responseWrapper); 92 response.setContentType("text/html"); 93 94 95 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream (); 96 new URLRewriter().rewrite( 97 98 new ByteArrayInputStream (responseWrapper.toByteArray()), 99 byteOutput, 101 getBaseURI(),requestUrl); 102 Writer writer = response.getWriter(); 103 writer.write(byteOutput.toString()); 104 105 } catch (Exception e) { 106 e.printStackTrace(); 107 throw new PortletException("Problems occur when using PortletDispatcher", e); 108 } finally { 109 if (requestWrapper != null) 110 requestWrapper.setRedirected(false); 111 if (isSharedSessionEnable) { 112 requestWrapper.setSharedSession(null); 113 } 114 } 115 } 116 117 private String getBaseURI(){ 119 RequestInfo rinfo = 120 (RequestInfo)SessionContainer.getComponent(RequestInfo.class); 121 String path = rinfo.getOwnerURI(); 122 123 124 ArrayList paramsArray = new ArrayList (); 125 126 if (rinfo.getTargetComponentId() != null){ 128 paramsArray.add( 129 Constants.COMPONENT_PARAMETER + "="+rinfo.getTargetComponentId()); 130 } 131 132 if (rinfo.getPortletWindowState() != null){ 133 paramsArray.add( 134 Constants.WINDOW_STATE_PARAMETER+"="+rinfo.getPortletWindowState()); 135 } 136 137 if (rinfo.getPortletMode() != null){ 138 paramsArray.add( 139 Constants.PORTLET_MODE_PARAMETER+"="+rinfo.getPortletMode()); 140 } 141 142 if (! paramsArray.isEmpty()){ 144 path += "?" + paramsArray.get(0); 145 for (int i = 1; i < paramsArray.size(); i++){ 146 path += "&" + paramsArray.get(i); 147 } 148 } 149 150 return path; 151 } 152 153 } 154 | Popular Tags |