Class AbstractRestProcessService<I,O>

Type Parameters:
I - the input data type
O - the output data type
All Implemented Interfaces:
MonitoringService, ParameterConfigurerProvider, Service, ServiceBase

public abstract class AbstractRestProcessService<I,O> extends AbstractProcessService<I,String,String,O>
Implements an abstract asynchronous process-based service which require a Rest-based communication with the actual service process for a single pair of input-output types, e.g., via JSON. Call setupConnectionManager() at the end of your AbstractService.start() method implementation.
Author:
Marcel Nöhre
  • Field Details

    • connection

      private HttpURLConnection connection
    • executor

      private ExecutorService executor
    • client

      private de.iip_ecosphere.platform.support.http.HttpClient client
  • Constructor Details

    • AbstractRestProcessService

      protected AbstractRestProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String,O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService yaml)
      Creates an instance of the service with the required type translators.
      Parameters:
      inTrans - the input translator
      outTrans - the output translator
      callback - called when data from the service is available
      yaml - the service description
  • Method Details

    • getApiPath

      protected abstract String getApiPath()
      Returns the HTTP/HTTPS path to the REST API.
      Returns:
      the path
    • getConnection

      protected HttpURLConnection getConnection()
      Returns the connection instance.
      Returns:
      the connection instance, may be null if service was not started before
    • getBearerToken

      protected abstract String getBearerToken()
      Returns the bearer token for authentication.
      Returns:
      the bearer token, may be null for none
    • getNewConnectionInstance

      protected HttpURLConnection getNewConnectionInstance() throws IOException
      Creates a new connection and overwrites the existing connection instance.
      Returns:
      the created connection
      Throws:
      IOException - if creating the connection fails
    • setupConnectionManager

      protected void setupConnectionManager()
      Sets up the connection manager and creates a new httpClient based on a connection pool.
    • getNewConnectionInstanceQuiet

      protected void getNewConnectionInstanceQuiet(boolean changeState)
      Get Connection to local server.
      Parameters:
      changeState - whether the state shall be changed if the connection creation fails
    • waitForConnection

      protected void waitForConnection()
      Waits until a connection is established.
    • process

      public void process(I data) throws IOException
      Description copied from class: AbstractProcessService
      Requests to process the given data item.
      Specified by:
      process in class AbstractProcessService<I,String,String,O>
      Parameters:
      data - the data to process
      Throws:
      IOException - if processing/transferring to the service fails
    • process

      public void process(I data, String inTypeName) throws IOException
      Processes input.
      Parameters:
      data - the data to be processed
      inTypeName - the symbolic input type name, may be empty if just one is handled
      Throws:
      IOException - if processing fails
    • toSendString

      protected String toSendString(I data, String inTypeName) throws IOException
      Turns data to an input string of the underlying REST service.
      Parameters:
      data - the data
      inTypeName - the symbolic input type name, may be empty if just one is handled
      Returns:
      the translated input string, by default via adjustRestQuery(String, String) and AbstractProcessService.getInputTranslator()
      Throws:
      IOException - if processing the input fails
    • handleReception

      protected void handleReception(String data)
      Parameters:
      data - the information received from the underlying REST service
    • adjustRestQuery

      protected abstract String adjustRestQuery(String input, String inTypeName)
      Adjusts the input produced by AbstractProcessService.getInputTranslator() to the actual receiver.
      Parameters:
      input - the input
      inTypeName - the symbolic input type name, may be empty if just one is handled
      Returns:
      the adjusted input
    • adjustRestResponse

      protected abstract String adjustRestResponse(String response)
      The rest response.
      Parameters:
      response - the received response
      Returns:
      the adjusted response
    • handleInputStream

      protected void handleInputStream(InputStream in)
      Description copied from class: AbstractProcessService
      Handles the input stream upon process creation.
      Specified by:
      handleInputStream in class AbstractProcessService<I,String,String,O>
      Parameters:
      in - the process input stream
    • stop

      protected ServiceState stop()
      Description copied from class: AbstractProcessService
      Preliminary: Stops the service and the background process.
      Overrides:
      stop in class AbstractProcessService<I,String,String,O>
      Returns:
      the state to transition to, may be null for none