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.HttpResponse;
35 import org.apache.http.HttpResponseFactory;
36 import org.apache.http.NoHttpResponseException;
37 import org.apache.http.StatusLine;
38 import org.apache.http.ParseException;
39 import org.apache.http.annotation.NotThreadSafe;
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.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 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 if (responseFactory == null) {
84 throw new IllegalArgumentException("Response factory may not be null");
85 }
86 this.responseFactory = responseFactory;
87 this.lineBuf = new CharArrayBuffer(128);
88 }
89
90 @Override
91 protected HttpMessage parseHead(
92 final SessionInputBuffer sessionBuffer)
93 throws IOException, HttpException, ParseException {
94
95 this.lineBuf.clear();
96 int i = sessionBuffer.readLine(this.lineBuf);
97 if (i == -1) {
98 throw new NoHttpResponseException("The target server failed to respond");
99 }
100 //create the status line from the status string
101 ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
102 StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
103 return this.responseFactory.newHttpResponse(statusline, null);
104 }
105
106 }