1 4 package com.openedit.servlet; 5 6 import java.io.IOException ; 7 import java.io.OutputStream ; 8 import java.io.OutputStreamWriter ; 9 import java.io.Writer ; 10 import java.util.Iterator ; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 import javax.servlet.http.HttpSession ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import com.openedit.BaseWebPageRequest; 20 import com.openedit.ModuleManager; 21 import com.openedit.OpenEditException; 22 import com.openedit.Shutdownable; 23 import com.openedit.WebPageRequest; 24 import com.openedit.error.ErrorHandler; 25 import com.openedit.generators.Output; 26 import com.openedit.page.Page; 27 import com.openedit.page.PageRequestKeys; 28 import com.openedit.page.PageStreamer; 29 import com.openedit.page.manage.PageManager; 30 import com.openedit.util.PathUtilities; 31 import com.openedit.util.SessionMap; 32 import com.openedit.util.SessionTool; 33 import com.openedit.util.URLUtilities; 34 import com.openedit.web.Browser; 35 36 public class BaseOpenEditEngine implements OpenEditEngine 37 { 38 private static final Log log = LogFactory.getLog(BaseOpenEditEngine.class); 39 protected PageManager fieldPageManager; 40 protected ModuleManager fieldModuleManager; 41 protected String fieldWelcomePath; 42 protected ErrorHandler fieldErrorHandler; 43 protected String fieldVersion; 44 protected boolean fieldHideFolders = true; 45 46 public void render( HttpServletRequest inRequest, HttpServletResponse inResponse ) throws IOException , OpenEditException 47 { 48 checkEngineInit( inResponse ); 49 URLUtilities util = new URLUtilities(inRequest, inResponse); 50 51 String requestedPath = util.getOriginalPath(); 52 Page page = getPage( requestedPath ); 53 if ( page.isFolder() ) 54 { 55 String welcomePath = getWelcomePath(); 56 if (!page.getPath().endsWith("/")) 57 { 58 welcomePath = "/" + welcomePath; 59 } 60 page = getPage( page.getPath() + welcomePath ); 61 if ( !requestedPath.endsWith("/") && page.exists() ) 62 { 63 String contextPath = inRequest.getContextPath(); 64 if( contextPath == null ) 65 { 66 contextPath = ""; 67 } 68 inResponse.sendRedirect(contextPath + page.getPath()); 71 return; 72 } 73 } 74 75 if ( page.isHtml() ) 76 { 77 inRequest.setCharacterEncoding( page.getCharacterEncoding() ); 78 inResponse.setContentType( page.getMimeType() + "; charset=" + page.getCharacterEncoding() ); 79 inResponse.setHeader("Cache-Control","proxy-revalidate"); } 82 else 83 { 84 String mime = page.getMimeType(); 85 inResponse.setContentType( mime ); 86 } 87 88 WebPageRequest context = createWebPageRequest( page, inRequest, inResponse, util ); 89 beginRender(context); 90 91 } 92 93 96 public boolean hideFolders() 97 { 98 return fieldHideFolders; 99 } 100 101 104 public void setHideFolders( boolean inFlag ) 105 { 106 fieldHideFolders = inFlag; 107 } 108 109 protected Page getPage( String inPath ) throws OpenEditException 110 { 111 return getPageManager().getPage( inPath ); 112 } 113 116 public void beginRender(WebPageRequest pageRequest) throws OpenEditException 117 { 118 Page page = pageRequest.getPage(); 119 PageStreamer pageStreamer = null; 120 try 121 { 122 pageStreamer = createPageStreamer( page,pageRequest ); 123 executePathActions(pageRequest); 124 if( !pageRequest.hasRedirected()) 125 { 126 getModuleManager().executePageActions( page,pageRequest ); 127 } 128 if( !pageRequest.hasRedirected()) 129 { 130 pageStreamer.render(); 131 } 132 } 133 catch( Exception e ) 134 { 135 log.error(e); 136 e.printStackTrace(); 137 getErrorHandler().handleError( e, pageRequest ); 138 139 } 140 141 } 142 143 164 167 public PageStreamer createPageStreamer( Page inPage, WebPageRequest inPageRequest ) throws OpenEditException 168 { 169 PageStreamer pageStreamer = new PageStreamer(); 170 pageStreamer.setEngine( this ); 171 172 Output out = new Output(); 173 out.setWriter((Writer )inPageRequest.getPageValue(PageRequestKeys.OUTPUT_WRITER)); 174 out.setStream((OutputStream )inPageRequest.getPageValue(PageRequestKeys.OUTPUT_STREAM)); 175 176 pageStreamer.setOutput(out); 177 pageStreamer.setWebPageRequest( inPageRequest); 178 inPageRequest.putPageStreamer(pageStreamer ); 179 return pageStreamer; 180 } 181 182 183 protected void checkEngineInit( HttpServletResponse inResponse ) throws IOException  184 { 185 if ( getPageManager() == null ) 186 { 187 inResponse.getWriter().print( 188 "<html>Server is not initialized, please check the logs for errors</html>"); 189 return; 190 } 191 } 192 193 protected WebPageRequest createWebPageRequest( 194 Page inPage, 195 HttpServletRequest inRequest, 196 HttpServletResponse inResponse, URLUtilities util) throws OpenEditException 197 { 198 HttpSession session = inRequest.getSession(true); 199 BaseWebPageRequest context = new BaseWebPageRequest(); 200 context.putProtectedPageValue( PageRequestKeys.PAGE, inPage); 201 context.putProtectedPageValue(PageRequestKeys.CONTENT, inPage); 202 203 context.putProtectedPageValue( PageRequestKeys.REQUEST, inRequest); 205 context.putProtectedPageValue( PageRequestKeys.RESPONSE, inResponse); 206 context.putProtectedPageValue( PageRequestKeys.SESSION, session); 207 context.setRequest(inRequest); 208 context.setResponse(inResponse); 209 context.setSession(session); 210 211 context.putPageValues(new SessionMap(session)); 213 try 214 { 215 OutputStream os = inResponse.getOutputStream(); 216 context.putProtectedPageValue(PageRequestKeys.OUTPUT_STREAM, os); 217 String encoding = inPage.getCharacterEncoding(); 219 if ( encoding != null) 220 { 221 Writer writer = new OutputStreamWriter (os,encoding); 223 context.putProtectedPageValue(PageRequestKeys.OUTPUT_WRITER, writer); 224 } 225 else 226 { 227 Writer writer = new OutputStreamWriter (os ); context.putProtectedPageValue(PageRequestKeys.OUTPUT_WRITER, writer); 229 } 230 } 231 catch (IOException ex) 232 { 233 log.error( ex ); 234 throw new OpenEditException(ex); 235 } 236 context.putProtectedPageValue(PageRequestKeys.URL_UTILITIES, util); 238 context.putProtectedPageValue( PageRequestKeys.WEB_SERVER_PATH, util.buildRoot() ); 239 240 context.putProtectedPageValue(PageRequestKeys.HOME, util.relativeHomePrefix()); 241 242 243 if( inPage.isHtml() ) { 245 context.putProtectedPageValue( "version", getVersion()); 246 247 String path = PathUtilities.extractDirectoryPath(inPage.getPath()); 249 context.putProtectedPageValue(PageRequestKeys.URL_PATH, util.relativeHomePrefix() + path); 250 251 context.putProtectedPageValue(PageRequestKeys.FILE_PATH, path); 253 Browser browser = new Browser(inRequest.getHeader("User-Agent")); 255 browser.setLocale( inRequest.getLocale() ); 256 context.putProtectedPageValue(PageRequestKeys.BROWSER, browser); 257 258 SessionTool sessionTool = new SessionTool( context, getModuleManager() ); 261 context.putProtectedPageValue(PageRequestKeys.CLASSTOOL, sessionTool ); 262 } 263 264 return context; 265 } 266 267 273 276 public void executePageActions( WebPageRequest inPageRequest ) throws OpenEditException 277 { 278 getModuleManager().executePageActions( inPageRequest.getPage(),inPageRequest ); 279 } 280 283 public void executePathActions( WebPageRequest inPageRequest ) throws OpenEditException 284 { 285 getModuleManager().executePathActions( inPageRequest.getPage(), inPageRequest ); 286 } 287 288 291 public ModuleManager getModuleManager() 292 { 293 return fieldModuleManager; 294 } 295 298 public void setModuleManager( ModuleManager moduleManager ) 299 { 300 fieldModuleManager = moduleManager; 301 } 302 305 public PageManager getPageManager() 306 { 307 return fieldPageManager; 308 } 309 312 public void setPageManager( PageManager pageManager ) 313 { 314 fieldPageManager = pageManager; 315 } 316 339 340 343 public String getWelcomePath() 344 { 345 return fieldWelcomePath; 346 } 347 350 public void setWelcomePath( String welcomePath ) 351 { 352 fieldWelcomePath = welcomePath; 353 } 354 357 public ErrorHandler getErrorHandler() 358 { 359 return fieldErrorHandler; 360 } 361 364 public void setErrorHandler( ErrorHandler errorHandler ) 365 { 366 fieldErrorHandler = errorHandler; 367 } 368 371 381 public String getVersion() 382 { 383 if (fieldVersion == null) 384 { 385 Package thisPackage = getClass().getPackage(); 386 if (thisPackage != null) 387 { 388 fieldVersion = thisPackage.getImplementationVersion(); 389 } 390 if (fieldVersion == null) 391 { 392 fieldVersion = "dev"; 393 } 394 } 395 return fieldVersion; 396 } 397 398 401 public void shutdown() 402 { 403 for (Iterator iter = getModuleManager().getLoadedBeans().iterator(); iter.hasNext();) 404 { 405 Object module = (Object ) iter.next(); 406 if( module instanceof Shutdownable) 407 { 408 ((Shutdownable)module).shutdown(); 409 } 410 } 411 } 412 413 } 414
| Popular Tags
|