org.fourthline.cling.transport.impl

## Class StreamClientImpl

- java.lang.Object
- - org.fourthline.cling.transport.impl.StreamClientImpl

- All Implemented Interfaces:
    StreamClient

      public class StreamClientImpl
      extends Object
      implements StreamClient

  Default implementation based on the JDK's `HttpURLConnection`.

  This class works around a serious design issue in the SUN JDK, so it will not work on any JDK that doesn't offer the `sun.net.www.protocol.http.HttpURLConnection` implementation.

  This implementation DOES NOT WORK on Android. Read the Cling manual for alternatives for Android.

  This implementation DOES NOT support Cling's server-side heartbeat for connection checking. Any data returned by a server has to be "valid HTTP", checked in Sun's HttpClient with:

  `ret = b[0] == 'H' && b[1] == 'T' && b[2] == 'T' && b[3] == 'P' && b[4] == '/' && b[5] == '1' && b[6] == '.';`

  Hence, if you are using this client, don't call Cling's `RemoteClientInfo.isRequestCancelled()` function on your server to send a heartbeat to the client!

  Author:
    Christian Bauer

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected StreamClientConfigurationImpl` | `configuration`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `StreamClientImpl(StreamClientConfigurationImpl configuration)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `applyHeaders(HttpURLConnection urlConnection, org.seamless.http.Headers headers)`
    `protected void` | `applyRequestBody(HttpURLConnection urlConnection, StreamRequestMessage requestMessage)`
    `protected void` | `applyRequestProperties(HttpURLConnection urlConnection, StreamRequestMessage requestMessage)`
    `protected StreamResponseMessage` | `createResponse(HttpURLConnection urlConnection, InputStream inputStream)`
    `StreamClientConfigurationImpl` | `getConfiguration()`
    `StreamResponseMessage` | `sendRequest(StreamRequestMessage requestMessage)` Sends the given request via TCP (HTTP) and returns the response.
    `void` | `stop()` Stops the service, closes any connection pools etc.

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

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

- - ### Field Detail

    - #### configuration

          protected final StreamClientConfigurationImpl configuration

  - ### Constructor Detail

    - #### StreamClientImpl

          public StreamClientImpl(StreamClientConfigurationImpl configuration)
                           throws InitializationException

      Throws:
        `InitializationException`

  - ### Method Detail

    - #### getConfiguration

          public StreamClientConfigurationImpl getConfiguration()

      Specified by:
        `getConfiguration` in interface `StreamClient`
      Returns:
        This service's configuration.

    - #### sendRequest

          public StreamResponseMessage sendRequest(StreamRequestMessage requestMessage)

      Description copied from interface: `StreamClient`
      Sends the given request via TCP (HTTP) and returns the response.

      This method must implement expiration of timed out requests using the `StreamClientConfiguration` settings. When a request expires, a `null` response will be returned.

      This method will always try to complete execution without throwing an exception. It will return `null` if an error occurs, and optionally log any exception messages.

      The rules for logging are:

      - If the caller interrupts the calling thread, log at `FINE`.
      - If the request expires because the timeout has been reached, log at `INFO` level.
      - If another error occurs, log at `WARNING` level

      This method is required to add a `Host` HTTP header to the outgoing HTTP request, even if the given `StreamRequestMessage` does not contain such a header.

      This method will add the `User-Agent` HTTP header to the outgoing HTTP request if the given message did not already contain such a header. You can set this default value in your `StreamClientConfiguration`.

      Specified by:
        `sendRequest` in interface `StreamClient`
      Parameters:
        `requestMessage` - The message to send.
      Returns:
        The response or `null` if no response has been received or an error occurred.

    - #### stop

          public void stop()

      Description copied from interface: `StreamClient`
      Stops the service, closes any connection pools etc.
      Specified by:
        `stop` in interface `StreamClient`

    - #### applyRequestProperties

          protected void applyRequestProperties(HttpURLConnection urlConnection,
                                    StreamRequestMessage requestMessage)

    - #### applyHeaders

          protected void applyHeaders(HttpURLConnection urlConnection,
                          org.seamless.http.Headers headers)

    - #### applyRequestBody

          protected void applyRequestBody(HttpURLConnection urlConnection,
                              StreamRequestMessage requestMessage)
                                   throws IOException

      Throws:
        `IOException`

    - #### createResponse

          protected StreamResponseMessage createResponse(HttpURLConnection urlConnection,
                                             InputStream inputStream)
                                                  throws Exception

      Throws:
        `Exception`

Copyright © 2014 4th Line GmbH, Switzerland. All Rights Reserved.