001: /*
002: * Handler.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.webserver;
054:
055: import seda.sandStorm.core.*;
056: import seda.sandStorm.lib.http.httpRequest;
057:
058: import com.rimfaxe.webserver.seda.SedaHttpResponse;
059: import com.rimfaxe.webserver.webapp.*;
060: import com.rimfaxe.webserver.servletapi.RimfaxeFilterChain;
061:
062: import java.util.*;
063:
064: /**
065: *
066: * @author Lars Andersen
067: */
068: public class Handler {
069: com.rimfaxe.webserver.servletapi.ServletFactory factory = com.rimfaxe.webserver.servletapi.ServletFactory
070: .getInstance();
071: Configuration conf = null;
072: long age;
073:
074: StringBuffer error_log = null;
075:
076: /** Creates a new instance of Handler */
077: public Handler(Configuration conf) {
078: this .age = System.currentTimeMillis();
079: this .conf = conf;
080: }
081:
082: public void setErrorLog(StringBuffer buf) {
083: this .error_log = buf;
084: }
085:
086: public VirtualHost lookupVirtualHost(httpRequest req) {
087:
088: String urlpath = req.getURL();
089: String host = req.getHeader("Host");
090: String port = ""
091: + req.getConnection().getConnection().getServerSocket()
092: .getLocalPort();
093: String hostname = host;
094:
095: if (host == null)
096: host = "default:" + port;
097:
098: if (error_log != null)
099: error_log.append("Handler, lookupVirtualHost, host=" + host
100: + "\n");
101:
102: int colon = host.indexOf(':');
103:
104: if (colon > 0) {
105: hostname = host.substring(0, colon);
106: port = host.substring(colon + 1);
107: } else {
108: hostname = host;
109: String tmp = "http";
110: if (tmp.equalsIgnoreCase("http"))
111: port = "80";
112: if (tmp.equalsIgnoreCase("https"))
113: port = "443";
114: }
115:
116: VirtualHost vhost = conf.getVirtualHost(hostname.toString()
117: .trim()
118: + ":" + port);
119:
120: if (vhost == null) {
121: port = ""
122: + req.getConnection().getConnection()
123: .getServerSocket().getLocalPort();
124:
125: vhost = conf.getVirtualHost("default:" + port);
126: }
127:
128: if (vhost == null) {
129: if (error_log != null)
130: error_log.append("Handler, no vhost??\n");
131:
132: return null;
133: }
134: return vhost;
135: }
136:
137: public boolean isCacheable(httpRequest req) {
138:
139: if (req.getURL().startsWith("/RWS_ADMIN"))
140: return false;
141: if (!req.getMethod().equalsIgnoreCase("GET"))
142: return false;
143: if (req.hasHeader("If-Modified-Since"))
144: return false;
145: if (req.hasHeader("If-Range"))
146: return false;
147:
148: String urlpath = req.getURL();
149: VirtualHost vhost = lookupVirtualHost(req);
150:
151: if (vhost != null) {
152: WebContext webcontext = vhost.getWebContext(urlpath);
153:
154: req.setSpecial("ROOT_DIR", webcontext.getRoot());
155: req
156: .setSpecial("WEBAPP_ROOT", webcontext
157: .getUrlpathPrefix());
158:
159: return webcontext.isStatic(req);
160: } else {
161: return false;
162: }
163:
164: }
165:
166: public SedaHttpResponse perform(httpRequest req) {
167:
168: String urlpath = req.getURL();
169:
170: //if (urlpath.startsWith("/servlet"))
171: //{
172: // System.out.println("Request dump \n"+req);
173: //}
174:
175: VirtualHost vhost = lookupVirtualHost(req);
176:
177: if (vhost != null) {
178:
179: WebContext webcontext = vhost.getWebContext(urlpath);
180:
181: req.setURL(webcontext.processUrlPath(req.getURL()));
182:
183: webcontext.getWebApp().reload();
184:
185: if (vhost.getDebug()) {
186: com.rimfaxe.webserver.DebugLog.log("Handler", 192,
187: "Debug messages", "" + req + "\n" + error_log
188: + "\n");
189: }
190:
191: return getResponse(req, vhost, webcontext);
192: } else {
193: if (error_log != null)
194: error_log.append("Handler, no virtualhost defined?\n");
195: return null;
196: }
197:
198: }
199:
200: public SedaHttpResponse getResponse(httpRequest req,
201: VirtualHost vh, WebContext webcontext) {
202:
203: String urlpath = req.getURL();
204:
205: RimfaxeFilterChain rfc = new RimfaxeFilterChain(req, vh,
206: webcontext, conf, factory);
207:
208: SedaHttpResponse response = null;
209: try {
210:
211: response = rfc.runservice();
212: } catch (javax.servlet.ServletException se) {
213: // TODO
214: if (error_log != null)
215: error_log.append("Handler, ServletEception\n");
216: } catch (java.io.IOException ioe) {
217: // TODO
218: if (error_log != null)
219: error_log.append("Handler, IOException\n");
220: }
221:
222: return response;
223: }
224:
225: public SedaHttpResponse callServlet(String name, httpRequest req,
226: VirtualHost vh, WebContext webcontext) {
227:
228: String urlpath = req.getURL();
229:
230: Servlet s = webcontext.getWebApp().getServlet(name);
231: if (s == null)
232: s = conf.getWebApp().getServlet(name);
233:
234: String cls = s.getClassName();
235:
236: com.rimfaxe.webserver.servletapi.ServletWrapper swrap = factory
237: .getWrapper(vh, webcontext, name, cls, urlpath, s);
238:
239: SedaHttpResponse response = null;
240: try {
241:
242: response = swrap.runservice(req, webcontext);
243: } catch (javax.servlet.ServletException se) {
244: se.printStackTrace(System.out);
245: } catch (java.io.IOException ioe) {
246: ioe.printStackTrace(System.out);
247: }
248:
249: return response;
250: }
251:
252: public long getAge() {
253: return System.currentTimeMillis() - age;
254: }
255:
256: public void reset() {
257: }
258: }
|