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