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 java.io.IOException;
31  
32  import org.apache.hc.core5.http.HttpException;
33  import org.apache.hc.core5.http.HttpRequest;
34  import org.apache.hc.core5.http.HttpRequestFactory;
35  import org.apache.hc.core5.http.MessageConstraintException;
36  import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
37  import org.apache.hc.core5.http.config.Http1Config;
38  import org.apache.hc.core5.http.message.LineParser;
39  import org.apache.hc.core5.http.message.RequestLine;
40  import org.apache.hc.core5.http.nio.SessionInputBuffer;
41  import org.apache.hc.core5.util.Args;
42  import org.apache.hc.core5.util.CharArrayBuffer;
43  
44  /**
45   * Default {@link org.apache.hc.core5.http.nio.NHttpMessageParser} implementation for {@link HttpRequest}s.
46   *
47   * @since 4.1
48   */
49  public class DefaultHttpRequestParser<T extends HttpRequest> extends AbstractMessageParser<T> {
50  
51      private final HttpRequestFactory<T> requestFactory;
52  
53      /**
54       * Creates an instance of DefaultHttpRequestParser.
55       *
56       * @param requestFactory the request factory.
57       * @param parser the line parser. If {@code null}
58       *   {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used.
59       * @param http1Config Message http1Config. If {@code null}
60       *   {@link Http1Config#DEFAULT} will be used.
61       *
62       * @since 4.3
63       */
64      public DefaultHttpRequestParser(
65              final HttpRequestFactory<T> requestFactory,
66              final LineParser parser,
67              final Http1Config http1Config) {
68          super(parser, http1Config);
69          this.requestFactory = Args.notNull(requestFactory, "Request factory");
70      }
71  
72      /**
73      * @since 4.3
74      */
75      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory, final Http1Config http1Config) {
76          this(requestFactory, null, http1Config);
77      }
78  
79      /**
80      * @since 4.3
81      */
82      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory) {
83          this(requestFactory, null);
84      }
85  
86      @Override
87      public T parse(final SessionInputBuffer sessionBuffer, final boolean endOfStream) throws IOException, HttpException {
88          try {
89              return super.parse(sessionBuffer, endOfStream);
90          } catch (final MessageConstraintException ex) {
91              throw new RequestHeaderFieldsTooLargeException(ex.getMessage(), ex);
92          }
93      }
94  
95      @Override
96      protected T createMessage(final CharArrayBuffer buffer) throws HttpException {
97          final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
98          final T request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
99          request.setVersion(requestLine.getProtocolVersion());
100         return request;
101     }
102 
103 }