@Override
  public final HttpResponse invoke(Q query, String endpoint) throws InvocationException {
    Preconditions.checkNotNull(query);
    Preconditions.checkNotNull(endpoint);

    HttpRequestBase method = null;
    HttpEntity response = null;
    try {
      method = getHttpMethod(query, endpoint);
      method.setParams(getHttpClientParams(query));

      org.apache.http.HttpResponse httpResponse = httpClient.execute(method);
      response = httpResponse.getEntity();
      return HttpResponse.create(
          httpResponse.getStatusLine().getStatusCode(), EntityUtils.toString(response));
    } catch (Exception e) {
      if (method != null) {
        log.debug(
            "Error during invocation with URL: "
                + method.getURI()
                + ", endpoint: "
                + endpoint
                + ", query: "
                + query,
            e);
      } else {
        log.debug("Error during invocation with: endpoint: " + endpoint + ", query: " + query, e);
      }
      throw new InvocationException("InvocationException : ", e);
    } finally {
      EntityUtils.consumeQuietly(response);
    }
  }
Ejemplo n.º 2
0
  private HttpResponse getHTTPResponse() throws IOException, NullPointerException {
    if (fileURI == null) throw new NullPointerException("No file URI specified");

    HttpClient client = new DefaultHttpClient();
    BasicHttpContext localContext = new BasicHttpContext();

    // Clear down the local cookie store every time to make sure we don't have any left over cookies
    // influencing the test
    localContext.setAttribute(ClientContext.COOKIE_STORE, null);

    LOG.info("Mimic WebDriver cookie state: " + mimicWebDriverCookieState);
    if (mimicWebDriverCookieState) {
      localContext.setAttribute(
          ClientContext.COOKIE_STORE, mimicCookieState(driver.manage().getCookies()));
    }

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);
    HttpParams httpRequestParameters = requestMethod.getParams();
    httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    requestMethod.setParams(httpRequestParameters);
    // TODO if post send map of variables, also need to add a post map setter

    LOG.info("Sending " + httpRequestMethod.toString() + " request for: " + fileURI);
    return client.execute(requestMethod, localContext);
  }
  /**
   * Perform an HTTP Status check and return the response code
   *
   * @return
   * @throws IOException
   */
  public int getHTTPStatusCode() throws IOException {

    HttpClient client = new DefaultHttpClient();
    BasicHttpContext localContext = new BasicHttpContext();
    System.out.println("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
    if (this.mimicWebDriverCookieState) {
      localContext.setAttribute(
          ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
    }
    HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod();
    requestMethod.setURI(this.linkToCheck);
    HttpParams httpRequestParameters = requestMethod.getParams();
    httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
    requestMethod.setParams(httpRequestParameters);

    System.out.println(
        "Sending " + requestMethod.getMethod() + " request for: " + requestMethod.getURI());
    HttpResponse response = client.execute(requestMethod, localContext);
    System.out.println(
        "HTTP "
            + requestMethod.getMethod()
            + " request status: "
            + response.getStatusLine().getStatusCode());

    return response.getStatusLine().getStatusCode();
  }