org.fourthline.cling.transport.impl.jetty

## Class JettyServletContainer

- java.lang.Object
- - org.fourthline.cling.transport.impl.jetty.JettyServletContainer

- All Implemented Interfaces:
    ServletContainerAdapter

      public class JettyServletContainer
      extends Object
      implements ServletContainerAdapter

  A singleton wrapper of a `org.eclipse.jetty.server.Server`.

  This `ServletContainerAdapter` starts a Jetty 8 instance on its own and stops it. Only one single context and servlet is registered, to handle UPnP requests.

  This implementation works on Android, dependencies are `jetty-server` and `jetty-servlet` Maven modules.

  Author:
    Christian Bauer

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static JettyServletContainer` | `INSTANCE`
    `protected org.eclipse.jetty.server.Server` | `server`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `addConnector(String host, int port)` Might be called several times to set up the connectors.
    `static boolean` | `isConnectionOpen(javax.servlet.http.HttpServletRequest request)` Casts the request to a Jetty API and tries to write a space character to the output stream of the socket.
    `static boolean` | `isConnectionOpen(javax.servlet.http.HttpServletRequest request, byte[] heartbeat)`
    `void` | `registerServlet(String contextPath, javax.servlet.Servlet servlet)` Might be called several times to register (the same) handler for UPnP requests, should only register it once.
    `void` | `removeConnector(String host, int port)` Removes a previously added connector.
    `protected void` | `resetServer()`
    `void` | `setExecutorService(ExecutorService executorService)` Might be called several times to integrate the servlet container with Cling's executor configuration.
    `void` | `startIfNotRunning()` Start your servlet container if it isn't already running, might be called multiple times.
    `void` | `stopIfRunning()` Stop your servlet container if it's still running, might be called multiple times.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### INSTANCE

          public static final JettyServletContainer INSTANCE

    - #### server

          protected org.eclipse.jetty.server.Server server

  - ### Method Detail

    - #### setExecutorService

          public void setExecutorService(ExecutorService executorService)

      Description copied from interface: `ServletContainerAdapter`
      Might be called several times to integrate the servlet container with Cling's executor configuration. You can ignore this call if you want to configure the container's thread pooling independently from Cling. If you use the given Cling `ExecutorService`, make sure the Jetty container won't shut it down when `ServletContainerAdapter.stopIfRunning()` is called!
      Specified by:
        `setExecutorService` in interface `ServletContainerAdapter`
      Parameters:
        `executorService` - The service to use when spawning new servlet execution threads.

    - #### addConnector

          public int addConnector(String host,
                                  int port)
                           throws IOException

      Description copied from interface: `ServletContainerAdapter`
      Might be called several times to set up the connectors. This is the host/address and the port Cling expects to receive HTTP requests on. If you set up your HTTP server connectors elsewhere and ignore when Cling calls this method, make sure you configure Cling with the correct host/port of your servlet container.
      Specified by:
        `addConnector` in interface `ServletContainerAdapter`
      Parameters:
        `host` - The host address for the socket.
        `port` - The port, might be `-1` to bind to an ephemeral port.
      Returns:
        The actual registered local port.
      Throws:
        `IOException` - If the connector couldn't be opened to retrieve the registered local port.

    - #### removeConnector

          public void removeConnector(String host,
                                      int port)

      Description copied from interface: `ServletContainerAdapter`
      Removes a previously added connector. Implementation should close the corresponding listening server socket. It may stop the server when the last connector is removed.
      Specified by:
        `removeConnector` in interface `ServletContainerAdapter`
      Parameters:
        `host` - The host address of the socket.
        `port` - The port of the connector

    - #### registerServlet

          public void registerServlet(String contextPath,
                                      javax.servlet.Servlet servlet)

      Description copied from interface: `ServletContainerAdapter`
      Might be called several times to register (the same) handler for UPnP requests, should only register it once.
      Specified by:
        `registerServlet` in interface `ServletContainerAdapter`
      Parameters:
        `contextPath` - The context path prefix for all UPnP requests.
        `servlet` - The servlet handling all UPnP requests.

    - #### startIfNotRunning

          public void startIfNotRunning()

      Description copied from interface: `ServletContainerAdapter`
      Start your servlet container if it isn't already running, might be called multiple times.
      Specified by:
        `startIfNotRunning` in interface `ServletContainerAdapter`

    - #### stopIfRunning

          public void stopIfRunning()

      Description copied from interface: `ServletContainerAdapter`
      Stop your servlet container if it's still running, might be called multiple times.
      Specified by:
        `stopIfRunning` in interface `ServletContainerAdapter`

    - #### resetServer

          protected void resetServer()

    - #### isConnectionOpen

          public static boolean isConnectionOpen(javax.servlet.http.HttpServletRequest request)

      Casts the request to a Jetty API and tries to write a space character to the output stream of the socket.

      This space character might confuse the HTTP client. The Cling transports for Jetty Client and Apache HttpClient have been tested to work with space characters. Unfortunately, Sun JDK's HttpURLConnection does not gracefully handle any garbage in the HTTP request!

    - #### isConnectionOpen

          public static boolean isConnectionOpen(javax.servlet.http.HttpServletRequest request,
                                                 byte[] heartbeat)

Copyright © 2016 4th Line GmbH, Switzerland. All rights reserved.