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.io.IOException;
022: import java.io.InputStream;
023: import java.util.logging.Level;
024:
025: import simple.http.ProtocolHandler;
026: import simple.http.Request;
027: import simple.http.Response;
028:
029: /**
030: * Simple protocol handler delegating the calls to the Restlet server helper.
031: *
032: * @author Jerome Louvel (contact@noelios.com) <a
033: * href="http://www.noelios.com/">Noelios Consulting</a>
034: */
035: public class SimpleProtocolHandler implements ProtocolHandler {
036: /** The delegate Restlet server helper. */
037: private SimpleServerHelper helper;
038:
039: /**
040: * Constructor.
041: *
042: * @param helper
043: * The delegate Restlet server helper.
044: */
045: public SimpleProtocolHandler(SimpleServerHelper helper) {
046: this .helper = helper;
047: }
048:
049: /**
050: * Returns the delegate Restlet server helper.
051: *
052: * @return The delegate Restlet server helper.
053: */
054: public SimpleServerHelper getHelper() {
055: return this .helper;
056: }
057:
058: /**
059: * Handles a Simple request/response transaction.
060: *
061: * @param request
062: * The Simple request.
063: * @param response
064: * The Simple response.
065: */
066: public void handle(Request request, Response response) {
067: getHelper().handle(
068: new SimpleCall(getHelper().getServer(), request,
069: response, getHelper().isConfidential()));
070:
071: try {
072: // Once the request is handled, the request input stream must be
073: // entirely consumed. Not doing so blocks invariably the transaction
074: // managed by the SimpleWeb connector.
075: InputStream in = request.getInputStream();
076: if (in != null) {
077: while (in.read() != -1) {
078: // just consume the stream
079: }
080: }
081: } catch (IOException e) {
082: // This is probably ok, the stream was certainly already
083: // closed by the Representation.release() method for
084: // example.
085: getHelper()
086: .getLogger()
087: .log(
088: Level.FINE,
089: "Exception while consuming the Simple request's input stream",
090: e);
091: }
092:
093: try {
094: response.getOutputStream().close();
095: } catch (IOException e) {
096: getHelper()
097: .getLogger()
098: .log(
099: Level.FINE,
100: "Exception while closing the Simple response's output stream",
101: e);
102: }
103: }
104:
105: }
|