View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.io;
20  
21  import java.net.Socket;
22  import java.nio.ByteBuffer;
23  
24  /**
25   * <p>A listener for raw network traffic within Jetty.</p>
26   * <p>{@link NetworkTrafficListener}s can be installed in a
27   * <code>org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector</code>,
28   * and are notified of the following network traffic events:</p>
29   * <ul>
30   * <li>Connection opened, when the server has accepted the connection from a remote client</li>
31   * <li>Incoming bytes, when the server receives bytes sent from a remote client</li>
32   * <li>Outgoing bytes, when the server sends bytes to a remote client</li>
33   * <li>Connection closed, when the server has closed the connection to a remote client</li>
34   * </ul>
35   * <p>{@link NetworkTrafficListener}s can be used to log the network traffic viewed by
36   * a Jetty server (for example logging to filesystem) for activities such as debugging
37   * or request/response cycles or for replaying request/response cycles to other servers.</p>
38   */
39  public interface NetworkTrafficListener
40  {
41      /**
42       * <p>Callback method invoked when a connection from a remote client has been accepted.</p>
43       * <p>The {@code socket} parameter can be used to extract socket address information of
44       * the remote client.</p>
45       *
46       * @param socket the socket associated with the remote client
47       */
48      public void opened(Socket socket);
49  
50      /**
51       * <p>Callback method invoked when bytes sent by a remote client arrived on the server.</p>
52       *
53       * @param socket the socket associated with the remote client
54       * @param bytes  the read-only buffer containing the incoming bytes
55       */
56      public void incoming(Socket socket, ByteBuffer bytes);
57  
58      /**
59       * <p>Callback method invoked when bytes are sent to a remote client from the server.</p>
60       * <p>This method is invoked after the bytes have been actually written to the remote client.</p>
61       *
62       * @param socket the socket associated with the remote client
63       * @param bytes  the read-only buffer containing the outgoing bytes
64       */
65      public void outgoing(Socket socket, ByteBuffer bytes);
66  
67      /**
68       * <p>Callback method invoked when a connection to a remote client has been closed.</p>
69       * <p>The {@code socket} parameter is already closed when this method is called, so it
70       * cannot be queried for socket address information of the remote client.<br>
71       * However, the {@code socket} parameter is the same object passed to {@link #opened(Socket)},
72       * so it is possible to map socket information in {@link #opened(Socket)} and retrieve it
73       * in this method.
74       *
75       * @param socket the (closed) socket associated with the remote client
76       */
77      public void closed(Socket socket);
78  
79      /**
80       * <p>A commodity class that implements {@link NetworkTrafficListener} with empty methods.</p>
81       */
82      public static class Adapter implements NetworkTrafficListener
83      {
84          public void opened(Socket socket)
85          {
86          }
87  
88          public void incoming(Socket socket, ByteBuffer bytes)
89          {
90          }
91  
92          public void outgoing(Socket socket, ByteBuffer bytes)
93          {
94          }
95  
96          public void closed(Socket socket)
97          {
98          }
99      }
100 }