1 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 ; 45 import java.io.IOException ; 46 import java.io.OutputStream ; 47 import java.io.Writer ; 48 import java.util.Iterator ; 49 50 54 public class CoreServlet extends AbstractMainServlet 55 { 56 57 public void init() throws ServletException 58 { 59 super.init(); 60 } 61 62 protected void render(PortalRequest req, PortalResponse resp) throws IOException , ServletException 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 i = resp.getRenderResults(); i.hasNext();) 74 { 75 Result result = (Result)i.next(); 76 77 if (result instanceof SecurityErrorResult) 79 { 80 SecurityErrorResult ser = (SecurityErrorResult)result; 81 FragmentResult fragment = new FragmentResult(ser.getProducer()); 82 fragment.setContentType("text/html"); 83 Writer writer = fragment.getWriter(); 84 writer.write("<div>You are not authorized to perform this operation</div>"); 85 writer.close(); 86 result = fragment; 87 Throwable 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 throwable = error.getThrowable(); 101 FragmentResult fragment = new FragmentResult(error.getProducer()); 102 fragment.setContentType("text/html"); 103 Writer writer = fragment.getWriter(); 104 writer.write("<div><pre style=\"text-align:left;\"><code>"); 105 writer.write(throwable.toString() + "\n"); 106 StackTraceElement [] elts = throwable.getStackTrace(); 107 for (int j = 0; j < elts.length; j++) 108 { 109 StackTraceElement 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 publishThemeServerToRequest(req); 154 publishRenderSetToRequest(req); 156 157 getLayoutAndDelegate(req, resp); 159 } 160 else if (tmp instanceof ErrorResult) 161 { 162 ErrorResult error = (ErrorResult)tmp; 164 String message = "An error occured during the action request : " + error.getMessage(); 165 Throwable throwable = error.getThrowable(); 166 if (throwable == null) 167 { 168 log.error(message); 169 throw new ServletException (message); 170 } 171 else 172 { 173 log.error(message, throwable); 174 throw new ServletException (message, error.getThrowable()); 175 } 176 } 177 else if (tmp instanceof AuthenticateResult) 178 { 179 AuthenticateResult result = (AuthenticateResult)tmp; 180 String url = req.getContextPath() + "/login?redirect=" + Tools.createXWWWFormURLEncoded(result.getTarget().toString()); 181 resp.sendRedirect(url); 182 } 183 else if (tmp instanceof StreamResult) 184 { 185 OutputStream out = null; 186 try 187 { 188 StreamResult result = (StreamResult)tmp; 189 String contentType = result.getContentType(); 190 byte[] bytes = result.getBytes(); 191 192 long howLong = 10 * 60 * 1000; 194 195 resp.setContentLength(bytes.length); 197 resp.setContentType(contentType); 198 199 if (contentType.startsWith("image/") || 201 "text/css".equals(contentType) || 202 "application/x-shockwave-flash".equals(contentType)) 203 { 204 resp.setDateHeader("Expires", System.currentTimeMillis() + howLong); 206 resp.setHeader("Cache-Control", "max-age=" + howLong / 1000 + ", " + "s-maxage=" + howLong / 1000); 207 } 208 209 212 out = resp.getOutputStream(); 214 out.write(bytes); 215 } 216 finally 217 { 218 Tools.safeClose(out); 219 } 220 } 221 else 222 { 223 throw new ServletException ("Unknown result " + tmp); 224 } 225 } 226 227 private void getLayoutAndDelegate(PortalRequest req, PortalResponse resp) 228 throws ServletException , IOException 229 { 230 String layoutURI = null; 231 PortalLayout layout = (PortalLayout)req.getAttribute(LayoutConstants.PARAM_LAYOUT); 232 if (layout != null) 234 { 235 layoutURI = (String )req.getAttribute(LayoutConstants.PARAM_LAYOUT_URI); 237 log.debug("using layout returned from the strategy: " + layout.getName() + " uri: " + layoutURI); 238 } 239 else 240 { 241 String layoutName = req.getParameter(LayoutConstants.PARAM_LAYOUT); 243 if (layoutName == null) 244 { 245 layoutName = (String )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 ("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 LayoutDispatcher dispatcher = new LayoutDispatcher(layout, layoutURI); 271 dispatcher.include(req, resp); 272 } 273 274 private void publishRenderSetToRequest(PortalRequest req) 276 { 277 String renderSetName = (String )req.getPortal().getProperties().get(CoreConstants.PORTAL_PROP_RENDERSET); 278 if (renderSetName == null) 279 { 280 renderSetName = "divRenderer"; 282 } 283 req.setAttribute(LayoutConstants.ATTR_RENDERSET, renderSetName); 284 } 285 286 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 themeName = request.getParameter(ThemeConstants.PARAM_THEME); 295 if (themeName == null) 296 { 297 themeName = (String )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 |