michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.message;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HttpRequest;
michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion;
michael@0: import ch.boye.httpclientandroidlib.RequestLine;
michael@0: import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0: import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
michael@0:
michael@0: /**
michael@0: * Basic implementation of {@link HttpRequest}.
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this class:
michael@0: *
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#PROTOCOL_VERSION}
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {
michael@0:
michael@0: private final String method;
michael@0: private final String uri;
michael@0:
michael@0: private RequestLine requestline;
michael@0:
michael@0: /**
michael@0: * Creates an instance of this class using the given request method
michael@0: * and URI. The HTTP protocol version will be obtained from the
michael@0: * {@link HttpParams} instance associated with the object.
michael@0: * The initialization will be deferred
michael@0: * until {@link #getRequestLine()} is accessed for the first time.
michael@0: *
michael@0: * @param method request method.
michael@0: * @param uri request URI.
michael@0: */
michael@0: public BasicHttpRequest(final String method, final String uri) {
michael@0: super();
michael@0: if (method == null) {
michael@0: throw new IllegalArgumentException("Method name may not be null");
michael@0: }
michael@0: if (uri == null) {
michael@0: throw new IllegalArgumentException("Request URI may not be null");
michael@0: }
michael@0: this.method = method;
michael@0: this.uri = uri;
michael@0: this.requestline = null;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates an instance of this class using the given request method, URI
michael@0: * and the HTTP protocol version.
michael@0: *
michael@0: * @param method request method.
michael@0: * @param uri request URI.
michael@0: * @param ver HTTP protocol version.
michael@0: */
michael@0: public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) {
michael@0: this(new BasicRequestLine(method, uri, ver));
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates an instance of this class using the given request line.
michael@0: *
michael@0: * @param requestline request line.
michael@0: */
michael@0: public BasicHttpRequest(final RequestLine requestline) {
michael@0: super();
michael@0: if (requestline == null) {
michael@0: throw new IllegalArgumentException("Request line may not be null");
michael@0: }
michael@0: this.requestline = requestline;
michael@0: this.method = requestline.getMethod();
michael@0: this.uri = requestline.getUri();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the HTTP protocol version to be used for this request. If an
michael@0: * HTTP protocol version was not explicitly set at the construction time,
michael@0: * this method will obtain it from the {@link HttpParams} instance
michael@0: * associated with the object.
michael@0: *
michael@0: * @see #BasicHttpRequest(String, String)
michael@0: */
michael@0: public ProtocolVersion getProtocolVersion() {
michael@0: return getRequestLine().getProtocolVersion();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the request line of this request. If an HTTP protocol version
michael@0: * was not explicitly set at the construction time, this method will obtain
michael@0: * it from the {@link HttpParams} instance associated with the object.
michael@0: *
michael@0: * @see #BasicHttpRequest(String, String)
michael@0: */
michael@0: public RequestLine getRequestLine() {
michael@0: if (this.requestline == null) {
michael@0: ProtocolVersion ver = HttpProtocolParams.getVersion(getParams());
michael@0: this.requestline = new BasicRequestLine(this.method, this.uri, ver);
michael@0: }
michael@0: return this.requestline;
michael@0: }
michael@0:
michael@0: public String toString() {
michael@0: return this.method + " " + this.uri + " " + this.headergroup;
michael@0: }
michael@0:
michael@0: }