コード例 #1
0
  public String getQueryString(String contentEncoding) {
    // Check if the sampler has a specified content encoding
    if (JOrphanUtils.isBlank(contentEncoding)) {
      // We use the encoding which should be used according to the HTTP spec, which is UTF-8
      contentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
    }
    StringBuilder buf = new StringBuilder();
    PropertyIterator iter = getQueryStringParameters().iterator();
    boolean first = true;
    while (iter.hasNext()) {
      HTTPArgument item = null;
      Object objectValue = iter.next().getObjectValue();
      try {
        item = (HTTPArgument) objectValue;
      } catch (ClassCastException e) {
        item = new HTTPArgument((Argument) objectValue);
      }
      final String encodedName = item.getEncodedName();
      if (encodedName.length() == 0) {
        continue; // Skip parameters with a blank name (allows use of optional variables in
                  // parameter lists)
      }
      if (!first) {
        buf.append(QRY_SEP);
      } else {
        first = false;
      }
      buf.append(encodedName);
      if (item.getMetaData() == null) {
        buf.append(ARG_VAL_SEP);
      } else {
        buf.append(item.getMetaData());
      }

      // Encode the parameter value in the specified content encoding
      try {
        buf.append(item.getEncodedValue(contentEncoding));
      } catch (UnsupportedEncodingException e) {
        log.warn(
            "Unable to encode parameter in encoding "
                + contentEncoding
                + ", parameter value not included in query string");
      }
    }
    return buf.toString();
  }
コード例 #2
0
  @Override
  public SampleResult sample(Entry entry) {
    ServiceSocket socket = null;
    SampleResult sampleResult = new SampleResult();
    sampleResult.setSampleLabel(getName());
    sampleResult.setDataEncoding(getContentEncoding());

    StringBuilder errorList = new StringBuilder();
    errorList.append("\n\n[Problems]\n");

    boolean isOK = false;

    String payloadMessage = getRequestPayload();
    sampleResult.setSamplerData(payloadMessage);
    sampleResult.sampleStart();

    try {
      socket = getConnectionSocket();
      if (socket == null) {
        sampleResult.setResponseCode("500");
        sampleResult.setSuccessful(false);
        sampleResult.sampleEnd();
        sampleResult.setResponseMessage(errorList.toString());
        errorList.append(" - Connection couldn't be opened").append("\n");
        return sampleResult;
      }

      if (!payloadMessage.isEmpty()) {
        socket.sendMessage(payloadMessage);
      }

      int responseTimeout = Integer.parseInt(getResponseTimeout());
      socket.awaitClose(responseTimeout, TimeUnit.MILLISECONDS);

      if (socket.getResponseMessage() == null || socket.getResponseMessage().isEmpty()) {
        sampleResult.setResponseCode("204");
      }

      if (socket.getError() != 0) {
        isOK = false;
        sampleResult.setResponseCode(socket.getError().toString());
      } else {
        sampleResult.setResponseCodeOK();
        isOK = true;
      }

      sampleResult.setResponseData(socket.getResponseMessage(), getContentEncoding());

    } catch (URISyntaxException e) {
      errorList
          .append(" - Invalid URI syntax: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (IOException e) {
      errorList
          .append(" - IO Exception: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (NumberFormatException e) {
      errorList
          .append(" - Cannot parse number: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (InterruptedException e) {
      errorList
          .append(" - Execution interrupted: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (Exception e) {
      errorList
          .append(" - Unexpected error: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    }

    sampleResult.sampleEnd();
    sampleResult.setSuccessful(isOK);

    String logMessage = (socket != null) ? socket.getLogMessage() : "";
    sampleResult.setResponseMessage(logMessage + errorList);
    return sampleResult;
  }