1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.client.methods;
29
30 import java.net.URI;
31
32 import org.apache.http.ProtocolVersion;
33 import org.apache.http.RequestLine;
34 import org.apache.http.annotation.NotThreadSafe;
35 import org.apache.http.client.config.RequestConfig;
36 import org.apache.http.message.BasicRequestLine;
37 import org.apache.http.params.HttpProtocolParams;
38
39 /**
40 * Base implementation of {@link HttpUriRequest}.
41 *
42 * @since 4.0
43 */
44 @SuppressWarnings("deprecation")
45 @NotThreadSafe
46 public abstract class HttpRequestBase extends AbstractExecutionAwareRequest
47 implements HttpUriRequest, Configurable {
48
49 private ProtocolVersion version;
50 private URI uri;
51 private RequestConfig config;
52
53 public abstract String getMethod();
54
55 /**
56 * @since 4.3
57 */
58 public void setProtocolVersion(final ProtocolVersion version) {
59 this.version = version;
60 }
61
62 public ProtocolVersion getProtocolVersion() {
63 return version != null ? version : HttpProtocolParams.getVersion(getParams());
64 }
65
66 /**
67 * Returns the original request URI.
68 * <p>
69 * Please note URI remains unchanged in the course of request execution and
70 * is not updated if the request is redirected to another location.
71 */
72 public URI getURI() {
73 return this.uri;
74 }
75
76 public RequestLine getRequestLine() {
77 final String method = getMethod();
78 final ProtocolVersion ver = getProtocolVersion();
79 final URI uri = getURI();
80 String uritext = null;
81 if (uri != null) {
82 uritext = uri.toASCIIString();
83 }
84 if (uritext == null || uritext.length() == 0) {
85 uritext = "/";
86 }
87 return new BasicRequestLine(method, uritext, ver);
88 }
89
90
91 public RequestConfig getConfig() {
92 return config;
93 }
94
95 public void setConfig(final RequestConfig config) {
96 this.config = config;
97 }
98
99 public void setURI(final URI uri) {
100 this.uri = uri;
101 }
102
103 /**
104 * @since 4.2
105 */
106 public void started() {
107 }
108
109 /**
110 * A convenience method to simplify migration from HttpClient 3.1 API. This method is
111 * equivalent to {@link #reset()}.
112 *
113 * @since 4.2
114 */
115 public void releaseConnection() {
116 reset();
117 }
118
119 @Override
120 public String toString() {
121 return getMethod() + " " + getURI() + " " + getProtocolVersion();
122 }
123
124 }