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