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.http.impl.nio.client;
29  
30  import java.io.IOException;
31  import java.nio.channels.CancelledKeyException;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.impl.nio.DefaultNHttpClientConnection;
36  import org.apache.http.impl.nio.reactor.AbstractIODispatch;
37  import org.apache.http.nio.NHttpClientEventHandler;
38  import org.apache.http.nio.reactor.IOSession;
39  
40  class InternalIODispatch extends AbstractIODispatch<DefaultNHttpClientConnection> {
41  
42      private final Log log = LogFactory.getLog(InternalIODispatch.class);
43  
44      private final NHttpClientEventHandler handler;
45  
46      public InternalIODispatch(final NHttpClientEventHandler handler) {
47          super();
48          if (this.log.isDebugEnabled()) {
49              this.handler = new InternalRequestExecutor(this.log, handler);
50          } else {
51              this.handler = handler;
52          }
53      }
54  
55      @Override
56      protected DefaultNHttpClientConnection createConnection(final IOSession session) {
57          // This method should never get called under normal circumstances
58          // Connection object should be created by the connection manager
59          // upon completion of the session request
60          log.debug("Unexpected invocation of #createConnection");
61          session.close();
62          throw new CancelledKeyException();
63      }
64  
65      @Override
66      protected void onConnected(final DefaultNHttpClientConnection conn) {
67          final Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
68          try {
69              this.handler.connected(conn, attachment);
70          } catch (final Exception ex) {
71              this.handler.exception(conn, ex);
72          }
73      }
74  
75      @Override
76      protected void onClosed(final DefaultNHttpClientConnection conn) {
77          this.handler.closed(conn);
78      }
79  
80      @Override
81      protected void onException(final DefaultNHttpClientConnection conn, final IOException ex) {
82          this.handler.exception(conn, ex);
83      }
84  
85      @Override
86      protected void onInputReady(final DefaultNHttpClientConnection conn) {
87          conn.consumeInput(this.handler);
88      }
89  
90      @Override
91      protected void onOutputReady(final DefaultNHttpClientConnection conn) {
92          conn.produceOutput(this.handler);
93      }
94  
95      @Override
96      protected void onTimeout(final DefaultNHttpClientConnection conn) {
97          try {
98              this.handler.timeout(conn);
99          } catch (final Exception ex) {
100             this.handler.exception(conn, ex);
101         }
102     }
103 
104 }