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.hc.core5.http.nio.command;
29
30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import org.apache.hc.core5.annotation.Internal;
33 import org.apache.hc.core5.concurrent.CancellableDependency;
34 import org.apache.hc.core5.http.RequestNotExecutedException;
35 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
36 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
37 import org.apache.hc.core5.http.nio.HandlerFactory;
38 import org.apache.hc.core5.http.protocol.HttpContext;
39 import org.apache.hc.core5.util.Args;
40
41
42
43
44
45
46 @Internal
47 public final class RequestExecutionCommand extends ExecutableCommand {
48
49 private final AsyncClientExchangeHandler exchangeHandler;
50 private final HandlerFactory<AsyncPushConsumer> pushHandlerFactory;
51 private final CancellableDependency cancellableDependency;
52 private final HttpContext context;
53 private final AtomicBoolean failed;
54
55 public RequestExecutionCommand(
56 final AsyncClientExchangeHandler exchangeHandler,
57 final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
58 final CancellableDependency cancellableDependency,
59 final HttpContext context) {
60 this.exchangeHandler = Args.notNull(exchangeHandler, "Handler");
61 this.pushHandlerFactory = pushHandlerFactory;
62 this.cancellableDependency = cancellableDependency;
63 this.context = context;
64 this.failed = new AtomicBoolean();
65 }
66
67 public RequestExecutionCommand(
68 final AsyncClientExchangeHandler exchangeHandler,
69 final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
70 final HttpContext context) {
71 this(exchangeHandler, pushHandlerFactory, null, context);
72 }
73
74 public RequestExecutionCommand(
75 final AsyncClientExchangeHandler exchangeHandler,
76 final HttpContext context) {
77 this(exchangeHandler, null, null, context);
78 }
79
80 public AsyncClientExchangeHandler getExchangeHandler() {
81 return exchangeHandler;
82 }
83
84 public HandlerFactory<AsyncPushConsumer> getPushHandlerFactory() {
85 return pushHandlerFactory;
86 }
87
88 @Override
89 public CancellableDependency getCancellableDependency() {
90 return cancellableDependency;
91 }
92
93 public HttpContext getContext() {
94 return context;
95 }
96
97 @Override
98 public void failed(final Exception ex) {
99 if (failed.compareAndSet(false, true)) {
100 try {
101 exchangeHandler.failed(ex);
102 } finally {
103 exchangeHandler.releaseResources();
104 }
105 }
106 }
107
108 @Override
109 public boolean cancel() {
110 if (failed.compareAndSet(false, true)) {
111 try {
112 exchangeHandler.failed(new RequestNotExecutedException());
113 return true;
114 } finally {
115 exchangeHandler.releaseResources();
116 }
117 }
118 return false;
119 }
120
121 }