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.message;
029
030import org.apache.http.HttpRequest;
031import org.apache.http.HttpVersion;
032import org.apache.http.ProtocolVersion;
033import org.apache.http.RequestLine;
034import org.apache.http.util.Args;
035
036/**
037 * Basic implementation of {@link HttpRequest}.
038 *
039 * @since 4.0
040 */
041public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {
042
043    private final String method;
044    private final String uri;
045
046    private RequestLine requestline;
047
048    /**
049     * Creates an instance of this class using the given request method
050     * and URI.
051     *
052     * @param method request method.
053     * @param uri request URI.
054     */
055    public BasicHttpRequest(final String method, final String uri) {
056        super();
057        this.method = Args.notNull(method, "Method name");
058        this.uri = Args.notNull(uri, "Request URI");
059        this.requestline = null;
060    }
061
062    /**
063     * Creates an instance of this class using the given request method, URI
064     * and the HTTP protocol version.
065     *
066     * @param method request method.
067     * @param uri request URI.
068     * @param ver HTTP protocol version.
069     */
070    public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) {
071        this(new BasicRequestLine(method, uri, ver));
072    }
073
074    /**
075     * Creates an instance of this class using the given request line.
076     *
077     * @param requestline request line.
078     */
079    public BasicHttpRequest(final RequestLine requestline) {
080        super();
081        this.requestline = Args.notNull(requestline, "Request line");
082        this.method = requestline.getMethod();
083        this.uri = requestline.getUri();
084    }
085
086    /**
087     * Returns the HTTP protocol version to be used for this request.
088     *
089     * @see #BasicHttpRequest(String, String)
090     */
091    @Override
092    public ProtocolVersion getProtocolVersion() {
093        return getRequestLine().getProtocolVersion();
094    }
095
096    /**
097     * Returns the request line of this request.
098     *
099     * @see #BasicHttpRequest(String, String)
100     */
101    @Override
102    public RequestLine getRequestLine() {
103        if (this.requestline == null) {
104            this.requestline = new BasicRequestLine(this.method, this.uri, HttpVersion.HTTP_1_1);
105        }
106        return this.requestline;
107    }
108
109    @Override
110    public String toString() {
111        return this.method + ' ' + this.uri + ' ' + this.headergroup;
112    }
113
114}