1 19 20 package za.org.coefficient.servlet; 21 22 import za.org.coefficient.core.CoefficientInterceptor; 23 import za.org.coefficient.core.CoefficientWebContext; 24 import za.org.coefficient.core.Constants; 25 import za.org.coefficient.interfaces.CoefficientContext; 26 27 import java.io.BufferedReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 53 public class CoefficientServlet extends HttpServlet { 54 55 57 private CoefficientInterceptor interceptor; 58 59 61 public void addInterceptor(CoefficientInterceptor val) { 62 if (val == null) { 63 throw new IllegalArgumentException ("Cannot add null interceptor"); 64 } 65 if (this.interceptor == null) { 66 this.interceptor = val; 67 } else { 68 this.interceptor.append(val); 69 } 70 } 71 72 77 public void destroy() { 78 } 79 80 91 public void init(ServletConfig cfg) throws ServletException { 92 super.init(cfg); 94 } 95 96 107 protected void doGet(HttpServletRequest request, 108 HttpServletResponse response) throws ServletException , IOException { 109 String pathInfo = request.getPathInfo(); 110 String servletPath = request.getServletPath(); 111 String contextPath = request.getContextPath(); 112 String path = 113 (pathInfo == null) ? servletPath : (servletPath + pathInfo); 114 115 if ("/index.html".equals(path) || 116 "/".equals(path)) { 117 response.setContentType("text/html"); 118 execute(request, response); 119 return; 120 } 121 response.sendError(HttpServletResponse.SC_NOT_FOUND); 122 } 123 124 130 protected void doPost(HttpServletRequest request, 131 HttpServletResponse response) throws ServletException , IOException { 132 doGet(request, response); 133 } 134 135 145 private void execute(HttpServletRequest request, 146 HttpServletResponse response) throws ServletException , IOException { 147 try { 148 if (interceptor == null) { 149 initInterceptors(); 150 } 151 CoefficientWebContext ctx = new CoefficientWebContext(request, response); 152 interceptor.invoke(ctx); 153 ctx.synchronize(request, response); 156 157 ctx.getPage().process(response.getWriter()); 159 } catch (IOException e) { 160 throw e; 161 } catch (Exception e) { 162 throw new ServletException (e); 163 } 164 } 165 166 private synchronized void initInterceptors() { 167 if (interceptor == null) { 168 InputStream is = 169 this.getServletContext() 170 .getResourceAsStream("/WEB-INF/interceptors.properties"); 171 try { 172 BufferedReader r = 173 new BufferedReader (new InputStreamReader (is)); 174 String line = r.readLine(); 175 while (line != null) { 176 if (((line = line.trim()).length() > 0) 178 && !line.startsWith("#")) { 179 Class cls = Class.forName(line); 180 addInterceptor((CoefficientInterceptor) cls.newInstance()); 181 } 182 183 line = r.readLine(); 185 } 186 } catch (Exception e) { 187 } finally { 189 try { 190 if (is != null) { 191 is.close(); 192 } 193 } catch (IOException e) { 194 } 195 196 } 198 } 199 } 200 } 201 | Popular Tags |