Class ServerStreamStringHandler


  • public class ServerStreamStringHandler
    extends AbstractIteratorHandler<ServerInputStream,​InitialStringRemoteIteratorData,​StringRemoteIteratorData>
    This class handles server input streams as iterators and converts their content to String. It assigns IDs to each server input stream and maps these IDs to the respective handler. This allows to use a UUID instead of the server input stream instance and thus simplifying interfaces not supporting Java objects, for instance web services.
    All methods of this class (except init(ServerInputStream, String, Integer) require a UUID identifying the corresponding server input stream. If the ID does not refer to a server input stream, an IllegalArgumentException will be thrown. Additionally, this handler keeps track of the alive time of known server input stream. As soon as their alive time elapses, they will be removed from this handler.

    This handler tries to minimise closing effort for server input stream. It will close them implicitly if

    • all content is transferred initially,
    • all content is retrieved at once (count is null),
    • the end of the data is reached, or
    • the server input stream contains no data at all.
    Usually null will be returned instead of StringRemoteIteratorData if there is no content (for the current iteration). If the server input stream is closed on-the-fly, a StringRemoteIteratorData will be returned even if there is no content.

    This class is thread-safe.

    • Constructor Detail

      • ServerStreamStringHandler

        public ServerStreamStringHandler​(LogService logService)
        Creates a new handler for server-side proxies returning data based on String.
        Parameters:
        logService - The log service to retrieve the logger for log messages from.
    • Method Detail

      • init

        @Valid
        public @Valid InitialStringRemoteIteratorData init​(@NotNull
                                                           @NotNull ServerInputStream sis,
                                                           String udtName,
                                                           @Positive
                                                           @Positive Integer count)
                                                    throws IOException
        Adds the designated input stream to this handler, wraps it in a server input stream, creates an ID for it and calls getNext(UUID, Integer) with the designated count. The added server input stream will be removed from this handler when it is closed. If all content of the server input stream is transferred now, the server input stream will be closed immediately.
        Parameters:
        sis - The server input stream to add to this handler. This will be coupled to created server input stream, so do not close it; clone it if required.
        udtName - The UDT name in case the designated server input stream represents a ServerUDTInputStream. Otherwise use null.
        count - The amount of content to retrieve in forward direction. This has to be > 0 or null. If this is null, all content of the designated input stream will be returned and converted to String.
        Returns:
        The initial meta data of the (UDT) server input stream and the next content of size n of the designated input stream represented as String. In case of an insufficient amount of content only the available content will be returned. If there is no content, null will be returned. If no count or Integer.MAX_VALUE is specified, all content of the designated input stream will be returned and the created server input stream will be closed.
        Throws:
        IOException - If reading content from the designated stream or detecting whether we reached the end of the stream failed, an IOException will be thrown. DataConsistencyException will be thrown.
        IllegalArgumentException - If count is non-positive (<= 0), an IllegalArgumentException will be thrown.
        IllegalStateException - If the created server input stream has been closed implicitly, an IllegalStateException will be thrown on server side.
        de.aristaflow.adept2.model.communication.ServiceConnectionException - If the created server input stream has been closed implicitly, a ServiceConnectionException will be thrown on client side.
      • getNext

        @Valid
        public @Valid StringRemoteIteratorData getNext​(@NotNull
                                                       @NotNull UUID sisId,
                                                       @Positive
                                                       @Positive Integer count)
                                                throws IOException
        Calls readContentAsString(ServerInputStream, int) on the designated server input stream. The alive time of the server input stream will be extended in case of implicit extension. The server input stream will be closed as soon as the last content is transferred.
        Parameters:
        sisId - The ID of the server input stream to forward the call to.
        count - The amount of content to retrieve in forward direction. This has to be > 0 or null. If this is null, all content of the designated server input stream will be returned and the server input stream will be closed.
        Returns:
        The next content of size n of the designated server input stream. In case of an insufficient amount of content remaining only the available content (or no content) will be returned. If no count or Integer.MAX_VALUE is specified, all content of the designated server input stream will be returned at once. If no count or Integer.MAX_VALUE is specified and there is no more content and the server input stream has been closed, null will be returned.
        Throws:
        IOException - If reading content from the designated stream or detecting whether we reached the end of the stream failed, an IOException will be thrown. DataConsistencyException will be thrown.
        IllegalArgumentException - If the designated ID does not refer to a ServerInputStream known by this handler, an IllegalArgumentException will be thrown.
        IllegalArgumentException - If count is non-positive (<= 0), an IllegalArgumentException will be thrown.
        IllegalStateException - If the designated server input stream has been closed implicitly, an IllegalStateException will be thrown on server side.
        de.aristaflow.adept2.model.communication.ServiceConnectionException - If the designated server input stream has been closed implicitly, a ServiceConnectionException will be thrown on client side.
      • getNext

        protected StringRemoteIteratorData getNext​(UUID sisId,
                                                   ServerInputStream sis,
                                                   Integer count)
                                            throws IOException
        Calls readContentAsString(ServerInputStream, int) on the designated server input stream. The alive time of the server input stream will be extended in case of implicit extension. It will be set to be closed as soon as the last content is transferred. It will also be set to be closed in case all (or the all of the remaining) content is provided at once. The caller of this method has to close the server input stream otherwise unless requesting further data!
        Parameters:
        sisId - The ID of the server input stream to forward the call to.
        sis - The server input stream to forward the call to.
        count - The amount of elements to retrieve in forward direction. This has to be > 0 or null. If this is null, all remaining content of the designated server input stream will be returned and the server input stream will be closed.
        Returns:
        The next content of size n of the designated server input stream. In case of an insufficient amount of content remaining only the available content (or no content) will be returned.
        Throws:
        IOException - If reading content from the designated stream or detecting whether we reached the end of the stream failed, an IOException will be thrown. DataConsistencyException will be thrown.
        IllegalArgumentException - If the designated ID does not refer to a ServerInputStream known by this handler, an IllegalArgumentException will be thrown.
        IllegalArgumentException - If count is non-positive (<= 0), an IllegalArgumentException will be thrown.
        IllegalStateException - If the designated server input stream has been closed implicitly, an IllegalStateException will be thrown on server side.
        de.aristaflow.adept2.model.communication.ServiceConnectionException - If the designated server input stream has been closed implicitly, a ServiceConnectionException will be thrown on client side.
      • keepAlive

        public long keepAlive​(@NotNull
                              @NotNull UUID iteratorId,
                              @NotNull
                              @NotNull Long aliveTime)
                       throws IOException
        Calls TimeTrackingSsProxy.keepAlive(long) on the designated server input stream.
        Parameters:
        iteratorId - The ID of the server input stream to forward the call to.
        aliveTime - The time in milliseconds to keep the designated server input stream alive that is not to close it automatically.
        Returns:
        The relative time the designated server input stream will be kept alive longer. 0 indicates immediate closing. A negative value represents the failure of extending the alive time, check TimeTrackingSsProxy.getRemainingAliveTime() in this case whether extending the keep alive time has been successful or not.
        Throws:
        IOException - If there are problems accessing the server input stream, an IOException will be thrown.
        IllegalArgumentException - If the designated ID does not refer to a ServerInputStream known by this handler, an IllegalArgumentException will be thrown.
        de.aristaflow.adept2.model.communication.ServiceConnectionException - If the designated server input stream has been closed implicitly, a ServiceConnectionException will be thrown on client side.
      • readContentAsString

        protected Pair<String,​Boolean> readContentAsString​(ServerInputStream sis,
                                                                 int len)
                                                          throws IOException
        Gets the next content of the designated length of the designated server input stream as string if available. If there is less remaining content, this remaining content will be returned. If there is no more content, null will be returned as first element of the pair.
        The second element will specify whether the stream is empty now if this can be determined. When requesting more content than available, this is obviously true. This tries to read the next byte of data from the stream if this supports marking/resetting. Otherwise, null will be returned as second element since the emptiness cannot be determined.
        Parameters:
        sis - The server input stream of which to get content.
        len - The amount of content to be retrieved from the designated server input stream. Note that due to encoding and/or transformation this length may differ from the actual length of the returned string.
        Returns:
        The content of (about) the designated length if available encoded as string as first element of the pair. If there is less content, only this will be returned, if there is no content, null will be returned. The second element specifies whether the stream is empty now (true), there is more content (false) or null if the complete request can be fulfilled but the stream does not support marking/resetting for peeking the next byte.
        Throws:
        IOException - if there are problems accessing the server input stream, an IOException will be thrown.