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 org.jicarilla.http.HTTPException;
030: import org.jicarilla.lang.Assert;
031: import org.jicarilla.plumbing.Sink;
032: import org.mortbay.http.HttpConnection;
033: import org.mortbay.http.HttpListener;
034: import org.mortbay.http.HttpMessage;
035: import org.mortbay.http.HttpRequest;
036: import org.mortbay.http.HttpServer;
037:
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.io.OutputStream;
041: import java.net.InetAddress;
042: import java.net.UnknownHostException;
043: import java.nio.channels.SocketChannel;
044:
045: /**
046: * Delegates request/response handling to Jetty completely. If you
047: * use this stage, it should be the only one.
048: *
049: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
050: * @version $Id: JettyStage.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
051: */
052: public class JettyStage extends AbstractStage {
053: public final static int DEFAULT_BUFFER_SIZE = 8096;
054:
055: protected HttpServer m_delegate;
056: protected InetAddress m_address;
057:
058: public JettyStage(final HttpServer delegate,
059: final InetAddress address, final Channel channel,
060: final Sink errorHandler) {
061: super (channel, errorHandler);
062:
063: Assert.assertNotNull("delegate argument may not be null",
064: delegate);
065: Assert.assertNotNull("address argument may not be null",
066: address);
067: m_delegate = delegate;
068: m_address = address;
069: }
070:
071: protected void process(final HTTPEvent e) throws HTTPException,
072: IOException {
073: final SocketChannel channel = e.getChannel();
074: final InputStream request = channel.socket().getInputStream();
075: final OutputStream response = channel.socket()
076: .getOutputStream();
077:
078: final Listener listener = new Listener();
079:
080: final HttpConnection con = new HttpConnection(listener,
081: m_address, request, response, channel.socket());
082:
083: con.handle();
084: }
085:
086: protected class Listener implements HttpListener {
087: protected HttpServer m_server = m_delegate;
088: protected String m_host = m_address.getHostName();
089: protected int m_port = 0;
090: protected int m_bufferSize = DEFAULT_BUFFER_SIZE;
091: protected int m_bufferReserve = 10;
092: protected String m_defaultScheme = HttpMessage.__SCHEME;
093: protected boolean m_lowOnResources = false;
094: protected boolean m_outOfResources = false;
095: protected String m_integralScheme = HttpMessage.__SSL_SCHEME;
096: protected int m_integralPort = 0;
097: protected String m_confidentialScheme = HttpMessage.__SSL_SCHEME;
098: protected int m_confidentialPort = 0;
099:
100: protected Listener() {
101: }
102:
103: protected Listener(final HttpServer server, final String host,
104: final int port, final int bufferSize,
105: final int bufferReserve, final String defaultScheme,
106: final boolean lowOnResources,
107: final boolean outOfResources,
108: final String integralScheme, final int integralPort,
109: final String confidentialScheme,
110: final int confidentialPort) {
111: m_server = server;
112: m_host = host;
113: m_port = port;
114: m_bufferSize = bufferSize;
115: m_bufferReserve = bufferReserve;
116: m_defaultScheme = defaultScheme;
117: m_lowOnResources = lowOnResources;
118: m_outOfResources = outOfResources;
119: m_integralScheme = integralScheme;
120: m_integralPort = integralPort;
121: m_confidentialScheme = confidentialScheme;
122: m_confidentialPort = confidentialPort;
123: }
124:
125: // ----------------------------------------------------------------------
126: // Getters/Setters
127: // ----------------------------------------------------------------------
128: public void setHttpServer(final HttpServer server) {
129: m_server = server;
130: }
131:
132: public HttpServer getHttpServer() {
133: return m_server;
134: }
135:
136: public int getPort() {
137: return m_port;
138: }
139:
140: public void setPort(final int port) {
141: m_port = port;
142: }
143:
144: public int getBufferSize() {
145: return m_bufferSize;
146: }
147:
148: public void setBufferSize(final int bufferSize) {
149: m_bufferSize = bufferSize;
150: }
151:
152: public int getBufferReserve() {
153: return m_bufferReserve;
154: }
155:
156: public void setBufferReserve(final int bufferReserve) {
157: m_bufferReserve = bufferReserve;
158: }
159:
160: public String getDefaultScheme() {
161: return m_defaultScheme;
162: }
163:
164: public void setDefaultScheme(final String defaultScheme) {
165: m_defaultScheme = defaultScheme;
166: }
167:
168: public boolean isLowOnResources() {
169: return m_lowOnResources;
170: }
171:
172: public void setLowOnResources(final boolean lowOnResources) {
173: m_lowOnResources = lowOnResources;
174: }
175:
176: public boolean isOutOfResources() {
177: return m_outOfResources;
178: }
179:
180: public void setOutOfResources(final boolean outOfResources) {
181: m_outOfResources = outOfResources;
182: }
183:
184: public String getIntegralScheme() {
185: return m_integralScheme;
186: }
187:
188: public void setIntegralScheme(final String integralScheme) {
189: m_integralScheme = integralScheme;
190: }
191:
192: public int getIntegralPort() {
193: return m_integralPort;
194: }
195:
196: public void setIntegralPort(final int integralPort) {
197: m_integralPort = integralPort;
198: }
199:
200: public String getConfidentialScheme() {
201: return m_confidentialScheme;
202: }
203:
204: public void setConfidentialScheme(
205: final String confidentialScheme) {
206: m_confidentialScheme = confidentialScheme;
207: }
208:
209: public int getConfidentialPort() {
210: return m_confidentialPort;
211: }
212:
213: public void setConfidentialPort(final int confidentialPort) {
214: m_confidentialPort = confidentialPort;
215: }
216:
217: public void setHost(final String host)
218: throws UnknownHostException {
219: m_host = host;
220: }
221:
222: public String getHost() {
223: return m_host;
224: }
225:
226: // ----------------------------------------------------------------------
227: // Interface: HttpListener
228: // ----------------------------------------------------------------------
229: public void customizeRequest(final HttpConnection connection,
230: final HttpRequest request) {
231: // no thanks
232: }
233:
234: public void persistConnection(final HttpConnection connection) {
235: // no thanks
236: }
237:
238: public boolean isIntegral(final HttpConnection connection) {
239: return false;
240: }
241:
242: public boolean isConfidential(final HttpConnection connection) {
243: return false;
244: }
245:
246: // ----------------------------------------------------------------------
247: // Interface: LifeCycle
248: // ----------------------------------------------------------------------
249: public void start() throws Exception {
250: }
251:
252: public void stop() throws InterruptedException {
253: }
254:
255: public boolean isStarted() {
256: return true;
257: }
258: }
259: }
|