001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018: package com.noelios.restlet.ext.asyncweb;
019:
020: import java.util.logging.Level;
021:
022: import org.restlet.Server;
023: import org.safehaus.asyncweb.container.ContainerLifecycleException;
024: import org.safehaus.asyncweb.container.ServiceContainer;
025: import org.safehaus.asyncweb.container.ServiceHandler;
026: import org.safehaus.asyncweb.http.HttpResponse;
027: import org.safehaus.asyncweb.request.AsyncWebRequest;
028: import org.safehaus.asyncweb.transport.Transport;
029: import org.safehaus.asyncweb.transport.TransportException;
030:
031: import com.noelios.restlet.http.HttpServerCall;
032: import com.noelios.restlet.http.HttpServerHelper;
033:
034: /**
035: * Abstract AsyncWeb server connector. Here is the list of parameters that are
036: * supported: <table>
037: * <tr>
038: * <th>Parameter name</th>
039: * <th>Value type</th>
040: * <th>Default value</th>
041: * <th>Description</th>
042: * </tr>
043: * <tr>
044: * <td>ioWorkerCount</td>
045: * <td>int</td>
046: * <td>2</td>
047: * <td>Number of worker threads to employ.</td>
048: * </tr>
049: * <tr>
050: * <td>converter</td>
051: * <td>String</td>
052: * <td>com.noelios.restlet.http.HttpServerConverter</td>
053: * <td>Class name of the converter of low-level HTTP calls into high level
054: * requests and responses.</td>
055: * </tr>
056: * <tr>
057: * <td>useForwardedForHeader</td>
058: * <td>boolean</td>
059: * <td>false</td>
060: * <td>Lookup the "X-Forwarded-For" header supported by popular proxies and
061: * caches and uses it to populate the Request.getClientAddresses() method
062: * result. This information is only safe for intermediary components within your
063: * local network. Other addresses could easily be changed by setting a fake
064: * header and should not be trusted for serious security checks.</td>
065: * </tr>
066: * </table> <br/> This implementation passes by all of AsyncWeb
067: * ServiceContainer, HttpServiceHandler etc. mechanisms and implements a
068: * {@link com.noelios.restlet.http.HttpServerHelper} and a
069: * {@link org.safehaus.asyncweb.container.ServiceContainer} directly. It takes
070: * care about setting up a
071: * {@link org.safehaus.asyncweb.transport.nio.NIOTransport}.
072: * <p>
073: * Note: This implementation is not usable inside an AsyncWeb standard
074: * environment because it represents a container and not a handler; it takes
075: * full control over the container lifecycle.
076: * </p>
077: *
078: * @author Lars Heuer (heuer[at]semagia.com) <a
079: * href="http://www.semagia.com/">Semagia</a>
080: * @author Jerome Louvel (contact@noelios.com)
081: */
082: public abstract class AsyncWebServerHelper extends HttpServerHelper
083: implements ServiceContainer {
084: /**
085: * Indicates if the server is acting in HTTPS mode.
086: */
087: private boolean confidential;
088:
089: /**
090: * The AsyncWeb transport layer.
091: */
092: private Transport transport;
093:
094: /**
095: * Constructor.
096: *
097: * @param server
098: * The server to help.
099: * @param confidential
100: * Indicates if the server is acting in HTTPS mode.
101: */
102: public AsyncWebServerHelper(Server server, boolean confidential) {
103: super (server);
104: this .confidential = confidential;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.safehaus.asyncweb.container.ServiceContainer#addServiceHandler(org.safehaus.asyncweb.container.ServiceHandler)
111: */
112: public void addServiceHandler(ServiceHandler serviceHandler) {
113: throw new UnsupportedOperationException(
114: "This container accepts no service handlers");
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.safehaus.asyncweb.container.ServiceContainer#addTransport(org.safehaus.asyncweb.transport.Transport)
121: */
122: public void addTransport(Transport transport) {
123: throw new UnsupportedOperationException(
124: "This container is bound to a transport");
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.safehaus.asyncweb.container.ServiceContainer#dispatchRequest(org.safehaus.asyncweb.request.AsyncWebRequest)
131: */
132: public void dispatchRequest(AsyncWebRequest request) {
133: HttpResponse response = request.createHttpResponse();
134: HttpServerCall call = new AsyncWebServerCall(getServer(),
135: request, response, confidential);
136: handle(call);
137: request.commitResponse(response);
138: }
139:
140: /** Starts the Connector. */
141: @SuppressWarnings("unchecked")
142: public void start() throws ContainerLifecycleException {
143: try {
144: getTransport().start();
145: } catch (TransportException ex) {
146: getLogger().log(Level.WARNING,
147: "Failed to start the transport", ex);
148: throw new ContainerLifecycleException(
149: "Failed to start the transport", ex);
150: } catch (Exception e) {
151: getLogger().log(Level.WARNING,
152: "Failed to start the AsyncWeb HTTP Server", e);
153: throw new ContainerLifecycleException(
154: "Failed to start the AsyncWeb HTTP Server", e);
155: }
156: }
157:
158: /** Stops the Connector. */
159: @SuppressWarnings("unchecked")
160: public void stop() {
161: try {
162: getTransport().stop();
163: } catch (TransportException ex) {
164: getLogger().log(Level.WARNING, "Failed to stop transport",
165: ex);
166: } catch (Exception e) {
167: getLogger().log(Level.WARNING,
168: "Failed to start the AsyncWeb HTTP Server", e);
169: }
170: }
171:
172: /**
173: * Returns the number of worker threads to employ.
174: *
175: * @return The number of worker threads to employ.
176: */
177: public int getIoWorkerCount() {
178: return Integer.parseInt(getParameters().getFirstValue(
179: "ioWorkerCount", "2"));
180: }
181:
182: /**
183: * Sets the AsyncWeb transport layer.
184: *
185: * @param transport
186: * The AsyncWeb transport layer.
187: */
188: protected void setTransport(Transport transport) {
189: this .transport = transport;
190: }
191:
192: /**
193: * Returns the AsyncWeb transport layer.
194: *
195: * @return The AsyncWeb transport layer.
196: */
197: protected Transport getTransport() {
198: return this.transport;
199: }
200:
201: }
|