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.client.methods;
029
030import java.net.URI;
031
032import org.apache.http.ProtocolVersion;
033import org.apache.http.RequestLine;
034import org.apache.http.client.config.RequestConfig;
035import org.apache.http.message.BasicRequestLine;
036import org.apache.http.params.HttpProtocolParams;
037
038/**
039 * Base implementation of {@link HttpUriRequest}.
040 *
041 * @since 4.0
042 */
043@SuppressWarnings("deprecation")
044public abstract class HttpRequestBase extends AbstractExecutionAwareRequest
045    implements HttpUriRequest, Configurable {
046
047    private ProtocolVersion version;
048    private URI uri;
049    private RequestConfig config;
050
051    @Override
052    public abstract String getMethod();
053
054    /**
055     * @since 4.3
056     */
057    public void setProtocolVersion(final ProtocolVersion version) {
058        this.version = version;
059    }
060
061    @Override
062    public ProtocolVersion getProtocolVersion() {
063        return version != null ? version : HttpProtocolParams.getVersion(getParams());
064    }
065
066    /**
067     * Returns the original request URI.
068     * <p>
069     * Please note URI remains unchanged in the course of request execution and
070     * is not updated if the request is redirected to another location.
071     */
072    @Override
073    public URI getURI() {
074        return this.uri;
075    }
076
077    @Override
078    public RequestLine getRequestLine() {
079        final String method = getMethod();
080        final ProtocolVersion ver = getProtocolVersion();
081        final URI uriCopy = getURI(); // avoids possible window where URI could be changed
082        String uritext = null;
083        if (uriCopy != null) {
084            uritext = uriCopy.toASCIIString();
085        }
086        if (uritext == null || uritext.isEmpty()) {
087            uritext = "/";
088        }
089        return new BasicRequestLine(method, uritext, ver);
090    }
091
092
093    @Override
094    public RequestConfig getConfig() {
095        return config;
096    }
097
098    public void setConfig(final RequestConfig config) {
099        this.config = config;
100    }
101
102    public void setURI(final URI uri) {
103        this.uri = uri;
104    }
105
106    /**
107     * @since 4.2
108     */
109    public void started() {
110    }
111
112    /**
113     * A convenience method to simplify migration from HttpClient 3.1 API. This method is
114     * equivalent to {@link #reset()}.
115     *
116     * @since 4.2
117     */
118    public void releaseConnection() {
119        reset();
120    }
121
122    @Override
123    public String toString() {
124        return getMethod() + " " + getURI() + " " + getProtocolVersion();
125    }
126
127}