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:
019: package com.noelios.restlet.ext.simple;
020:
021: import java.net.ServerSocket;
022:
023: import org.restlet.Server;
024:
025: import simple.http.PipelineHandler;
026: import simple.http.connect.Connection;
027:
028: import com.noelios.restlet.http.HttpServerHelper;
029:
030: /**
031: * Abstract Simple Web server connector. Here is the list of parameters that are
032: * supported: <table>
033: * <tr>
034: * <th>Parameter name</th>
035: * <th>Value type</th>
036: * <th>Default value</th>
037: * <th>Description</th>
038: * </tr>
039: * <tr>
040: * <td>defaultThreads</td>
041: * <td>int</td>
042: * <td>20</td>
043: * <td>Default number of polling threads for a handler object.</td>
044: * </tr>
045: * <tr>
046: * <td>maxWaitTimeMs</td>
047: * <td>int</td>
048: * <td>200</td>
049: * <td>Maximum waiting time between polls of the input.</td>
050: * </tr>
051: * <tr>
052: * <td>converter</td>
053: * <td>String</td>
054: * <td>com.noelios.restlet.http.HttpServerConverter</td>
055: * <td>Class name of the converter of low-level HTTP calls into high level
056: * requests and responses.</td>
057: * </tr>
058: * <tr>
059: * <td>useForwardedForHeader</td>
060: * <td>boolean</td>
061: * <td>false</td>
062: * <td>Lookup the "X-Forwarded-For" header supported by popular proxies and
063: * caches and uses it to populate the Request.getClientAddresses() method
064: * result. This information is only safe for intermediary components within your
065: * local network. Other addresses could easily be changed by setting a fake
066: * header and should not be trusted for serious security checks.</td>
067: * </tr>
068: * </table>
069: *
070: * @author Lars Heuer (heuer[at]semagia.com) <a
071: * href="http://semagia.com/">Semagia</a>
072: * @author Jerome Louvel (contact@noelios.com) <a
073: * href="http://www.noelios.com">Noelios Consulting</a>
074: */
075: public abstract class SimpleServerHelper extends HttpServerHelper {
076: /**
077: * Indicates if this service is acting in HTTP or HTTPS mode.
078: */
079: private boolean confidential;
080:
081: /**
082: * Server socket this server is listening to.
083: */
084: private ServerSocket socket;
085:
086: /**
087: * Simple pipeline handler.
088: */
089: private PipelineHandler handler;
090:
091: /**
092: * Simple connection.
093: */
094: private Connection connection;
095:
096: /**
097: * Constructor.
098: *
099: * @param server
100: * The server to help.
101: */
102: public SimpleServerHelper(Server server) {
103: super (server);
104: }
105:
106: /** Stops the Restlet. */
107: public void stop() throws Exception {
108: getSocket().close();
109: setSocket(null);
110: this .setHandler(null);
111: this .setConnection(null);
112:
113: // For further information on how to shutdown a Simple
114: // server, see
115: // http://sourceforge.net/mailarchive/forum.php?thread_id=10138257&forum_id=38791
116: // There seems to be place for improvement in this method.
117: }
118:
119: /**
120: * Returns the default number of polling threads for a handler object.
121: *
122: * @return The default number of polling threads for a handler object.
123: */
124: public int getDefaultThreads() {
125: return Integer.parseInt(getParameters().getFirstValue(
126: "defaultThreads", "20"));
127: }
128:
129: /**
130: * Returns the maximum waiting time between polls of the input.
131: *
132: * @return The maximum waiting time between polls of the input.
133: */
134: public int getMaxWaitTimeMs() {
135: return Integer.parseInt(getParameters().getFirstValue(
136: "maxWaitTimeMs", "200"));
137: }
138:
139: /**
140: * Sets the server socket this server is listening to.
141: *
142: * @param socket
143: * The server socket this server is listening to.
144: */
145: protected void setSocket(ServerSocket socket) {
146: this .socket = socket;
147: }
148:
149: /**
150: * Returns the server socket this server is listening to.
151: *
152: * @return The server socket this server is listening to.
153: */
154: protected ServerSocket getSocket() {
155: return socket;
156: }
157:
158: /**
159: * Sets the Simple pipeline handler.
160: *
161: * @param handler
162: * The Simple pipeline handler.
163: */
164: protected void setHandler(PipelineHandler handler) {
165: this .handler = handler;
166: }
167:
168: /**
169: * Returns the Simple pipeline handler.
170: *
171: * @return The Simple pipeline handler.
172: */
173: protected PipelineHandler getHandler() {
174: return handler;
175: }
176:
177: /**
178: * Sets the Simple connection.
179: *
180: * @param connection
181: * The Simple connection.
182: */
183: protected void setConnection(Connection connection) {
184: this .connection = connection;
185: }
186:
187: /**
188: * Returns the Simple connection.
189: *
190: * @return The Simple connection.
191: */
192: protected Connection getConnection() {
193: return connection;
194: }
195:
196: /**
197: * Indicates if this service is acting in HTTP or HTTPS mode.
198: *
199: * @param confidential
200: * True if this service is acting in HTTP or HTTPS mode.
201: */
202: protected void setConfidential(boolean confidential) {
203: this .confidential = confidential;
204: }
205:
206: /**
207: * Indicates if this service is acting in HTTP or HTTPS mode.
208: *
209: * @return True if this service is acting in HTTP or HTTPS mode.
210: */
211: protected boolean isConfidential() {
212: return confidential;
213: }
214:
215: }
|