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