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 org.apache.hc.core5.annotation.Internal;
31  import org.apache.hc.core5.http.ConnectionClosedException;
32  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
33  import org.apache.hc.core5.reactor.Command;
34  import org.apache.hc.core5.reactor.IOSession;
35  import org.apache.hc.core5.util.Args;
36  
37  /**
38   * {@link Command} utility methods.
39   *
40   * @since 5.0
41   */
42  @Internal
43  public final class CommandSupport {
44  
45      /**
46       * Fails all pending session {@link Command}s.
47       */
48      static public void failCommands(final IOSession ioSession, final Exception ex) {
49          Args.notNull(ioSession, "I/O session");
50          Command command;
51          while ((command = ioSession.poll()) != null) {
52              if (command instanceof RequestExecutionCommand) {
53                  final AsyncClientExchangeHandler exchangeHandler = ((RequestExecutionCommand) command).getExchangeHandler();
54                  try {
55                      exchangeHandler.failed(ex);
56                  } finally {
57                      exchangeHandler.releaseResources();
58                  }
59              } else {
60                  command.cancel();
61              }
62          }
63      }
64  
65      /**
66       * Cancels all pending session {@link Command}s.
67       */
68      static public void cancelCommands(final IOSession ioSession) {
69          Args.notNull(ioSession, "I/O session");
70          Command command;
71          while ((command = ioSession.poll()) != null) {
72              if (command instanceof RequestExecutionCommand) {
73                  final AsyncClientExchangeHandler exchangeHandler = ((RequestExecutionCommand) command).getExchangeHandler();
74                  try {
75                      if (!ioSession.isOpen()) {
76                          exchangeHandler.failed(new ConnectionClosedException());
77                      } else {
78                          exchangeHandler.cancel();
79                      }
80                  } finally {
81                      exchangeHandler.releaseResources();
82                  }
83              } else {
84                  command.cancel();
85              }
86          }
87      }
88  
89  }