001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.nio.codecs;
029
030import org.apache.http.HttpException;
031import org.apache.http.HttpRequest;
032import org.apache.http.HttpRequestFactory;
033import org.apache.http.ParseException;
034import org.apache.http.RequestLine;
035import org.apache.http.config.MessageConstraints;
036import org.apache.http.impl.DefaultHttpRequestFactory;
037import org.apache.http.message.LineParser;
038import org.apache.http.message.ParserCursor;
039import org.apache.http.nio.reactor.SessionInputBuffer;
040import org.apache.http.params.HttpParams;
041import org.apache.http.util.Args;
042import org.apache.http.util.CharArrayBuffer;
043
044/**
045 * Default {@link org.apache.http.nio.NHttpMessageParser} implementation
046 * for {@link HttpRequest}s.
047 *
048 * @since 4.1
049 */
050@SuppressWarnings("deprecation")
051public class DefaultHttpRequestParser extends AbstractMessageParser<HttpRequest> {
052
053    private final HttpRequestFactory requestFactory;
054
055    /**
056     * Creates an instance of this class.
057     *
058     * @param buffer the session input buffer.
059     * @param parser the line parser.
060     * @param params HTTP parameters.
061     *
062     * @deprecated (4.3) use
063     *   {@link DefaultHttpRequestParser#DefaultHttpRequestParser(
064     *   SessionInputBuffer, LineParser, HttpRequestFactory, MessageConstraints)}
065     */
066    @Deprecated
067    public DefaultHttpRequestParser(
068            final SessionInputBuffer buffer,
069            final LineParser parser,
070            final HttpRequestFactory requestFactory,
071            final HttpParams params) {
072        super(buffer, parser, params);
073        Args.notNull(requestFactory, "Request factory");
074        this.requestFactory = requestFactory;
075    }
076
077    /**
078     * Creates an instance of DefaultHttpRequestParser.
079     *
080     * @param buffer the session input buffer.
081     * @param parser the line parser. If {@code null}
082     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
083     * @param requestFactory the request factory. If {@code null}
084     *   {@link DefaultHttpRequestFactory#INSTANCE} will be used.
085     * @param constraints Message constraints. If {@code null}
086     *   {@link MessageConstraints#DEFAULT} will be used.
087     *
088     * @since 4.3
089     */
090    public DefaultHttpRequestParser(
091            final SessionInputBuffer buffer,
092            final LineParser parser,
093            final HttpRequestFactory requestFactory,
094            final MessageConstraints constraints) {
095        super(buffer, parser, constraints);
096        this.requestFactory = requestFactory != null ? requestFactory : DefaultHttpRequestFactory.INSTANCE;
097    }
098
099    /**
100    * @since 4.3
101    */
102    public DefaultHttpRequestParser(final SessionInputBuffer buffer, final MessageConstraints constraints) {
103        this(buffer, null, null, constraints);
104    }
105
106    /**
107    * @since 4.3
108    */
109    public DefaultHttpRequestParser(final SessionInputBuffer buffer) {
110        this(buffer, null);
111    }
112
113    @Override
114    protected HttpRequest createMessage(final CharArrayBuffer buffer)
115            throws HttpException, ParseException {
116        final ParserCursor cursor = new ParserCursor(0, buffer.length());
117        final RequestLine requestLine = lineParser.parseRequestLine(buffer, cursor);
118        return this.requestFactory.newHttpRequest(requestLine);
119    }
120
121}