1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.client.fluent;
29
30 import java.net.URI;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import java.util.concurrent.atomic.AtomicReference;
33
34 import org.apache.http.HttpVersion;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.RequestLine;
37 import org.apache.http.client.config.RequestConfig;
38 import org.apache.http.client.methods.Configurable;
39 import org.apache.http.client.methods.HttpExecutionAware;
40 import org.apache.http.client.methods.HttpUriRequest;
41 import org.apache.http.concurrent.Cancellable;
42 import org.apache.http.message.AbstractHttpMessage;
43 import org.apache.http.message.BasicRequestLine;
44 import org.apache.http.util.Args;
45
46 class InternalHttpRequest extends AbstractHttpMessage
47 implements HttpUriRequest, HttpExecutionAware, Configurable {
48
49 private final String method;
50 private ProtocolVersion version;
51 private URI uri;
52 private RequestConfig config;
53
54 private final AtomicBoolean aborted;
55 private final AtomicReference<Cancellable> cancellableRef;
56
57 InternalHttpRequest(final String method, final URI requestURI) {
58 Args.notBlank(method, "Method");
59 Args.notNull(requestURI, "Request URI");
60 this.method = method;
61 this.uri = requestURI;
62 this.aborted = new AtomicBoolean(false);
63 this.cancellableRef = new AtomicReference<Cancellable>(null);
64 }
65
66 public void setProtocolVersion(final ProtocolVersion version) {
67 this.version = version;
68 }
69
70 @Override
71 public ProtocolVersion getProtocolVersion() {
72 return version != null ? version : HttpVersion.HTTP_1_1;
73 }
74
75 @Override
76 public String getMethod() {
77 return this.method;
78 }
79
80 @Override
81 public URI getURI() {
82 return this.uri;
83 }
84
85 @Override
86 public void abort() throws UnsupportedOperationException {
87 if (this.aborted.compareAndSet(false, true)) {
88 final Cancellable cancellable = this.cancellableRef.getAndSet(null);
89 if (cancellable != null) {
90 cancellable.cancel();
91 }
92 }
93 }
94
95 @Override
96 public boolean isAborted() {
97 return this.aborted.get();
98 }
99
100 @Override
101 public void setCancellable(final Cancellable cancellable) {
102 if (!this.aborted.get()) {
103 this.cancellableRef.set(cancellable);
104 }
105 }
106
107 @Override
108 public RequestLine getRequestLine() {
109 final ProtocolVersion ver = getProtocolVersion();
110 final URI uriCopy = getURI();
111 String uritext = null;
112 if (uriCopy != null) {
113 uritext = uriCopy.toASCIIString();
114 }
115 if (uritext == null || uritext.isEmpty()) {
116 uritext = "/";
117 }
118 return new BasicRequestLine(getMethod(), uritext, ver);
119 }
120
121 @Override
122 public RequestConfig getConfig() {
123 return config;
124 }
125
126 public void setConfig(final RequestConfig config) {
127 this.config = config;
128 }
129
130 public void setURI(final URI uri) {
131 this.uri = uri;
132 }
133
134 @Override
135 public String toString() {
136 return getMethod() + " " + getURI() + " " + getProtocolVersion();
137 }
138
139 }