Home | History | Annotate | Download | only in protocol
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java $
      3  * $Revision: 610763 $
      4  * $Date: 2008-01-10 04:01:13 -0800 (Thu, 10 Jan 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.protocol;
     33 
     34 import java.io.IOException;
     35 
     36 import org.apache.http.ConnectionReuseStrategy;
     37 import org.apache.http.HttpEntity;
     38 import org.apache.http.HttpEntityEnclosingRequest;
     39 import org.apache.http.HttpException;
     40 import org.apache.http.HttpRequest;
     41 import org.apache.http.HttpResponse;
     42 import org.apache.http.HttpResponseFactory;
     43 import org.apache.http.HttpServerConnection;
     44 import org.apache.http.HttpStatus;
     45 import org.apache.http.HttpVersion;
     46 import org.apache.http.MethodNotSupportedException;
     47 import org.apache.http.ProtocolException;
     48 import org.apache.http.ProtocolVersion;
     49 import org.apache.http.UnsupportedHttpVersionException;
     50 import org.apache.http.entity.ByteArrayEntity;
     51 import org.apache.http.params.HttpParams;
     52 import org.apache.http.params.DefaultedHttpParams;
     53 import org.apache.http.util.EncodingUtils;
     54 
     55 /**
     56  * Minimalistic server-side implementation of an HTTP processor.
     57  *
     58  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     59  *
     60  * @version $Revision: 610763 $
     61  *
     62  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     63  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     64  *     for further details.
     65  */
     66 @Deprecated
     67 public class HttpService {
     68 
     69     private HttpParams params = null;
     70     private HttpProcessor processor = null;
     71     private HttpRequestHandlerResolver handlerResolver = null;
     72     private ConnectionReuseStrategy connStrategy = null;
     73     private HttpResponseFactory responseFactory = null;
     74     private HttpExpectationVerifier expectationVerifier = null;
     75 
     76     /**
     77      * Create a new HTTP service.
     78      *
     79      * @param proc             the processor to use on requests and responses
     80      * @param connStrategy     the connection reuse strategy
     81      * @param responseFactory  the response factory
     82      */
     83     public HttpService(
     84             final HttpProcessor proc,
     85             final ConnectionReuseStrategy connStrategy,
     86             final HttpResponseFactory responseFactory) {
     87         super();
     88         setHttpProcessor(proc);
     89         setConnReuseStrategy(connStrategy);
     90         setResponseFactory(responseFactory);
     91     }
     92 
     93     public void setHttpProcessor(final HttpProcessor processor) {
     94         if (processor == null) {
     95             throw new IllegalArgumentException("HTTP processor may not be null.");
     96         }
     97         this.processor = processor;
     98     }
     99 
    100     public void setConnReuseStrategy(final ConnectionReuseStrategy connStrategy) {
    101         if (connStrategy == null) {
    102             throw new IllegalArgumentException("Connection reuse strategy may not be null");
    103         }
    104         this.connStrategy = connStrategy;
    105     }
    106 
    107     public void setResponseFactory(final HttpResponseFactory responseFactory) {
    108         if (responseFactory == null) {
    109             throw new IllegalArgumentException("Response factory may not be null");
    110         }
    111         this.responseFactory = responseFactory;
    112     }
    113 
    114     public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) {
    115         this.handlerResolver = handlerResolver;
    116     }
    117 
    118     public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
    119         this.expectationVerifier = expectationVerifier;
    120     }
    121 
    122     public HttpParams getParams() {
    123         return this.params;
    124     }
    125 
    126     public void setParams(final HttpParams params) {
    127         this.params = params;
    128     }
    129 
    130     public void handleRequest(
    131             final HttpServerConnection conn,
    132             final HttpContext context) throws IOException, HttpException {
    133 
    134         context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    135 
    136         HttpResponse response = null;
    137 
    138         try {
    139 
    140             HttpRequest request = conn.receiveRequestHeader();
    141             request.setParams(
    142                     new DefaultedHttpParams(request.getParams(), this.params));
    143 
    144             ProtocolVersion ver =
    145                 request.getRequestLine().getProtocolVersion();
    146             if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
    147                 // Downgrade protocol version if greater than HTTP/1.1
    148                 ver = HttpVersion.HTTP_1_1;
    149             }
    150 
    151             if (request instanceof HttpEntityEnclosingRequest) {
    152 
    153                 if (((HttpEntityEnclosingRequest) request).expectContinue()) {
    154                     response = this.responseFactory.newHttpResponse(ver,
    155                             HttpStatus.SC_CONTINUE, context);
    156                     response.setParams(
    157                             new DefaultedHttpParams(response.getParams(), this.params));
    158 
    159                     if (this.expectationVerifier != null) {
    160                         try {
    161                             this.expectationVerifier.verify(request, response, context);
    162                         } catch (HttpException ex) {
    163                             response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0,
    164                                     HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
    165                             response.setParams(
    166                                     new DefaultedHttpParams(response.getParams(), this.params));
    167                             handleException(ex, response);
    168                         }
    169                     }
    170                     if (response.getStatusLine().getStatusCode() < 200) {
    171                         // Send 1xx response indicating the server expections
    172                         // have been met
    173                         conn.sendResponseHeader(response);
    174                         conn.flush();
    175                         response = null;
    176                         conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
    177                     }
    178                 } else {
    179                     conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
    180                 }
    181             }
    182 
    183             if (response == null) {
    184                 response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
    185                 response.setParams(
    186                         new DefaultedHttpParams(response.getParams(), this.params));
    187 
    188                 context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
    189                 context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
    190 
    191                 this.processor.process(request, context);
    192                 doService(request, response, context);
    193             }
    194 
    195             // Make sure the request content is fully consumed
    196             if (request instanceof HttpEntityEnclosingRequest) {
    197                 HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
    198                 if (entity != null) {
    199                     entity.consumeContent();
    200                 }
    201             }
    202 
    203         } catch (HttpException ex) {
    204             response = this.responseFactory.newHttpResponse
    205                 (HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR,
    206                  context);
    207             response.setParams(
    208                     new DefaultedHttpParams(response.getParams(), this.params));
    209             handleException(ex, response);
    210         }
    211 
    212         this.processor.process(response, context);
    213         conn.sendResponseHeader(response);
    214         conn.sendResponseEntity(response);
    215         conn.flush();
    216 
    217         if (!this.connStrategy.keepAlive(response, context)) {
    218             conn.close();
    219         }
    220     }
    221 
    222     protected void handleException(final HttpException ex, final HttpResponse response) {
    223         if (ex instanceof MethodNotSupportedException) {
    224             response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    225         } else if (ex instanceof UnsupportedHttpVersionException) {
    226             response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
    227         } else if (ex instanceof ProtocolException) {
    228             response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
    229         } else {
    230             response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    231         }
    232         byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
    233         ByteArrayEntity entity = new ByteArrayEntity(msg);
    234         entity.setContentType("text/plain; charset=US-ASCII");
    235         response.setEntity(entity);
    236     }
    237 
    238     protected void doService(
    239             final HttpRequest request,
    240             final HttpResponse response,
    241             final HttpContext context) throws HttpException, IOException {
    242         HttpRequestHandler handler = null;
    243         if (this.handlerResolver != null) {
    244             String requestURI = request.getRequestLine().getUri();
    245             handler = this.handlerResolver.lookup(requestURI);
    246         }
    247         if (handler != null) {
    248             handler.handle(request, response, context);
    249         } else {
    250             response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    251         }
    252     }
    253 
    254 }
    255