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.testing.nio;
29  
30  import java.io.IOException;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.atomic.AtomicBoolean;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.concurrent.BasicFuture;
37  import org.apache.hc.core5.concurrent.FutureCallback;
38  import org.apache.hc.core5.http.ConnectionClosedException;
39  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
40  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
41  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
42  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
43  import org.apache.hc.core5.http.nio.HandlerFactory;
44  import org.apache.hc.core5.http.nio.command.RequestExecutionCommand;
45  import org.apache.hc.core5.http.nio.command.ShutdownCommand;
46  import org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler;
47  import org.apache.hc.core5.http.protocol.HttpContext;
48  import org.apache.hc.core5.io.CloseMode;
49  import org.apache.hc.core5.io.ModalCloseable;
50  import org.apache.hc.core5.reactor.Command;
51  import org.apache.hc.core5.reactor.IOSession;
52  import org.apache.hc.core5.util.Asserts;
53  
54  /**
55   * Client endpoint that can be used to initiate HTTP message exchanges.
56   *
57   * @since 5.0
58   */
59  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
60  public final class ClientSessionEndpoint implements ModalCloseable {
61  
62      private final IOSession ioSession;
63      private final AtomicBoolean closed;
64  
65      public ClientSessionEndpoint(final IOSession ioSession) {
66          super();
67          this.ioSession = ioSession;
68          this.closed = new AtomicBoolean(false);
69      }
70  
71      public void execute(final Command command, final Command.Priority priority) {
72          ioSession.enqueue(command, priority);
73          if (!ioSession.isOpen()) {
74              command.cancel();
75          }
76      }
77  
78      public void execute(
79              final AsyncClientExchangeHandler exchangeHandler,
80              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
81              final HttpContext context) {
82          Asserts.check(!closed.get(), "Connection is already closed");
83          final Command executionCommand = new RequestExecutionCommand(exchangeHandler, pushHandlerFactory, null, context);
84          ioSession.enqueue(executionCommand, Command.Priority.NORMAL);
85          if (!ioSession.isOpen()) {
86              exchangeHandler.failed(new ConnectionClosedException());
87          }
88      }
89  
90      public void execute(
91              final AsyncClientExchangeHandler exchangeHandler,
92              final HttpContext context) {
93          execute(exchangeHandler, null, context);
94      }
95  
96      public <T> Future<T> execute(
97              final AsyncRequestProducer requestProducer,
98              final AsyncResponseConsumer<T> responseConsumer,
99              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
100             final HttpContext context,
101             final FutureCallback<T> callback) {
102         Asserts.check(!closed.get(), "Connection is already closed");
103         final BasicFuture<T> future = new BasicFuture<>(callback);
104         execute(new BasicClientExchangeHandler<>(requestProducer, responseConsumer,
105                 new FutureCallback<T>() {
106 
107                     @Override
108                     public void completed(final T result) {
109                         future.completed(result);
110                     }
111 
112                     @Override
113                     public void failed(final Exception ex) {
114                         future.failed(ex);
115                     }
116 
117                     @Override
118                     public void cancelled() {
119                         future.cancel();
120                     }
121 
122                 }),
123                 pushHandlerFactory, context);
124         return future;
125     }
126 
127     public <T> Future<T> execute(
128             final AsyncRequestProducer requestProducer,
129             final AsyncResponseConsumer<T> responseConsumer,
130             final HttpContext context,
131             final FutureCallback<T> callback) {
132         return execute(requestProducer, responseConsumer, null, context, callback);
133     }
134 
135     public <T> Future<T> execute(
136             final AsyncRequestProducer requestProducer,
137             final AsyncResponseConsumer<T> responseConsumer,
138             final FutureCallback<T> callback) {
139         return execute(requestProducer, responseConsumer, null, null, callback);
140     }
141 
142     public boolean isOpen() {
143         return !closed.get() && ioSession.isOpen();
144     }
145 
146     @Override
147     public void close(final CloseMode closeMode) {
148         if (closed.compareAndSet(false, true)) {
149             if (closeMode == CloseMode.GRACEFUL) {
150                 ioSession.enqueue(ShutdownCommand.GRACEFUL, Command.Priority.NORMAL);
151             } else {
152                 ioSession.close(closeMode);
153             }
154         }
155     }
156 
157     @Override
158     public void close() throws IOException {
159         if (closed.compareAndSet(false, true)) {
160             ioSession.enqueue(ShutdownCommand.GRACEFUL, Command.Priority.IMMEDIATE);
161         }
162     }
163 
164     @Override
165     public String toString() {
166         return ioSession.toString();
167     }
168 
169 }