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.HttpResponse;
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.config.MessageConstraints;
40  import org.apache.http.impl.DefaultHttpResponseFactory;
41  import org.apache.http.io.SessionInputBuffer;
42  import org.apache.http.message.BasicLineParser;
43  import org.apache.http.message.LineParser;
44  import org.apache.http.message.ParserCursor;
45  import org.apache.http.params.HttpParams;
46  import org.apache.http.util.Args;
47  import org.apache.http.util.CharArrayBuffer;
48  
49  /**
50   * HTTP response parser that obtain its input from an instance
51   * of {@link SessionInputBuffer}.
52   *
53   * @since 4.2
54   */
55  @SuppressWarnings("deprecation")
56  @NotThreadSafe
57  public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
58  
59      private final HttpResponseFactory responseFactory;
60      private final CharArrayBuffer lineBuf;
61  
62      /**
63       * Creates an instance of this class.
64       *
65       * @param buffer the session input buffer.
66       * @param lineParser the line parser.
67       * @param responseFactory the factory to use to create
68       *    {@link HttpResponse}s.
69       * @param params HTTP parameters.
70       *
71       * @deprecated (4.3) use
72       *   {@link DefaultHttpResponseParser#DefaultHttpResponseParser(SessionInputBuffer, LineParser,
73       *     HttpResponseFactory, MessageConstraints)}
74       */
75      @Deprecated
76      public DefaultHttpResponseParser(
77              final SessionInputBuffer buffer,
78              final LineParser lineParser,
79              final HttpResponseFactory responseFactory,
80              final HttpParams params) {
81          super(buffer, lineParser, params);
82          this.responseFactory = Args.notNull(responseFactory, "Response factory");
83          this.lineBuf = new CharArrayBuffer(128);
84      }
85  
86      /**
87       * Creates new instance of DefaultHttpResponseParser.
88       *
89       * @param buffer the session input buffer.
90       * @param lineParser the line parser. If <code>null</code> {@link BasicLineParser#INSTANCE}
91       *   will be used
92       * @param responseFactory the response factory. If <code>null</code>
93       *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
94       * @param constraints the message constraints. If <code>null</code>
95       *   {@link MessageConstraints#DEFAULT} will be used.
96       *
97       * @since 4.3
98       */
99      public DefaultHttpResponseParser(
100             final SessionInputBuffer buffer,
101             final LineParser lineParser,
102             final HttpResponseFactory responseFactory,
103             final MessageConstraints constraints) {
104         super(buffer, lineParser, constraints);
105         this.responseFactory = responseFactory != null ? responseFactory :
106             DefaultHttpResponseFactory.INSTANCE;
107         this.lineBuf = new CharArrayBuffer(128);
108     }
109 
110     /**
111      * @since 4.3
112      */
113     public DefaultHttpResponseParser(
114             final SessionInputBuffer buffer,
115             final MessageConstraints constraints) {
116         this(buffer, null, null, constraints);
117     }
118 
119     /**
120      * @since 4.3
121      */
122     public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
123         this(buffer, null, null, MessageConstraints.DEFAULT);
124     }
125 
126     @Override
127     protected HttpResponse parseHead(
128             final SessionInputBuffer sessionBuffer)
129         throws IOException, HttpException, ParseException {
130 
131         this.lineBuf.clear();
132         final int i = sessionBuffer.readLine(this.lineBuf);
133         if (i == -1) {
134             throw new NoHttpResponseException("The target server failed to respond");
135         }
136         //create the status line from the status string
137         final ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
138         final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
139         return this.responseFactory.newHttpResponse(statusline, null);
140     }
141 
142 }