/**
   * Cache HTTP headers in message.
   *
   * @param message the current message
   */
  protected void setHeaders(Message message) {
    Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
    copyRequestHeaders(message, requestHeaders);
    message.put(Message.PROTOCOL_HEADERS, requestHeaders);

    if (requestHeaders.containsKey("Authorization")) {
      List<String> authorizationLines = requestHeaders.get("Authorization");
      String credentials = authorizationLines.get(0);
      String authType = credentials.split(" ")[0];
      if ("Basic".equals(authType)) {
        String authEncoded = credentials.split(" ")[1];
        try {
          String authDecoded = new String(Base64Utility.decode(authEncoded));
          String authInfo[] = authDecoded.split(":");
          String username = (authInfo.length > 0) ? authInfo[0] : "";
          // Below line for systems that blank out password after authentication;
          // see CXF-1495 for more info
          String password = (authInfo.length > 1) ? authInfo[1] : "";
          AuthorizationPolicy policy = new AuthorizationPolicy();
          policy.setUserName(username);
          policy.setPassword(password);

          message.put(AuthorizationPolicy.class, policy);
        } catch (Base64Exception ex) {
          // ignore, we'll leave things alone.  They can try decoding it themselves
        }
      }
    }

    if (LOG.isLoggable(Level.FINE)) {
      LOG.log(Level.FINE, "Request Headers: " + requestHeaders.toString());
    }
  }
Example #2
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;
  }
 public void handleMessage(Message message) throws Fault {
   ExchangeMetrics m = message.getExchange().get(ExchangeMetrics.class);
   if (m != null) {
     Map<String, List<String>> h =
         CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
     String auth = h.get("Authorization").toString();
     auth = auth.substring(auth.indexOf(' ') + 1);
     try {
       auth = new String(Base64Utility.decode(auth));
     } catch (Base64Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     auth = auth.substring(0, auth.indexOf(':'));
     Customer c = customers.get(auth);
     if (c == null) {
       throw new RuntimeException("Not authorized");
     }
     m.addContext(c.getMetricsContext(registry));
     message.getExchange().put(Customer.class, c);
   }
 }