View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.io;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.HttpException;
33  import org.apache.http.HttpMessage;
34  import org.apache.http.HttpResponseFactory;
35  import org.apache.http.NoHttpResponseException;
36  import org.apache.http.ParseException;
37  import org.apache.http.StatusLine;
38  import org.apache.http.annotation.NotThreadSafe;
39  import org.apache.http.io.SessionInputBuffer;
40  import org.apache.http.message.LineParser;
41  import org.apache.http.message.ParserCursor;
42  import org.apache.http.params.HttpParams;
43  import org.apache.http.util.Args;
44  import org.apache.http.util.CharArrayBuffer;
45  
46  /**
47   * HTTP response parser that obtain its input from an instance
48   * of {@link SessionInputBuffer}.
49   * <p>
50   * The following parameters can be used to customize the behavior of this
51   * class:
52   * <ul>
53   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
54   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
55   * </ul>
56   *
57   * @since 4.0
58   *
59   * @deprecated (4.2) use {@link DefaultHttpResponseParser}
60   */
61  @Deprecated
62  @NotThreadSafe
63  public class HttpResponseParser extends AbstractMessageParser<HttpMessage> {
64  
65      private final HttpResponseFactory responseFactory;
66      private final CharArrayBuffer lineBuf;
67  
68      /**
69       * Creates an instance of this class.
70       *
71       * @param buffer the session input buffer.
72       * @param parser the line parser.
73       * @param responseFactory the factory to use to create
74       *    {@link org.apache.http.HttpResponse}s.
75       * @param params HTTP parameters.
76       */
77      public HttpResponseParser(
78              final SessionInputBuffer buffer,
79              final LineParser parser,
80              final HttpResponseFactory responseFactory,
81              final HttpParams params) {
82          super(buffer, parser, params);
83          this.responseFactory = Args.notNull(responseFactory, "Response factory");
84          this.lineBuf = new CharArrayBuffer(128);
85      }
86  
87      @Override
88      protected HttpMessage parseHead(
89              final SessionInputBuffer sessionBuffer)
90          throws IOException, HttpException, ParseException {
91  
92          this.lineBuf.clear();
93          final int i = sessionBuffer.readLine(this.lineBuf);
94          if (i == -1) {
95              throw new NoHttpResponseException("The target server failed to respond");
96          }
97          //create the status line from the status string
98          final ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
99          final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
100         return this.responseFactory.newHttpResponse(statusline, null);
101     }
102 
103 }