Example #1
0
  public CommandOutput getCommandOutput(String shellId, String commandId) {
    String messageId = UUID.randomUUID().toString();
    HashMap<String, String> options = null; // new HashMap<>();

    prepareRequest(URI_ACTION_SHELL_RECEIVE, shellId, messageId, options);

    // add SOAP body
    Receive recv = new Receive();
    DesiredStreamType stream = new DesiredStreamType();
    stream.setCommandId(commandId);
    stream.getValue().add("stdout stderr");
    recv.setDesiredStream(stream);

    CommandOutput commandOutput = new CommandOutput();

    // fetch output in a loop until command finishes
    while (true) {
      ReceiveResponse response = wsmanService.receive(recv); // ws call

      for (StreamType streamItem : response.getStream()) {
        if (streamItem.getName().equals("stdout")) {
          try {
            commandOutput.std_out += Base64Utility.decode(new String(streamItem.getValue()));
          } catch (Base64Exception ex) {
            Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex);
          }
        } else if (streamItem.getName().equals("stderr")) {
          try {
            commandOutput.std_err += Base64Utility.decode(new String(streamItem.getValue()));
          } catch (Base64Exception ex) {
            Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex);
          }
        }
      }

      // quit loop when command output says 'done'
      if (response.getCommandState().getState().equals(COMMAND_STATE_DONE)) {
        commandOutput.statusCode = response.getCommandState().getExitCode().intValue();
        break;
      }
    }

    return commandOutput;
  }