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       * @since 5.3
55       */
56      public DefaultHttpRequestParser(
57              final Http1Config http1Config,
58              final LineParser parser,
59              final HttpRequestFactory<T> requestFactory) {
60          super(http1Config, parser);
61          this.requestFactory = Args.notNull(requestFactory, "Request factory");
62      }
63  
64      /**
65       * @since 5.3
66       */
67      public DefaultHttpRequestParser(final Http1Config http1Config, final HttpRequestFactory<T> requestFactory) {
68          this(http1Config, null, requestFactory);
69      }
70  
71      /**
72       * @deprecated Use {@link #DefaultHttpRequestParser(Http1Config, HttpRequestFactory)}  }
73       */
74      @Deprecated
75      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory, final Http1Config http1Config) {
76          this(requestFactory, null, http1Config);
77      }
78  
79      /**
80       * @deprecated Use {@link #DefaultHttpRequestParser(Http1Config, LineParser, HttpRequestFactory)}  }
81       */
82      @Deprecated
83      public DefaultHttpRequestParser(
84              final HttpRequestFactory<T> requestFactory,
85              final LineParser parser,
86              final Http1Config http1Config) {
87          this(http1Config, parser, requestFactory);
88      }
89  
90      /**
91       * @since 4.3
92       */
93      public DefaultHttpRequestParser(final HttpRequestFactory<T> requestFactory) {
94          this(null, null, requestFactory);
95      }
96  
97      @Override
98      public T parse(final SessionInputBuffer sessionBuffer, final boolean endOfStream) throws IOException, HttpException {
99          try {
100             return super.parse(sessionBuffer, endOfStream);
101         } catch (final MessageConstraintException ex) {
102             throw new RequestHeaderFieldsTooLargeException(ex.getMessage(), ex);
103         }
104     }
105 
106     @Override
107     protected T createMessage(final CharArrayBuffer buffer) throws HttpException {
108         final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
109         final T request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
110         request.setVersion(requestLine.getProtocolVersion());
111         return request;
112     }
113 
114 }