001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.webserver.plumbing;
027:
028: import EDU.oswego.cs.dl.util.concurrent.Channel;
029: import junit.framework.Assert;
030: import org.jicarilla.http.HTTPEncoding;
031: import org.jicarilla.http.HTTPException;
032: import org.jicarilla.net.Event;
033: import org.jicarilla.plumbing.DefaultStage;
034: import org.jicarilla.plumbing.Sink;
035:
036: import java.io.IOException;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: /**
041: * @todo ContentNegotiationStage that handles Expect/100-Continue, etc (8.2.3)
042: * @todo HostFinderStage that determines the host (5.2)
043: * @todo ErrorMessageStage that adds a (configurable) message body on errors
044: * @todo EntityHeaderStage that adds entity headers
045: * @todo ConfigurationScreener that sends events to other pipes based on configuration (selectors on host, uri, version, method, etc)
046: * @todo OptionsStage that responds to OPTIONS
047: * @todo UnsupportedOperationStage that sends 501
048: * @todo CacheHeaderStage that adds caching stuff (13)
049: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
050: * @version $Id: AbstractStage.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
051: */
052: public abstract class AbstractStage extends DefaultStage {
053: public final static String EXCEPTION_CONTEXT_KEY = "urn:jicarilla:http:context-key:"
054: + AbstractStage.class.getPackage().getName() + "exception";
055:
056: protected AbstractStage(final Channel channel,
057: final Sink errorHandler) {
058: super (channel, errorHandler);
059: }
060:
061: public void put(final Object o) throws InterruptedException {
062: final HTTPEvent e = (HTTPEvent) o;
063: try {
064: process(e);
065: super .put(e);
066: } catch (HTTPException he) {
067: handleException(e, he);
068: } catch (IOException ioe) {
069: handleException(e, new HTTPException(
070: HTTPEncoding.STATUS_999_IO_Problem, ioe));
071: }
072: }
073:
074: public boolean offer(final Object o, final long l)
075: throws InterruptedException {
076: final HTTPEvent e = (HTTPEvent) o;
077: try {
078: process(e);
079: final boolean result = super .offer(e, l);
080: return result;
081: } catch (HTTPException he) {
082: handleException(e, he);
083: } catch (IOException ioe) {
084: handleException(e, new HTTPException(
085: HTTPEncoding.STATUS_999_IO_Problem, ioe));
086: }
087:
088: return true;
089: }
090:
091: protected void process(final HTTPEvent event) throws HTTPException,
092: IOException {
093: }
094:
095: public void handleException(final HTTPEvent event, final Throwable t)
096: throws InterruptedException {
097: Assert.assertNotNull(event);
098:
099: ensureContext(event);
100: final Map m = event.getContext();
101: if (!m.containsKey(EXCEPTION_CONTEXT_KEY)) {
102: m.put(EXCEPTION_CONTEXT_KEY, t);
103:
104: if (t instanceof HTTPException) {
105: final HTTPException he = (HTTPException) t;
106: event.getHTTPResponse().setStatusCode(he.getCode());
107: }
108: }
109: handleError(event);
110: }
111:
112: protected static void ensureContext(final Event event) {
113: Assert.assertNotNull(event);
114:
115: if (event.getContext() == null)
116: event.setContext(new HashMap());
117: }
118:
119: public static boolean checkExceptionOccured(final Event event) {
120: Assert.assertNotNull(event);
121:
122: //ensureContext( event );
123: final Map m = event.getContext();
124: if (m != null && m.containsKey(EXCEPTION_CONTEXT_KEY))
125: return true;
126:
127: return false;
128: }
129:
130: public static Throwable getThrowable(final Event event) {
131: Assert.assertNotNull(event);
132: //ensureContext( event );
133:
134: final Map m = event.getContext();
135: if (m != null && m.containsKey(EXCEPTION_CONTEXT_KEY))
136: return (Throwable) m.get(EXCEPTION_CONTEXT_KEY);
137:
138: return null;
139: }
140: }
|