KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > portlet > struts > ExoStrutsPortlet


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

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 JavaDoc;
18 import javax.servlet.ServletContext JavaDoc;
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
21 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
22 import java.io.IOException JavaDoc;
23 /**
24  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
25  * @since Dec 3, 2003 3:53:21 PM
26  * @version $rev
27  */

28 public class ExoStrutsPortlet extends GenericPortlet {
29   private static Log log_ = LogUtil.getLog("org.exoplatform.portal.portlet");
30   private String JavaDoc defaultPage_ ;
31   private String JavaDoc 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 JavaDoc {
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 JavaDoc
59   {
60     doRender(request, response) ;
61   }
62
63   public void doHelp(RenderRequest request, RenderResponse response)
64     throws PortletException, IOException JavaDoc
65   {
66     doRender(request, response) ;
67   }
68
69   public void doEdit(RenderRequest request, RenderResponse response)
70     throws PortletException, IOException JavaDoc
71   {
72     doRender(request, response) ;
73   }
74
75   public void doConfig(RenderRequest request, RenderResponse response)
76     throws PortletException, IOException JavaDoc
77   {
78     doRender(request, response) ;
79   }
80
81   protected void doRender(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
82     log_.debug( "$$$ dispatch to servlet acion $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
83     String JavaDoc path = "/struts-controller" ;
84     CustomRequestWrapper requestWrapper = null;
85
86     ExoPortletContext context = (ExoPortletContext) request.getPortletSession().getPortletContext();
87     boolean isSharedSessionEnable = context.isSessionShared();
88     requestWrapper = ((CustomRequestWrapper) ((HttpServletRequestWrapper JavaDoc)request).getRequest());
89     CustomResponseWrapper responseWrapper = (CustomResponseWrapper) ((HttpServletResponseWrapper JavaDoc)response).getResponse();
90     try {
91       PortletContextImpl portletContext = (PortletContextImpl) getPortletContext() ;
92       ServletContext JavaDoc scontext = portletContext.getWrappedServletContext() ;
93       RequestDispatcher JavaDoc dispatcher = null;
94       
95       String JavaDoc 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 JavaDoc 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 JavaDoc {
134
135   }
136 }
137
Popular Tags