View Javadoc
1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: VelocityInterceptor.java,v 1.13 2003/03/05 04:31:57 michaelh Exp $ 20 */ 21 22 package juju.reattore.server.intercept.impl; 23 24 import org.apache.velocity.app.VelocityEngine; 25 import org.apache.velocity.VelocityContext; 26 import org.apache.velocity.exception.*; 27 28 import org.apache.commons.logging.*; 29 30 import java.io.*; 31 32 import juju.reattore.protocol.http.*; 33 import juju.reattore.server.intercept.Interceptor; 34 import juju.reattore.io.impl.ByteBufferSource; 35 36 /*** Interceptor that serves a Apache Velocity based template. 37 38 @tag velocity 39 @group Interceptor 40 41 @todo Minor: Uses ByteArrayOutputStream, which could be replaced with a Source. 42 @todo Doesn't configure the velocity engine. 43 */ 44 public class VelocityInterceptor 45 implements Interceptor { 46 47 private static Log log = LogFactory.getLog(VelocityInterceptor.class); 48 49 private VelocityEngine engine; 50 private String lockedPath; 51 52 /*** Create a new interceptor 53 54 @todo Minor: Doesn't handle errors during init very well. 55 */ 56 public VelocityInterceptor() { 57 engine = createEngine(); 58 59 try { 60 engine.init(); 61 } 62 catch (Exception ex) { 63 log.error("While initialising", ex); 64 throw new RuntimeException(ex); 65 } 66 } 67 68 private static VelocityEngine createEngine() { 69 VelocityEngine ret = new VelocityEngine(); 70 71 return ret; 72 } 73 74 /*** Lock this interceptor to only ever return the contents of the 75 given path no matter what the request. 76 77 @param path The path to lock to, or null to clear. 78 */ 79 public void setLockedPath(String path) { 80 lockedPath = path; 81 } 82 83 /*** Hook to allow a derrived class to add extra items to the 84 context. 85 86 @param ctx Main context 87 @param req Incoming request 88 @param resp Outgoing response 89 90 @return true if the request was handled and processing should 91 abort. 92 */ 93 protected boolean populateContext(VelocityContext ctx, HttpRequest req, HttpResponse resp) { 94 return false; 95 } 96 97 /*** @see Interceptor 98 99 @todo populateContext() is a bit of a hack 100 */ 101 public boolean process(HttpRequest req, HttpResponse resp) { 102 VelocityContext ctx = new VelocityContext(); 103 104 ctx.put("resp", resp); 105 ctx.put("req", req); 106 107 if (populateContext(ctx, req, resp) == true) { 108 /* Handled already */ 109 return true; 110 } 111 112 String path = lockedPath != null ? lockedPath : req.getPath(); 113 ByteArrayOutputStream out = new ByteArrayOutputStream(); 114 OutputStreamWriter writer = new OutputStreamWriter(out); 115 116 try { 117 if (engine.mergeTemplate(path, ctx, 118 writer) == true) { 119 120 writer.flush(); 121 122 /* All done */ 123 resp.setBody(new ByteBufferSource(out.toByteArray())); 124 resp.setStatus(HttpResponse.SC_OK); 125 resp.setHeader(HttpConstants.CONTENT_TYPE, HttpConstants.TEXT_HTML); 126 127 return true; 128 } 129 else { 130 log.debug("Template could not process " + path); 131 resp.setStatus(HttpResponse.SC_NOT_FOUND); 132 return false; 133 } 134 } 135 catch (ResourceNotFoundException ex) { 136 log.debug("Error while processing " + path, ex); 137 resp.setStatus(HttpResponse.SC_NOT_FOUND); 138 return false; 139 } 140 catch (ParseErrorException ex) { 141 log.debug("Error while processing " + path, ex); 142 resp.setStatus(HttpResponse.SC_INTERNAL_SERVER_ERROR); 143 return false; 144 } 145 catch (MethodInvocationException ex) { 146 log.debug("Error while processing " + path, ex); 147 resp.setStatus(HttpResponse.SC_INTERNAL_SERVER_ERROR); 148 return false; 149 } 150 catch (Exception ex) { 151 log.debug("Error while processing " + path, ex); 152 resp.setStatus(HttpResponse.SC_INTERNAL_SERVER_ERROR); 153 return false; 154 } 155 } 156 }

This page was automatically generated by Maven