KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > cocoon > CocoonPortlet


1 /**
2  **************************************************************************
3  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
4  * Please look at license.txt in info directory for more license detail. *
5  */

6
7 package org.exoplatform.portlet.cocoon;
8
9
10 import javax.portlet.*;
11 import javax.servlet.RequestDispatcher JavaDoc;
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
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 JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
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 //import javax.servlet.ServletOutputStream;
30

31 /**
32  * Created by The eXo Platform SARL .
33  *
34  * @author Gennady Azarenkov
35  * @version $Id: CocoonPortlet.java 596 2005-01-28 15:45:37Z kravchuk $
36  */

37
38 public class CocoonPortlet extends GenericPortlet {
39   private String JavaDoc 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 JavaDoc {
58     doView(request, response);
59   }
60
61   public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
62       WindowState state = request.getWindowState();
63     String JavaDoc path = "/";
64     // System.out.println("---->>>>>>>Cocoon Portlet in View Mode!");
65
CustomRequestWrapper requestWrapper = null;
66     ExoPortletContext context = (ExoPortletContext) request.getPortletSession().getPortletContext();
67     boolean isSharedSessionEnable = context.isSessionShared();
68     try {
69       requestWrapper = ((CustomRequestWrapper) ((HttpServletRequestWrapper JavaDoc) request).getRequest());
70       BufferedServletResponse responseWrapper = new BufferedServletResponse((RenderResponseImp) response);
71       PortletContextImpl portletContext = (PortletContextImpl) getPortletContext();
72       ServletContext JavaDoc scontext = portletContext.getWrappedServletContext();
73       RequestDispatcher JavaDoc dispatcher = null;
74       String JavaDoc 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       // System.out.println("Real Path: " + scontext.getRealPath(path));
81
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 JavaDoc byteOutput = new ByteArrayOutputStream JavaDoc();
96       new URLRewriter().rewrite(
97
98                 new ByteArrayInputStream JavaDoc(responseWrapper.toByteArray()),
99                 //response.getPortletOutputStream(),
100
byteOutput,
101                 getBaseURI(),requestUrl);
102       Writer JavaDoc writer = response.getWriter();
103       writer.write(byteOutput.toString());
104
105     } catch (Exception JavaDoc 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   //URI (path + query string) of current string
118
private String JavaDoc getBaseURI(){
119       RequestInfo rinfo =
120             (RequestInfo)SessionContainer.getComponent(RequestInfo.class);
121       String JavaDoc path = rinfo.getOwnerURI();
122
123
124       ArrayList JavaDoc paramsArray = new ArrayList JavaDoc();
125
126       //query string
127
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       //add query to path
143
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