Java Code Examples for org.apache.http.client.methods.HttpRequestBase#setURI()

The following examples show how to use org.apache.http.client.methods.HttpRequestBase#setURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileDownloader.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 6 votes vote down vote up
private HttpResponse makeHTTPConnection() throws IOException, NullPointerException {
    if (fileURI == null) throw new NullPointerException("No file URI specified");

    HttpClient client = HttpClientBuilder.create().build();

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);

    BasicHttpContext localContext = new BasicHttpContext();

    if (null != urlParameters && (
            httpRequestMethod.equals(RequestType.PATCH) ||
                    httpRequestMethod.equals(RequestType.POST) ||
                    httpRequestMethod.equals(RequestType.PUT)
    )) {
        ((HttpEntityEnclosingRequestBase) requestMethod)
                .setEntity(new UrlEncodedFormEntity(urlParameters));
    }

    return client.execute(requestMethod, localContext);
}
 
Example 2
Source File: FileDownloader.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 6 votes vote down vote up
private HttpResponse makeHTTPConnection() throws IOException, NullPointerException {
    if (fileURI == null) throw new NullPointerException("No file URI specified");

    HttpClient client = HttpClientBuilder.create().build();

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);

    BasicHttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(HttpClientContext.COOKIE_STORE, getWebDriverCookies(driver.manage().getCookies()));
    requestMethod.setHeader("User-Agent", getWebDriverUserAgent());

    if (null != urlParameters && (
            httpRequestMethod.equals(RequestType.PATCH) ||
                    httpRequestMethod.equals(RequestType.POST) ||
                    httpRequestMethod.equals(RequestType.PUT))
            ) {
        ((HttpEntityEnclosingRequestBase) requestMethod).setEntity(new UrlEncodedFormEntity(urlParameters));
    }

    return client.execute(requestMethod, localContext);
}
 
Example 3
Source File: FileDownloader.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 6 votes vote down vote up
private HttpResponse makeHTTPConnection() throws IOException, NullPointerException {
    if (fileURI == null) throw new NullPointerException("No file URI specified");

    HttpClient client = HttpClientBuilder.create().build();

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);

    BasicHttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(HttpClientContext.COOKIE_STORE, getWebDriverCookies(driver.manage().getCookies()));
    requestMethod.setHeader("User-Agent", getWebDriverUserAgent());

    if (null != urlParameters && (
            httpRequestMethod.equals(RequestType.PATCH) ||
                    httpRequestMethod.equals(RequestType.POST) ||
                    httpRequestMethod.equals(RequestType.PUT))
            ) {
        ((HttpEntityEnclosingRequestBase) requestMethod).setEntity(new UrlEncodedFormEntity(urlParameters));
    }

    return client.execute(requestMethod, localContext);
}
 
Example 4
Source File: ApacheHttpRequest.java    From junit-servers with MIT License 6 votes vote down vote up
@Override
protected HttpResponse doExecute() throws Exception {
	final HttpMethod method = getMethod();

	final HttpRequestBase httpRequest = FACTORY.create(method);

	httpRequest.setURI(createRequestURI());
	handleBody(httpRequest);
	handleHeaders(httpRequest);
	handleCookies(httpRequest);

	final long start = nanoTime();
	final org.apache.http.HttpResponse httpResponse = client.execute(httpRequest);
	final long duration = nanoTime() - start;

	return ApacheHttpResponseFactory.of(httpResponse, duration);
}
 
Example 5
Source File: RequestHelper.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Append query parameters
 *
 * @param verb
 * @param params
 * @throws Throwable
 */
public static void appendParameters(HttpRequestBase verb, Map<String, String> params) throws Throwable {
  if (null != verb
          && null != params && !params.isEmpty()) {
    String address = verb.getURI().toString();
    boolean isAmpersand = address.contains("?");
    StringBuilder url = new StringBuilder(address);

    for (Map.Entry<String, String> param : params.entrySet()) {
      if (!isAmpersand) {
        url.append("?");
        isAmpersand = true;
      } else {
        url.append("&");
      }
      url.append(URLEncoder.encode(param.getKey(), "UTF-8"));
      url.append("=");
      url.append(URLEncoder.encode(param.getValue(), "UTF-8"));
    }
    URI uri = new URI(url.toString());
    verb.setURI(uri);
  }
}
 
Example 6
Source File: RequestForwardingClient.java    From selenium-grid-extensions with Apache License 2.0 6 votes vote down vote up
private HttpRequestBase createHttpRequest(HttpServletRequest request) throws IOException {
    String method = request.getMethod();
    LOGGER.info("Creating " + method + " request to forward");
    HttpRequestBase httpRequestBase =   HttpPost.METHOD_NAME.equals(method)     ?    createPostRequest(request) :
                                        HttpGet.METHOD_NAME.equals(method)      ?    new HttpGet() :
                                        HttpPut.METHOD_NAME.equals(method)      ?    new HttpPut() :
                                        HttpDelete.METHOD_NAME.equals(method)   ?    new HttpDelete() : null;

    if (httpRequestBase == null) {
        throw new UnsupportedHttpMethodException(method);
    }
    URI uri = URI.create(endpoint + SeleniumSessions.trimSessionPath(request.getPathInfo()));
    LOGGER.info("Trimming session id from path, new path: " + uri.toString());
    httpRequestBase.setURI(uri);

    return httpRequestBase;
}
 
Example 7
Source File: AbstractRefTest.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
protected HttpResponse callUri(
    final ODataHttpMethod httpMethod, final String uri,
    final String additionalHeader, final String additionalHeaderValue,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {

  HttpRequestBase request =
      httpMethod == ODataHttpMethod.GET ? new HttpGet() :
          httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() :
              httpMethod == ODataHttpMethod.POST ? new HttpPost() :
                  httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch();
  request.setURI(URI.create(getEndpoint() + uri));
  if (additionalHeader != null) {
    request.addHeader(additionalHeader, additionalHeaderValue);
  }
  if (requestBody != null) {
    ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody));
    request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType);
  }

  final HttpResponse response = getHttpClient().execute(request);

  assertNotNull(response);
  assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());

  if (expectedStatusCode == HttpStatusCodes.OK) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
  } else if (expectedStatusCode == HttpStatusCodes.CREATED) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
    assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
  } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
    assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
  }

  return response;
}
 
Example 8
Source File: RequestHelper.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prepend the given base to the given HTTP element.
 * @param verb
 * @param base
 */
public static void addBaseUrl(HttpRequestBase verb, String base) throws Throwable {
  if (null != verb) {
    String address = verb.getURI().toString();

    address = appendPath(base, address);
    URI uri = new URI(address);
    verb.setURI(uri);
  }
}
 
Example 9
Source File: RequestHelper.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Append the given path to the existing base
 *
 * @param uri
 * @param path
 * @return
 */
public static void appendPath(HttpRequestBase verb, List<String> path) throws Throwable {

  if (null != verb) {
    String address = verb.getURI().toString();

    address = RequestHelper.appendPath(address, path);
    URI uri = new URI(address);
    verb.setURI(uri);
  }
}
 
Example 10
Source File: UrlRewriteTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private HttpRequestBase createRedirectRequest(final Class<? extends HttpRequestBase> clazz) throws Exception {
  String endpoint = getEndpoint().toASCIIString();
  endpoint = endpoint.substring(0, endpoint.length() - 1);

  final HttpRequestBase httpMethod = clazz.newInstance();
  httpMethod.setURI(URI.create(endpoint));

  final HttpParams params = new BasicHttpParams();
  params.setParameter("http.protocol.handle-redirects", false);
  httpMethod.setParams(params);
  return httpMethod;
}
 
Example 11
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected HttpResponse callUri(
    final ODataHttpMethod httpMethod, final String uri,
    final String additionalHeader, final String additionalHeaderValue,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {

  HttpRequestBase request =
      httpMethod == ODataHttpMethod.GET ? new HttpGet() :
          httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() :
              httpMethod == ODataHttpMethod.POST ? new HttpPost() :
                  httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch();
  request.setURI(URI.create(getEndpoint() + uri));
  if (additionalHeader != null) {
    request.addHeader(additionalHeader, additionalHeaderValue);
  }
  if (requestBody != null) {
    ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody));
    request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType);
  }

  final HttpResponse response = getHttpClient().execute(request);

  assertNotNull(response);
  assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());

  if (expectedStatusCode == HttpStatusCodes.OK) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
  } else if (expectedStatusCode == HttpStatusCodes.CREATED) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
    assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
  } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
    assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
  }

  return response;
}
 
Example 12
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected HttpResponse callUri(
    final ODataHttpMethod httpMethod, final String uri,
    final String additionalHeader, final String additionalHeaderValue,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {

  HttpRequestBase request =
      httpMethod == ODataHttpMethod.GET ? new HttpGet() :
          httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() :
              httpMethod == ODataHttpMethod.POST ? new HttpPost() :
                  httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch();
  request.setURI(URI.create(getEndpoint() + uri));
  if (additionalHeader != null) {
    request.addHeader(additionalHeader, additionalHeaderValue);
  }
  if (requestBody != null) {
    ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody));
    request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType);
  }

  final HttpResponse response = getHttpClient().execute(request);

  assertNotNull(response);
  assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());

  if (expectedStatusCode == HttpStatusCodes.OK) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
  } else if (expectedStatusCode == HttpStatusCodes.CREATED) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
    assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
  } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
    assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
  }

  return response;
}
 
Example 13
Source File: RestTask.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
private HttpUriRequest requestWithQueryString(HttpRequestBase request, Uri.Builder uriBuilder) throws URISyntaxException {
    if (params != null) {
        for (BasicNameValuePair param : params) {
            uriBuilder.appendQueryParameter(param.getName(), param.getValue());
        }
    }
    request.setURI(new URI(uriBuilder.build().toString()));
    return request;
}
 
Example 14
Source File: DAVClient.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected HttpResponse execute(final HttpRequestBase request) throws IOException {
    if(StringUtils.isNotBlank(request.getURI().getRawQuery())) {
        request.setURI(URI.create(String.format("%s%s?%s", uri, request.getURI().getRawPath(), request.getURI().getRawQuery())));
    }
    else {
        request.setURI(URI.create(String.format("%s%s", uri, request.getURI().getRawPath())));
    }
    return super.execute(request);
}
 
Example 15
Source File: UrlRewriteTest.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private HttpRequestBase createRedirectRequest(final Class<? extends HttpRequestBase> clazz) throws Exception {
  String endpoint = getEndpoint().toASCIIString();
  endpoint = endpoint.substring(0, endpoint.length() - 1);

  final HttpRequestBase httpMethod = clazz.newInstance();
  httpMethod.setURI(URI.create(endpoint));

  final HttpParams params = new BasicHttpParams();
  params.setParameter("http.protocol.handle-redirects", false);
  httpMethod.setParams(params);
  return httpMethod;
}
 
Example 16
Source File: HttpClientAdapter.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * fix relative uri and update current uri.
 *
 * @param request http request
 */
private void handleURI(HttpRequestBase request) {
    URI requestURI = request.getURI();
    if (!requestURI.isAbsolute()) {
        request.setURI(URIUtils.resolve(uri, requestURI));
    }
    uri = request.getURI();
}
 
Example 17
Source File: GoAgentServerHttpClient.java    From gocd with Apache License 2.0 4 votes vote down vote up
public CloseableHttpResponse execute(HttpRequestBase request) throws IOException {
    request.setURI(request.getURI().normalize());
    return client.execute(request);
}
 
Example 18
Source File: GoAgentServerHttpClient.java    From gocd with Apache License 2.0 4 votes vote down vote up
public CloseableHttpResponse execute(HttpRequestBase request, HttpContext context) throws IOException {
    request.setURI(request.getURI().normalize());
    return client.execute(request, context);
}
 
Example 19
Source File: HttpConnector.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Adds a query string to a given method URI.
 * 
 * @param query
 *            - a query string to be added
 * @param method
 *            - a method to be updated
 */
private void addQuery(String query, HttpRequestBase method) {
	if (!StringUtils.isEmpty(method.getURI().getQuery())) {
		method.setURI(changeQueryString(method.getURI(), method.getURI().getRawQuery() + "&" + query));
	} else {
		method.setURI(changeQueryString(method.getURI(), query));
	}
}
 
Example 20
Source File: HttpClientService.java    From waterdrop with Apache License 2.0 3 votes vote down vote up
public static void execAsyncGet(String baseUrl, List<BasicNameValuePair> urlParams, FutureCallback callback)
		throws Exception {

	if (baseUrl == null) {
		logger.warn("we don't have base url, check config");
		throw new ConfigException("missing base url");
	}

	HttpRequestBase httpMethod = new HttpGet(baseUrl);
	
	CloseableHttpAsyncClient hc = null;

	try {
		hc = HttpClientFactory.getInstance().getHttpAsyncClientPool()
				.getAsyncHttpClient();

		hc.start();

		HttpClientContext localContext = HttpClientContext.create();
		BasicCookieStore cookieStore = new BasicCookieStore();

		if (null != urlParams) {

			String getUrl = EntityUtils.toString(new UrlEncodedFormEntity(urlParams));

			httpMethod.setURI(new URI(httpMethod.getURI().toString() + "?" + getUrl));
		}


		localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

		hc.execute(httpMethod, localContext, callback);

	} catch (Exception e) {
		e.printStackTrace();
	}

}