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.reactor;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.nio.reactor.IOEventDispatch;
34  import org.apache.http.nio.reactor.IOSession;
35  import org.apache.http.nio.reactor.ssl.SSLIOSession;
36  import org.apache.http.util.Asserts;
37  
38  /**
39   * Abstract {@link IOEventDispatch} implementation that supports both plain (non-encrypted)
40   * and SSL encrypted HTTP connections.
41   *
42   * @param <T> the connection type.
43   * @since 4.2
44   */
45  @Immutable // provided injected dependencies are immutable
46  public abstract class AbstractIODispatch<T> implements IOEventDispatch {
47  
48      protected abstract T createConnection(IOSession session);
49  
50      protected abstract void onConnected(T conn);
51  
52      protected abstract void onClosed(T conn);
53  
54      protected abstract void onException(T conn, IOException ex);
55  
56      protected abstract void onInputReady(T conn);
57  
58      protected abstract void onOutputReady(T conn);
59  
60      protected abstract void onTimeout(T conn);
61  
62      private void ensureNotNull(final T conn) {
63          Asserts.notNull(conn, "HTTP connection");
64      }
65  
66      public void connected(final IOSession session) {
67          @SuppressWarnings("unchecked")
68          T conn = (T) session.getAttribute(IOEventDispatch.CONNECTION_KEY);
69          try {
70              if (conn == null) {
71                  conn = createConnection(session);
72                  session.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
73              }
74              onConnected(conn);
75              final SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
76                      SSLIOSession.SESSION_KEY);
77              if (ssliosession != null) {
78                  try {
79                      synchronized (ssliosession) {
80                          if (!ssliosession.isInitialized()) {
81                              ssliosession.initialize();
82                          }
83                      }
84                  } catch (final IOException ex) {
85                      onException(conn, ex);
86                      ssliosession.shutdown();
87                  }
88              }
89          } catch (final RuntimeException ex) {
90              session.shutdown();
91              throw ex;
92          }
93      }
94  
95      public void disconnected(final IOSession session) {
96          @SuppressWarnings("unchecked")
97          final
98          T conn = (T) session.getAttribute(IOEventDispatch.CONNECTION_KEY);
99          if (conn != null) {
100             onClosed(conn);
101         }
102     }
103 
104     public void inputReady(final IOSession session) {
105         @SuppressWarnings("unchecked")
106         final
107         T conn = (T) session.getAttribute(IOEventDispatch.CONNECTION_KEY);
108         try {
109             ensureNotNull(conn);
110             final SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
111                     SSLIOSession.SESSION_KEY);
112             if (ssliosession == null) {
113                 onInputReady(conn);
114             } else {
115                 try {
116                     if (!ssliosession.isInitialized()) {
117                         ssliosession.initialize();
118                     }
119                     if (ssliosession.isAppInputReady()) {
120                         onInputReady(conn);
121                     }
122                     ssliosession.inboundTransport();
123                 } catch (final IOException ex) {
124                     onException(conn, ex);
125                     ssliosession.shutdown();
126                 }
127             }
128         } catch (final RuntimeException ex) {
129             session.shutdown();
130             throw ex;
131         }
132     }
133 
134     public void outputReady(final IOSession session) {
135         @SuppressWarnings("unchecked")
136         final
137         T conn = (T) session.getAttribute(IOEventDispatch.CONNECTION_KEY);
138         try {
139             ensureNotNull(conn);
140             final SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
141                     SSLIOSession.SESSION_KEY);
142             if (ssliosession == null) {
143                 onOutputReady(conn);
144             } else {
145                 try {
146                     if (!ssliosession.isInitialized()) {
147                         ssliosession.initialize();
148                     }
149                     if (ssliosession.isAppOutputReady()) {
150                         onOutputReady(conn);
151                     }
152                     ssliosession.outboundTransport();
153                 } catch (final IOException ex) {
154                     onException(conn, ex);
155                     ssliosession.shutdown();
156                 }
157             }
158         } catch (final RuntimeException ex) {
159             session.shutdown();
160             throw ex;
161         }
162     }
163 
164     public void timeout(final IOSession session) {
165         @SuppressWarnings("unchecked")
166         final
167         T conn = (T) session.getAttribute(IOEventDispatch.CONNECTION_KEY);
168         try {
169             final SSLIOSession ssliosession = (SSLIOSession) session.getAttribute(
170                     SSLIOSession.SESSION_KEY);
171             ensureNotNull(conn);
172             onTimeout(conn);
173             if (ssliosession != null) {
174                 synchronized (ssliosession) {
175                     if (ssliosession.isOutboundDone() && !ssliosession.isInboundDone()) {
176                         // The session failed to terminate cleanly
177                         ssliosession.shutdown();
178                     }
179                 }
180             }
181         } catch (final RuntimeException ex) {
182             session.shutdown();
183             throw ex;
184         }
185     }
186 
187 }