View Javadoc
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.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   * Request execution command.
43   *
44   * @since 5.0
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 }