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.util.logging.Level;
022:
023: import org.restlet.Client;
024: import org.restlet.Context;
025: import org.restlet.data.Request;
026: import org.restlet.data.Response;
027: import org.restlet.data.Status;
028:
029: import com.noelios.restlet.ClientHelper;
030:
031: /**
032: * Base HTTP client connector. Here is the list of parameters that are
033: * supported: <table>
034: * <tr>
035: * <th>Parameter name</th>
036: * <th>Value type</th>
037: * <th>Default value</th>
038: * <th>Description</th>
039: * </tr>
040: * <tr>
041: * <td>converter</td>
042: * <td>String</td>
043: * <td>com.noelios.restlet.http.HttpClientConverter</td>
044: * <td>Class name of the converter of low-level HTTP calls into high level
045: * requests and responses.</td>
046: * </tr>
047: * </table>
048: *
049: * @author Jerome Louvel (contact@noelios.com)
050: */
051: public abstract class HttpClientHelper extends ClientHelper {
052: /** The converter from uniform calls to HTTP calls. */
053: private HttpClientConverter converter;
054:
055: /**
056: * Constructor.
057: *
058: * @param client
059: * The client to help.
060: */
061: public HttpClientHelper(Client client) {
062: super (client);
063: this .converter = null;
064: }
065:
066: /**
067: * Creates a low-level HTTP client call from a high-level request.
068: *
069: * @param request
070: * The high-level request.
071: * @return A low-level HTTP client call.
072: */
073: public abstract HttpClientCall create(Request request);
074:
075: @Override
076: public void handle(Request request, Response response) {
077: try {
078: HttpClientCall httpCall = getConverter().toSpecific(this ,
079: request);
080: getConverter().commit(httpCall, request, response);
081: } catch (Exception e) {
082: getLogger().log(Level.INFO,
083: "Error while handling an HTTP client call", e);
084: response.setStatus(Status.CONNECTOR_ERROR_INTERNAL, e
085: .getMessage());
086: }
087: }
088:
089: /**
090: * Returns the converter from uniform calls to HTTP calls.
091: *
092: * @return the converter from uniform calls to HTTP calls.
093: */
094: public HttpClientConverter getConverter() throws Exception {
095: if (this .converter == null) {
096: String converterClass = getParameters().getFirstValue(
097: "converter",
098: "com.noelios.restlet.http.HttpClientConverter");
099: this .converter = (HttpClientConverter) Class.forName(
100: converterClass).getConstructor(Context.class)
101: .newInstance(getContext());
102: }
103:
104: return this .converter;
105: }
106:
107: /**
108: * Sets the converter from uniform calls to HTTP calls.
109: *
110: * @param converter
111: * The converter to set.
112: */
113: public void setConverter(HttpClientConverter converter) {
114: this.converter = converter;
115: }
116: }
|