|
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 ch.boye.httpclientandroidlib.message; |
|
29 |
|
30 import ch.boye.httpclientandroidlib.HttpRequest; |
|
31 import ch.boye.httpclientandroidlib.ProtocolVersion; |
|
32 import ch.boye.httpclientandroidlib.RequestLine; |
|
33 import ch.boye.httpclientandroidlib.params.HttpParams; |
|
34 import ch.boye.httpclientandroidlib.params.HttpProtocolParams; |
|
35 |
|
36 /** |
|
37 * Basic implementation of {@link HttpRequest}. |
|
38 * <p> |
|
39 * The following parameters can be used to customize the behavior of this class: |
|
40 * <ul> |
|
41 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#PROTOCOL_VERSION}</li> |
|
42 * </ul> |
|
43 * |
|
44 * @since 4.0 |
|
45 */ |
|
46 public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest { |
|
47 |
|
48 private final String method; |
|
49 private final String uri; |
|
50 |
|
51 private RequestLine requestline; |
|
52 |
|
53 /** |
|
54 * Creates an instance of this class using the given request method |
|
55 * and URI. The HTTP protocol version will be obtained from the |
|
56 * {@link HttpParams} instance associated with the object. |
|
57 * The initialization will be deferred |
|
58 * until {@link #getRequestLine()} is accessed for the first time. |
|
59 * |
|
60 * @param method request method. |
|
61 * @param uri request URI. |
|
62 */ |
|
63 public BasicHttpRequest(final String method, final String uri) { |
|
64 super(); |
|
65 if (method == null) { |
|
66 throw new IllegalArgumentException("Method name may not be null"); |
|
67 } |
|
68 if (uri == null) { |
|
69 throw new IllegalArgumentException("Request URI may not be null"); |
|
70 } |
|
71 this.method = method; |
|
72 this.uri = uri; |
|
73 this.requestline = null; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Creates an instance of this class using the given request method, URI |
|
78 * and the HTTP protocol version. |
|
79 * |
|
80 * @param method request method. |
|
81 * @param uri request URI. |
|
82 * @param ver HTTP protocol version. |
|
83 */ |
|
84 public BasicHttpRequest(final String method, final String uri, final ProtocolVersion ver) { |
|
85 this(new BasicRequestLine(method, uri, ver)); |
|
86 } |
|
87 |
|
88 /** |
|
89 * Creates an instance of this class using the given request line. |
|
90 * |
|
91 * @param requestline request line. |
|
92 */ |
|
93 public BasicHttpRequest(final RequestLine requestline) { |
|
94 super(); |
|
95 if (requestline == null) { |
|
96 throw new IllegalArgumentException("Request line may not be null"); |
|
97 } |
|
98 this.requestline = requestline; |
|
99 this.method = requestline.getMethod(); |
|
100 this.uri = requestline.getUri(); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Returns the HTTP protocol version to be used for this request. If an |
|
105 * HTTP protocol version was not explicitly set at the construction time, |
|
106 * this method will obtain it from the {@link HttpParams} instance |
|
107 * associated with the object. |
|
108 * |
|
109 * @see #BasicHttpRequest(String, String) |
|
110 */ |
|
111 public ProtocolVersion getProtocolVersion() { |
|
112 return getRequestLine().getProtocolVersion(); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Returns the request line of this request. If an HTTP protocol version |
|
117 * was not explicitly set at the construction time, this method will obtain |
|
118 * it from the {@link HttpParams} instance associated with the object. |
|
119 * |
|
120 * @see #BasicHttpRequest(String, String) |
|
121 */ |
|
122 public RequestLine getRequestLine() { |
|
123 if (this.requestline == null) { |
|
124 ProtocolVersion ver = HttpProtocolParams.getVersion(getParams()); |
|
125 this.requestline = new BasicRequestLine(this.method, this.uri, ver); |
|
126 } |
|
127 return this.requestline; |
|
128 } |
|
129 |
|
130 public String toString() { |
|
131 return this.method + " " + this.uri + " " + this.headergroup; |
|
132 } |
|
133 |
|
134 } |