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 package org.apache.hc.client5.http.classic.methods;
28
29 import java.net.URI;
30 import java.util.concurrent.atomic.AtomicMarkableReference;
31
32 import org.apache.hc.client5.http.config.RequestConfig;
33 import org.apache.hc.core5.concurrent.Cancellable;
34 import org.apache.hc.core5.concurrent.CancellableDependency;
35 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
36
37 public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpUriRequest, CancellableDependency {
38
39 private static final long serialVersionUID = 1L;
40
41 private final AtomicMarkableReference<Cancellable> cancellableRef;
42 private RequestConfig requestConfig;
43
44 public HttpUriRequestBase(final String method, final URI requestUri) {
45 super(method, requestUri);
46 this.cancellableRef = new AtomicMarkableReference<>(null, false);
47 }
48
49 @Override
50 public boolean cancel() {
51 while (!cancellableRef.isMarked()) {
52 final Cancellable actualCancellable = cancellableRef.getReference();
53 if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
54 if (actualCancellable != null) {
55 actualCancellable.cancel();
56 }
57 return true;
58 }
59 }
60 return false;
61 }
62
63 @Override
64 public boolean isCancelled() {
65 return cancellableRef.isMarked();
66 }
67
68
69
70
71 @Override
72 public void setDependency(final Cancellable cancellable) {
73 final Cancellable actualCancellable = cancellableRef.getReference();
74 if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
75 cancellable.cancel();
76 }
77 }
78
79
80
81
82
83
84 public void reset() {
85 for (;;) {
86 final boolean marked = cancellableRef.isMarked();
87 final Cancellable actualCancellable = cancellableRef.getReference();
88 if (actualCancellable != null) {
89 actualCancellable.cancel();
90 }
91 if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
92 break;
93 }
94 }
95 }
96
97 @Override
98 public void abort() throws UnsupportedOperationException {
99 cancel();
100 }
101
102 @Override
103 public boolean isAborted() {
104 return isCancelled();
105 }
106
107 public void setConfig(final RequestConfig requestConfig) {
108 this.requestConfig = requestConfig;
109 }
110
111 @Override
112 public RequestConfig getConfig() {
113 return requestConfig;
114 }
115
116 @Override
117 public String toString() {
118 final StringBuilder sb = new StringBuilder();
119 sb.append(getMethod()).append(" ").append(getRequestUri());
120 return sb.toString();
121 }
122
123 }