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