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.HttpRequest;
32  import org.apache.hc.core5.http.HttpRequestFactory;
33  import org.apache.hc.core5.http.config.H1Config;
34  import org.apache.hc.core5.http.message.LineParser;
35  import org.apache.hc.core5.http.message.RequestLine;
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 HttpRequest}s.
41   *
42   * @since 4.1
43   */
44  public class DefaultHttpRequestParser<T extends HttpRequest> extends AbstractMessageParser<T> {
45  
46      private final HttpRequestFactory<T> requestFactory;
47  
48      /**
49       * Creates an instance of DefaultHttpRequestParser.
50       *
51       * @param requestFactory the request 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 h1Config Message h1Config. If {@code null}
55       *   {@link H1Config#DEFAULT} will be used.
56       *
57       * @since 4.3
58       */
59      public DefaultHttpRequestParser(
60              final HttpRequestFactory<T> requestFactory,
61              final LineParser parser,
62              final H1Config h1Config) {
63          super(parser, h1Config);
64          this.requestFactory = Args.notNull(requestFactory, "Request factory");
65      }
66  
67      /**
68      * @since 4.3
69      */
70      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory, final H1Config h1Config) {
71          this(requestFactory, null, h1Config);
72      }
73  
74      /**
75      * @since 4.3
76      */
77      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory) {
78          this(requestFactory, null);
79      }
80  
81      @Override
82      protected T createMessage(final CharArrayBuffer buffer) throws HttpException {
83          final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
84          final T request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
85          request.setVersion(requestLine.getProtocolVersion());
86          return request;
87      }
88  
89  }