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.http;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.util.logging.Level;
023:
024: import org.restlet.Context;
025: import org.restlet.Server;
026:
027: import com.noelios.restlet.ServerHelper;
028:
029: /**
030: * Base HTTP server connector. Here is the list of parameters that are
031: * supported: <table>
032: * <tr>
033: * <th>Parameter name</th>
034: * <th>Value type</th>
035: * <th>Default value</th>
036: * <th>Description</th>
037: * </tr>
038: * <tr>
039: * <td>useForwardedForHeader</td>
040: * <td>boolean</td>
041: * <td>false</td>
042: * <td>Lookup the "X-Forwarded-For" header supported by popular proxies and
043: * caches and uses it to populate the Request.getClientAddresses() method
044: * result. This information is only safe for intermediary components within your
045: * local network. Other addresses could easily be changed by setting a fake
046: * header and should not be trusted for serious security checks.</td>
047: * </tr>
048: * <tr>
049: * <td>converter</td>
050: * <td>String</td>
051: * <td>com.noelios.restlet.http.HttpServerConverter</td>
052: * <td>Class name of the converter of low-level HTTP calls into high level
053: * requests and responses.</td>
054: * </tr>
055: * </table>
056: *
057: * @author Jerome Louvel (contact@noelios.com)
058: */
059: public class HttpServerHelper extends ServerHelper {
060: /** The converter from HTTP calls to uniform calls. */
061: private HttpServerConverter converter;
062:
063: /**
064: * Default constructor. Note that many methods assume that a non-null server
065: * is set to work properly. You can use the {@link #setServer} method for
066: * this purpose or better rely on the other constructor.
067: */
068: public HttpServerHelper() {
069: this (null);
070: }
071:
072: /**
073: * Constructor.
074: *
075: * @param server
076: * The server to help.
077: */
078: public HttpServerHelper(Server server) {
079: super (server);
080: this .converter = null;
081: }
082:
083: /**
084: * Handles the connector call.<br/> The default behavior is to create an
085: * REST call and delegate it to the attached Restlet.
086: *
087: * @param httpCall
088: * The HTTP server call.
089: */
090: public void handle(HttpServerCall httpCall) {
091: try {
092: HttpRequest request = getConverter().toRequest(httpCall);
093: HttpResponse response = new HttpResponse(httpCall, request);
094: handle(request, response);
095: getConverter().commit(response);
096: } catch (Exception e) {
097: getLogger().log(Level.WARNING,
098: "Error while handling an HTTP server call: ",
099: e.getMessage());
100: getLogger().log(Level.INFO,
101: "Error while handling an HTTP server call", e);
102: }
103: }
104:
105: /**
106: * Returns the converter from HTTP calls to uniform calls.
107: *
108: * @return the converter from HTTP calls to uniform calls.
109: */
110: public HttpServerConverter getConverter() {
111: if (this .converter == null) {
112: try {
113: String converterClass = getParameters().getFirstValue(
114: "converter",
115: "com.noelios.restlet.http.HttpServerConverter");
116: this .converter = (HttpServerConverter) Class.forName(
117: converterClass).getConstructor(Context.class)
118: .newInstance(getContext());
119: } catch (IllegalArgumentException e) {
120: getLogger()
121: .log(
122: Level.SEVERE,
123: "Unable to create the HTTP server converter",
124: e);
125: } catch (SecurityException e) {
126: getLogger()
127: .log(
128: Level.SEVERE,
129: "Unable to create the HTTP server converter",
130: e);
131: } catch (InstantiationException e) {
132: getLogger()
133: .log(
134: Level.SEVERE,
135: "Unable to create the HTTP server converter",
136: e);
137: } catch (IllegalAccessException e) {
138: getLogger()
139: .log(
140: Level.SEVERE,
141: "Unable to create the HTTP server converter",
142: e);
143: } catch (InvocationTargetException e) {
144: getLogger()
145: .log(
146: Level.SEVERE,
147: "Unable to create the HTTP server converter",
148: e);
149: } catch (NoSuchMethodException e) {
150: getLogger()
151: .log(
152: Level.SEVERE,
153: "Unable to create the HTTP server converter",
154: e);
155: } catch (ClassNotFoundException e) {
156: getLogger()
157: .log(
158: Level.SEVERE,
159: "Unable to create the HTTP server converter",
160: e);
161: }
162: }
163:
164: return this .converter;
165: }
166:
167: /**
168: * Sets the converter from HTTP calls to uniform calls.
169: *
170: * @param converter
171: * The converter to set.
172: */
173: public void setConverter(HttpServerConverter converter) {
174: this.converter = converter;
175: }
176: }
|