View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import java.net.Socket;
22  
23  import org.eclipse.jetty.io.ChannelEndPoint;
24  import org.eclipse.jetty.io.Connection;
25  import org.eclipse.jetty.io.Connection.Listener;
26  import org.eclipse.jetty.io.ssl.SslConnection;
27  import org.eclipse.jetty.io.ssl.SslConnection.DecryptedEndPoint;
28  import org.eclipse.jetty.io.EndPoint;
29  
30  
31  /* ------------------------------------------------------------ */
32  /** 
33   * A Connection Lister for customization of SocketConnections.
34   * <p>
35   * Instances of this listener may be added to a {@link Connector} (or 
36   * {@link ConnectionFactory}) so that they are applied to all connections
37   * for that connector (or protocol) and thus allow additional Socket
38   * configuration to be applied by implementing {@link #customize(Socket, Class, boolean)}
39   */
40  public class SocketCustomizationListener implements Listener
41  {
42      private final boolean _ssl;
43      
44      /**
45       * Construct with SSL unwrapping on.
46       */
47      public SocketCustomizationListener()
48      {
49          this(true);
50      }
51      
52      /**
53       * @param ssl If True, then a Socket underlying an SSLConnection is unwrapped
54       * and notified.
55       */
56      public SocketCustomizationListener(boolean ssl)
57      {
58          _ssl=ssl;
59      }
60  
61      @Override
62      public void onOpened(Connection connection)
63      {
64          EndPoint endp = connection.getEndPoint();
65          boolean ssl=false;
66          
67          if (_ssl && endp instanceof DecryptedEndPoint)
68          {
69              endp = ((DecryptedEndPoint)endp).getSslConnection().getEndPoint();
70              ssl=true;
71          }
72          
73          if (endp instanceof ChannelEndPoint) 
74          {
75              Socket socket = ((ChannelEndPoint)endp).getSocket();
76              customize(socket,connection.getClass(),ssl);
77          }
78      }
79  
80      /* ------------------------------------------------------------ */
81      /** This method may be extended to configure a socket on open 
82       * events.
83       * @param socket The Socket to configure
84       * @param connection The class of the connection (The socket may be wrapped
85       * by an {@link SslConnection} prior to this connection).
86       * @param ssl True if the socket is wrapped with an SslConnection
87       */
88      protected void customize(Socket socket, Class<? extends Connection> connection, boolean ssl)
89      {
90      }
91  
92      @Override
93      public void onClosed(Connection connection)
94      {
95      }
96  
97  }