Ejemplo n.º 1
0
  public ResponseContentSupplier performHttpRequest(
      TaskListener listener, RequestAction requestAction) throws InterruptedException, IOException {
    final PrintStream logger = listener.getLogger();
    logger.println("HttpMode: " + requestAction.getMode());
    logger.println(String.format("URL: %s", requestAction.getUrl()));
    for (HttpRequestNameValuePair header : requestAction.getHeaders()) {
      if (header.getName().equalsIgnoreCase("Authorization")) {
        logger.println(header.getName() + ": *****");
      } else {
        logger.println(header.getName() + ": " + header.getValue());
      }
    }

    DefaultHttpClient httpclient = new SystemDefaultHttpClient();

    HttpClientUtil clientUtil = new HttpClientUtil();
    HttpRequestBase httpRequestBase = clientUtil.createRequestBase(requestAction);

    HttpContext context = new BasicHttpContext();

    if (authentication != null && !authentication.isEmpty()) {
      final Authenticator auth = HttpRequestGlobalConfig.get().getAuthentication(authentication);
      if (auth == null) {
        throw new IllegalStateException(
            "Authentication '" + authentication + "' doesn't exist anymore");
      }

      logger.println("Using authentication: " + auth.getKeyName());
      auth.authenticate(httpclient, context, httpRequestBase, logger, timeout);
    }

    ResponseContentSupplier responseContentSupplier;
    try {
      final HttpResponse response =
          clientUtil.execute(httpclient, context, httpRequestBase, logger, timeout);
      // The HttpEntity is consumed by the ResponseContentSupplier
      responseContentSupplier = new ResponseContentSupplier(response);
    } catch (UnknownHostException uhe) {
      logger.println("Treating UnknownHostException(" + uhe.getMessage() + ") as 404 Not Found");
      responseContentSupplier =
          new ResponseContentSupplier("UnknownHostException as 404 Not Found", 404);
    } catch (SocketTimeoutException | ConnectException ce) {
      logger.println(
          "Treating " + ce.getClass() + "(" + ce.getMessage() + ") as 408 Request Timeout");
      responseContentSupplier =
          new ResponseContentSupplier(
              ce.getClass() + "(" + ce.getMessage() + ") as 408 Request Timeout", 408);
    }

    if (consoleLogResponseBody) {
      logger.println("Response: \n" + responseContentSupplier.getContent());
    }

    responseCodeIsValid(responseContentSupplier, logger);
    contentIsValid(responseContentSupplier, logger);

    return responseContentSupplier;
  }
Ejemplo n.º 2
0
 private void responseCodeIsValid(ResponseContentSupplier response, PrintStream logger)
     throws AbortException {
   List<Range<Integer>> ranges = DescriptorImpl.parseToRange(validResponseCodes);
   for (Range<Integer> range : ranges) {
     if (range.contains(response.getStatus())) {
       logger.println("Success code from " + range);
       return;
     }
   }
   throw new AbortException(
       "Fail: the returned code "
           + response.getStatus()
           + " is not in the accepted range: "
           + ranges);
 }
Ejemplo n.º 3
0
  private void logResponseToFile(
      FilePath workspace, PrintStream logger, ResponseContentSupplier responseContentSupplier)
      throws IOException, InterruptedException {

    FilePath outputFilePath = getOutputFilePath(workspace, logger);

    if (outputFilePath != null && responseContentSupplier.getContent() != null) {
      OutputStreamWriter write = null;
      try {
        write = new OutputStreamWriter(outputFilePath.write(), Charset.forName("UTF-8"));
        write.write(responseContentSupplier.getContent());
      } finally {
        if (write != null) {
          write.close();
        }
      }
    }
  }
Ejemplo n.º 4
0
  private void contentIsValid(ResponseContentSupplier responseContentSupplier, PrintStream logger)
      throws AbortException {
    if (Strings.isNullOrEmpty(validResponseContent)) {
      return;
    }

    String response = responseContentSupplier.getContent();
    if (!response.contains(validResponseContent)) {
      throw new AbortException(
          "Fail: Response with length "
              + response.length()
              + " doesn't contain '"
              + validResponseContent
              + "'");
    }
    return;
  }