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.HttpResponse;
032import org.apache.http.HttpResponseFactory;
033import org.apache.http.ParseException;
034import org.apache.http.StatusLine;
035import org.apache.http.config.MessageConstraints;
036import org.apache.http.impl.DefaultHttpResponseFactory;
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 HttpResponse}s.
047 *
048 * @since 4.1
049 */
050@SuppressWarnings("deprecation")
051public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
052
053    private final HttpResponseFactory responseFactory;
054
055    /**
056     * @deprecated (4.3) use
057     *   {@link DefaultHttpResponseParser#DefaultHttpResponseParser(
058     *   SessionInputBuffer, LineParser, HttpResponseFactory, MessageConstraints)}
059     */
060    @Deprecated
061    public DefaultHttpResponseParser(
062            final SessionInputBuffer buffer,
063            final LineParser parser,
064            final HttpResponseFactory responseFactory,
065            final HttpParams params) {
066        super(buffer, parser, params);
067        Args.notNull(responseFactory, "Response factory");
068        this.responseFactory = responseFactory;
069    }
070
071    /**
072     * Creates an instance of DefaultHttpResponseParser.
073     *
074     * @param buffer the session input buffer.
075     * @param parser the line parser. If {@code null}
076     *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
077     * @param responseFactory the response factory. If {@code null}
078     *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
079     * @param constraints Message constraints. If {@code null}
080     *   {@link MessageConstraints#DEFAULT} will be used.
081     *
082     * @since 4.3
083     */
084    public DefaultHttpResponseParser(
085            final SessionInputBuffer buffer,
086            final LineParser parser,
087            final HttpResponseFactory responseFactory,
088            final MessageConstraints constraints) {
089        super(buffer, parser, constraints);
090        this.responseFactory = responseFactory != null ? responseFactory :
091            DefaultHttpResponseFactory.INSTANCE;
092    }
093
094    /**
095     * @since 4.3
096     */
097    public DefaultHttpResponseParser(final SessionInputBuffer buffer, final MessageConstraints constraints) {
098        this(buffer, null, null, constraints);
099    }
100
101    /**
102     * @since 4.3
103     */
104    public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
105        this(buffer, null);
106    }
107
108    @Override
109    protected HttpResponse createMessage(final CharArrayBuffer buffer)
110            throws HttpException, ParseException {
111        final ParserCursor cursor = new ParserCursor(0, buffer.length());
112        final StatusLine statusline = lineParser.parseStatusLine(buffer, cursor);
113        return this.responseFactory.newHttpResponse(statusline, null);
114    }
115
116}