Passing Data Streams to Operation Methods

TeaOperation methods can accept data streams (such as file data) from the server. The default GUI makes it easy to select a file and pass its data content to an agent method.

Procedure

  1. Define an operation with a parameter of type javax.activation.DataSource, and annotate it as a @TeaParam.
    @TeaOperation(name = "upload", description = "Upload File")
    public void uploadFile(
        @TeaParam(name = "filepath", description = "File to Upload")
           final DataSource fileDataSource)
      throws IOException {
      ...
    }
    The default GUI lets a user select a file from the local file system. The server passes the file's contents to the agent as an argument to the operation method. The agent specifies this behavior with the example code shown in bold above—specifically, the parameter type.
  2. Use the data stream in the method body. In this example, the method writes the data stream to a temporary file, effectively transferring the data from the user's host computer to the agent's host computer.
    {
      byte[] buffer = new byte[8 * 1024];
      String name = fileDataSource.getName(); 
      FileOutputStream writerStream = new FileOutputStream(name, true);
      final InputStream inputStream = fileDataSource.getInputStream();
      int read;
      while((read = inputStream.read(buffer)) > 0)
      {
         writerStream.write(buffer, 0, read);
      }
      inputStream.close();
      writerStream.flush();
      writerStream.close();
    }