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