KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > servlet > CoreServlet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.servlet;
10
11 import org.jboss.portal.common.util.Tools;
12 import org.jboss.portal.core.CoreConstants;
13 import org.jboss.portal.core.CorePortal;
14 import org.jboss.portal.core.output.AuthenticateResult;
15 import org.jboss.portal.core.plugins.page.Page;
16 import org.jboss.portal.core.plugins.page.PageRepository;
17 import org.jboss.portal.core.plugins.page.WindowLocation;
18 import org.jboss.portal.server.LayoutServer;
19 import org.jboss.portal.server.PortalRequest;
20 import org.jboss.portal.server.PortalResponse;
21 import org.jboss.portal.server.PortalServer;
22 import org.jboss.portal.server.ServerObjectID;
23 import org.jboss.portal.server.ThemeServer;
24 import org.jboss.portal.server.Window;
25 import org.jboss.portal.server.WindowContext;
26 import org.jboss.portal.server.output.ActionResult;
27 import org.jboss.portal.server.output.ErrorResult;
28 import org.jboss.portal.server.output.FragmentResult;
29 import org.jboss.portal.server.output.HTTPRedirectionResult;
30 import org.jboss.portal.server.output.NoResult;
31 import org.jboss.portal.server.output.Result;
32 import org.jboss.portal.server.output.SecurityErrorResult;
33 import org.jboss.portal.server.output.StreamResult;
34 import org.jboss.portal.server.servlet.AbstractMainServlet;
35 import org.jboss.portal.server.theme.Item;
36 import org.jboss.portal.server.theme.LayoutConstants;
37 import org.jboss.portal.server.theme.LayoutDispatcher;
38 import org.jboss.portal.server.theme.PageHolder;
39 import org.jboss.portal.server.theme.PortalLayout;
40 import org.jboss.portal.server.theme.Region;
41 import org.jboss.portal.server.theme.RegionSet;
42 import org.jboss.portal.server.theme.ThemeConstants;
43
44 import javax.servlet.ServletException JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.io.Writer JavaDoc;
48 import java.util.Iterator JavaDoc;
49
50 /**
51  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
52  * @version $Revision: 1.25 $
53  */

54 public class CoreServlet extends AbstractMainServlet
55 {
56
57    public void init() throws ServletException JavaDoc
58    {
59       super.init();
60    }
61
62    protected void render(PortalRequest req, PortalResponse resp) throws IOException JavaDoc, ServletException JavaDoc
63    {
64       Result tmp = resp.getTargetResult();
65       if (tmp instanceof HTTPRedirectionResult)
66       {
67          HTTPRedirectionResult redirection = (HTTPRedirectionResult)tmp;
68          resp.sendRedirect(redirection.getLocation());
69       }
70       else if (tmp == null || tmp instanceof ActionResult || tmp instanceof NoResult)
71       {
72          RegionSet regions = new RegionSet();
73          for (Iterator JavaDoc i = resp.getRenderResults(); i.hasNext();)
74          {
75             Result result = (Result)i.next();
76
77             // Convert the error result into a fragment
78
if (result instanceof SecurityErrorResult)
79             {
80                SecurityErrorResult ser = (SecurityErrorResult)result;
81                FragmentResult fragment = new FragmentResult(ser.getProducer());
82                fragment.setContentType("text/html");
83                Writer JavaDoc writer = fragment.getWriter();
84                writer.write("<div>You are not authorized to perform this operation</div>");
85                writer.close();
86                result = fragment;
87                Throwable JavaDoc throwable = ser.getThrowable();
88                if (throwable != null)
89                {
90                   log.error("Security issue", throwable);
91                }
92                else
93                {
94                   log.error("Security issue");
95                }
96             }
97             else if (result instanceof ErrorResult)
98             {
99                ErrorResult error = (ErrorResult)result;
100                Throwable JavaDoc throwable = error.getThrowable();
101                FragmentResult fragment = new FragmentResult(error.getProducer());
102                fragment.setContentType("text/html");
103                Writer JavaDoc writer = fragment.getWriter();
104                writer.write("<div><pre style=\"text-align:left;\"><code>");
105                writer.write(throwable.toString() + "\n");
106                StackTraceElement JavaDoc[] elts = throwable.getStackTrace();
107                for (int j = 0; j < elts.length; j++)
108                {
109                   StackTraceElement JavaDoc elt = elts[j];
110                   writer.write("\tat " + elt + "\n");
111                }
112                writer.write("</code></pre></div>");
113                writer.close();
114                result = fragment;
115                if (throwable != null)
116                {
117                   log.error("Error", throwable);
118                }
119                else
120                {
121                   log.error("Error");
122                }
123             }
124
125             if (result instanceof FragmentResult)
126             {
127                FragmentResult fragment = (FragmentResult)result;
128                WindowContext windowContext = (WindowContext)fragment.getProducer();
129                ServerObjectID id = windowContext.getID();
130                PortalServer container = req.getServer();
131                Window window = (Window)container.getObject(id);
132                CorePortal portal = (CorePortal)window.getPortal();
133                PageRepository repo = portal.getPageRepository();
134                Page page = repo.getPageByWindow(id);
135                WindowLocation ref = (WindowLocation)page.getLocation(window.getID());
136                int height = ref.getHeight();
137                Region region = regions.getRegion(ref.getRegion());
138                if (region == null)
139                {
140                   region = new Region(ref.getRegion());
141                   regions.addRegion(region);
142                }
143                region.add(new Item(height, fragment));
144             }
145          }
146
147          PageHolder page = new PageHolder(regions);
148          req.setAttribute("PAGE", page);
149          req.setAttribute("REQ", req);
150          req.setAttribute("RESP", resp);
151
152          // handle the theme
153
publishThemeServerToRequest(req);
154          // now check for the defined renderSet (if any)
155
publishRenderSetToRequest(req);
156
157          // handle the layout
158
getLayoutAndDelegate(req, resp);
159       }
160       else if (tmp instanceof ErrorResult)
161       {
162          // This an error that occured during the action request
163
ErrorResult error = (ErrorResult)tmp;
164          String JavaDoc message = "An error occured during the action request : " + error.getMessage();
165          Throwable JavaDoc throwable = error.getThrowable();
166          if (throwable == null)
167          {
168             log.error(message);
169             throw new ServletException JavaDoc(message);
170          }
171          else
172          {
173             log.error(message, throwable);
174             throw new ServletException JavaDoc(message, error.getThrowable());
175          }
176       }
177       else if (tmp instanceof AuthenticateResult)
178       {
179          AuthenticateResult result = (AuthenticateResult)tmp;
180          String JavaDoc url = req.getContextPath() + "/login?redirect=" + Tools.createXWWWFormURLEncoded(result.getTarget().toString());
181          resp.sendRedirect(url);
182       }
183       else if (tmp instanceof StreamResult)
184       {
185          OutputStream JavaDoc out = null;
186          try
187          {
188             StreamResult result = (StreamResult)tmp;
189             String JavaDoc contentType = result.getContentType();
190             byte[] bytes = result.getBytes();
191
192             // How long images are fresh
193
long howLong = 10 * 60 * 1000;
194
195             // Set content type and length
196
resp.setContentLength(bytes.length);
197             resp.setContentType(contentType);
198
199             // If content type is an image, css or pdf set them as cached
200
if (contentType.startsWith("image/") ||
201                 "text/css".equals(contentType) ||
202                 "application/x-shockwave-flash".equals(contentType))
203             {
204                // cache 15 mn
205
resp.setDateHeader("Expires", System.currentTimeMillis() + howLong);
206                resp.setHeader("Cache-Control", "max-age=" + howLong / 1000 + ", " + "s-maxage=" + howLong / 1000);
207             }
208
209             // Set the filename so the client knows it
210
// resp.addHeader( "content-disposition", "attachment; filename=\"" + res.getName()+"\"");
211

212             // Push the bytes
213
out = resp.getOutputStream();
214             out.write(bytes);
215          }
216          finally
217          {
218             Tools.safeClose(out);
219          }
220       }
221       else
222       {
223          throw new ServletException JavaDoc("Unknown result " + tmp);
224       }
225    }
226
227    private void getLayoutAndDelegate(PortalRequest req, PortalResponse resp)
228       throws ServletException JavaDoc, IOException JavaDoc
229    {
230       String JavaDoc layoutURI = null;
231       PortalLayout layout = (PortalLayout)req.getAttribute(LayoutConstants.PARAM_LAYOUT);
232       // if the layout was set via the layout strategy (interceptor)
233
if (layout != null)
234       {
235          // then get the uri that was set by the strategy as well
236
layoutURI = (String JavaDoc)req.getAttribute(LayoutConstants.PARAM_LAYOUT_URI);
237          log.debug("using layout returned from the strategy: " + layout.getName() + " uri: " + layoutURI);
238       }
239       else
240       {
241          // nothing came via the strategy, so we have to handle it here
242
String JavaDoc layoutName = req.getParameter(LayoutConstants.PARAM_LAYOUT);
243          if (layoutName == null)
244          {
245             layoutName = (String JavaDoc)req.getPortal().getProperties().get(CoreConstants.PORTAL_PROP_LAYOUT);
246          }
247
248          if (layoutName == null)
249          {
250             layoutName = "nodesk";
251          }
252          log.debug("requesting layout: " + layoutName);
253
254          LayoutServer layoutServer = req.getServer().getManager().getLayoutServer();
255          layout = layoutServer.getLayout(layoutName, true);
256
257          if (layout != null)
258          {
259             layoutURI = layout.getURI();
260          }
261          else
262          {
263             throw new ServletException JavaDoc("No valid Layout was found for this request; layout name: " + layoutName);
264          }
265          log.debug("using layout (not via strategy): " + layout.getName() + " uri: " + layoutURI);
266       }
267       log.debug("got layout to use [" + layout.getName() + "] in app [" + layout.getAppName() + "] uri [" + layout.getURI() + "]");
268
269       // Perform dispatch
270
LayoutDispatcher dispatcher = new LayoutDispatcher(layout, layoutURI);
271       dispatcher.include(req, resp);
272    }
273
274    // check for the defined renderSet (if any)
275
private void publishRenderSetToRequest(PortalRequest req)
276    {
277       String JavaDoc renderSetName = (String JavaDoc)req.getPortal().getProperties().get(CoreConstants.PORTAL_PROP_RENDERSET);
278       if (renderSetName == null)
279       {
280          // default to the divRenderer, if no renderSet was specified in the portal (as property)
281
renderSetName = "divRenderer";
282       }
283       req.setAttribute(LayoutConstants.ATTR_RENDERSET, renderSetName);
284    }
285
286    // pass the theme server and the theme name on to anyone that is interested down stream (jsp/portlet)
287
private void publishThemeServerToRequest(PortalRequest request)
288    {
289       ThemeServer themeServer = request.getServer().getManager().getThemeServer();
290       if (themeServer != null)
291       {
292          request.setAttribute(ThemeConstants.ATTR_THEMESERVER, themeServer);
293
294          String JavaDoc themeName = request.getParameter(ThemeConstants.PARAM_THEME);
295          if (themeName == null)
296          {
297             themeName = (String JavaDoc)request.getPortal().getProperties().get(CoreConstants.PORTAL_PROP_THEME);
298          }
299          if (themeName != null)
300          {
301             request.setAttribute(ThemeConstants.ATTR_THEME, themeName);
302          }
303          log.debug("requesting theme: " + themeName);
304       }
305    }
306 }
307
Popular Tags