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.hc.core5.http.impl.nio;
29  
30  import org.apache.hc.core5.http.HttpException;
31  import org.apache.hc.core5.http.HttpResponse;
32  import org.apache.hc.core5.http.HttpResponseFactory;
33  import org.apache.hc.core5.http.config.Http1Config;
34  import org.apache.hc.core5.http.message.LineParser;
35  import org.apache.hc.core5.http.message.StatusLine;
36  import org.apache.hc.core5.util.Args;
37  import org.apache.hc.core5.util.CharArrayBuffer;
38  
39  /**
40   * Default {@link org.apache.hc.core5.http.nio.NHttpMessageParser} implementation for {@link HttpResponse}s.
41   *
42   * @since 4.1
43   */
44  public class DefaultHttpResponseParser<T extends HttpResponse> extends AbstractMessageParser<T> {
45  
46      private final HttpResponseFactory<T> responseFactory;
47  
48      /**
49       * Creates an instance of DefaultHttpResponseParser.
50       *
51       * @param responseFactory the response factory.
52       * @param parser the line parser. If {@code null}
53       *   {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used.
54       * @param http1Config Message http1Config. If {@code null}
55       *   {@link Http1Config#DEFAULT} will be used.
56       *
57       * @since 4.3
58       */
59      public DefaultHttpResponseParser(
60              final HttpResponseFactory<T> responseFactory,
61              final LineParser parser,
62              final Http1Config http1Config) {
63          super(parser, http1Config);
64          this.responseFactory = Args.notNull(responseFactory, "Response factory");
65      }
66  
67      /**
68       * @since 4.3
69       */
70      public DefaultHttpResponseParser(final HttpResponseFactory<T> responseFactory, final Http1Config http1Config) {
71          this(responseFactory, null, http1Config);
72      }
73  
74      /**
75       * @since 4.3
76       */
77      public DefaultHttpResponseParser(final HttpResponseFactory<T> responseFactory) {
78          this(responseFactory, null);
79      }
80  
81      @Override
82      protected T createMessage(final CharArrayBuffer buffer) throws HttpException {
83          final StatusLine statusLine = getLineParser().parseStatusLine(buffer);
84          final T response = this.responseFactory.newHttpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase());
85          response.setVersion(statusLine.getProtocolVersion());
86          return response;
87      }
88  
89  }