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.message;
29
30 import org.apache.http.HttpRequest;
31 import org.apache.http.HttpVersion;
32 import org.apache.http.ProtocolVersion;
33 import org.apache.http.RequestLine;
34 import org.apache.http.annotation.NotThreadSafe;
35 import org.apache.http.util.Args;
36
37 /**
38 * Basic implementation of {@link HttpRequest}.
39 *
40 * @since 4.0
41 */
42 @NotThreadSafe
43 public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {
44
45 private final String method;
46 private final String uri;
47
48 private RequestLine requestline;
49
50 /**
51 * Creates an instance of this class using the given request method
52 * and URI.
53 *
54 * @param method request method.
55 * @param uri request URI.
56 */
57 public BasicHttpRequest(final String method, final String uri) {
58 super();
59 this.method = Args.notNull(method, "Method name");
60 this.uri = Args.notNull(uri, "Request URI");
61 this.requestline = null;
62 }
63
64 /**
65 * Creates an instance of this class using the given request method, URI
66 * and the HTTP protocol version.
67 *
68 * @param method request method.
69 * @param uri request URI.
70 * @param ver HTTP protocol version.
71 */
72 public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) {
73 this(new BasicRequestLine(method, uri, ver));
74 }
75
76 /**
77 * Creates an instance of this class using the given request line.
78 *
79 * @param requestline request line.
80 */
81 public BasicHttpRequest(final RequestLine requestline) {
82 super();
83 this.requestline = Args.notNull(requestline, "Request line");
84 this.method = requestline.getMethod();
85 this.uri = requestline.getUri();
86 }
87
88 /**
89 * Returns the HTTP protocol version to be used for this request.
90 *
91 * @see #BasicHttpRequest(String, String)
92 */
93 public ProtocolVersion getProtocolVersion() {
94 return getRequestLine().getProtocolVersion();
95 }
96
97 /**
98 * Returns the request line of this request.
99 *
100 * @see #BasicHttpRequest(String, String)
101 */
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 }