Java Code Examples for org.apache.http.client.methods.HttpGet#setHeaders()

The following examples show how to use org.apache.http.client.methods.HttpGet#setHeaders() . These examples are extracted from open source projects. 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
/**
 * Preform a get request
 *
 * @param baseUrl               (nexusUrl in most tests)
 * @param path                  to the resource
 * @param headers               {@link Header}s
 * @param useDefaultCredentials use {@link NexusITSupport#clientBuilder(URL, boolean)} for using credentials
 * @return the response object
 */
protected Response get(final URL baseUrl,
                       final String path,
                       final Header[] headers,
                       final boolean useDefaultCredentials) throws Exception
{
  HttpGet request = new HttpGet();
  request.setURI(UriBuilder.fromUri(baseUrl.toURI()).path(path).build());
  request.setHeaders(headers);

  try (CloseableHttpClient client = clientBuilder(nexusUrl, useDefaultCredentials).build()) {

    try (CloseableHttpResponse response = client.execute(request)) {
      ResponseBuilder responseBuilder = Response.status(response.getStatusLine().getStatusCode());
      Arrays.stream(response.getAllHeaders()).forEach(h -> responseBuilder.header(h.getName(), h.getValue()));

      HttpEntity entity = response.getEntity();
      if (entity != null) {
        responseBuilder.entity(new ByteArrayInputStream(IOUtils.toByteArray(entity.getContent())));
      }
      return responseBuilder.build();
    }
  }
}
 
Example 2
/**
 * Preform a get request
 *
 * @param baseUrl               (nexusUrl in most tests)
 * @param path                  to the resource
 * @param headers               {@link Header}s
 * @param useDefaultCredentials use {@link NexusITSupport#clientBuilder(URL, boolean)} for using credentials
 * @return the response object
 */
protected Response get(final URL baseUrl,
                       final String path,
                       final Header[] headers,
                       final boolean useDefaultCredentials) throws Exception
{
  HttpGet request = new HttpGet();
  request.setURI(UriBuilder.fromUri(baseUrl.toURI()).path(path).build());
  request.setHeaders(headers);

  try (CloseableHttpClient client = clientBuilder(nexusUrl, useDefaultCredentials).build()) {

    try (CloseableHttpResponse response = client.execute(request)) {
      ResponseBuilder responseBuilder = Response.status(response.getStatusLine().getStatusCode());
      Arrays.stream(response.getAllHeaders()).forEach(h -> responseBuilder.header(h.getName(), h.getValue()));

      HttpEntity entity = response.getEntity();
      if (entity != null) {
        responseBuilder.entity(new ByteArrayInputStream(IOUtils.toByteArray(entity.getContent())));
      }
      return responseBuilder.build();
    }
  }
}
 
Example 3
List<URI> getAllRedirectLocations(String uri, Header[] headers) throws IOException {
    CloseableHttpResponse response = null;
    try {
        HttpClientContext context = HttpClientContext.create();
        HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/%s", address.getHostName(), address.getPort(), uri));
        if (headers != null) {
            httpGet.setHeaders(headers);
        }
        response = httpClient.execute(httpGet, context);

        // get all redirection locations
        return context.getRedirectLocations();
    } finally {
        if(response != null) {
            response.close();
        }
    }
}
 
Example 4
Source Project: newblog   File: LibraryUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * http get
 *
 * @param url
 * @return response
 * @throws ClientProtocolException
 * @throws IOException
 */
public static CloseableHttpResponse get(String url, Header[] header) throws IOException {
    HttpGet httpget = new HttpGet(url);
    if (header != null && header.length > 0) {
        httpget.setHeaders(header);
    }
    CloseableHttpResponse response = httpClient.execute(httpget, context);
    return response;
}
 
Example 5
public static String doHttpGet(String url, Header[] headers) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeaders(headers);
        HttpResponse resp = httpClient.execute(httpGet);

        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
public RequestHandle get(Context context, String s, Header aheader[], RequestParams requestparams, ResponseHandlerInterface responsehandlerinterface)
{
    HttpGet httpget = new HttpGet(getUrlWithQueryString(h, s, requestparams));
    if (aheader != null)
    {
        httpget.setHeaders(aheader);
    }
    return sendRequest(c, d, httpget, null, responsehandlerinterface, context);
}
 
Example 7
CloseableHttpResponse get(String uri, Header[] headers) throws IOException {
    HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/%s", address.getHostName(), address.getPort(), uri));
    if (headers != null) {
        httpGet.setHeaders(headers);
    }
    return executeAndDefaultAssertions(httpGet);
}
 
Example 8
Source Project: crate   File: BlobHttpIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
protected CloseableHttpResponse get(String uri, Header[] headers) throws IOException {
    HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", dataNode1.getHostName(), dataNode1.getPort(), uri));
    if (headers != null) {
        httpGet.setHeaders(headers);
    }
    return executeAndDefaultAssertions(httpGet);
}
 
Example 9
Source Project: snowflake-jdbc   File: SessionUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Given access token, query IDP URL snowflake app to get SAML response
 * We also need to perform important client side validation:
 * validate the post back url come back with the SAML response
 * contains the same prefix as the Snowflake's server url, which is the
 * intended destination url to Snowflake.
 * Explanation:
 * This emulates the behavior of IDP initiated login flow in the user
 * browser where the IDP instructs the browser to POST the SAML
 * assertion to the specific SP endpoint.  This is critical in
 * preventing a SAML assertion issued to one SP from being sent to
 * another SP.
 *
 * @param loginInput   Login Info for the request
 * @param ssoUrl       URL to use for SSO
 * @param oneTimeToken The token used for SSO
 * @return The response in HTML form
 * @throws SnowflakeSQLException Will be thrown if the destination URL in
 *                               the SAML assertion does not match
 */
private static String federatedFlowStep4(
    SFLoginInput loginInput,
    String ssoUrl,
    String oneTimeToken) throws SnowflakeSQLException
{
  String responseHtml = "";
  try
  {

    final URL url = new URL(ssoUrl);
    URI oktaGetUri = new URIBuilder()
        .setScheme(url.getProtocol())
        .setHost(url.getHost())
        .setPath(url.getPath())
        .setParameter("RelayState", "%2Fsome%2Fdeep%2Flink")
        .setParameter("onetimetoken", oneTimeToken).build();
    HttpGet httpGet = new HttpGet(oktaGetUri);

    HeaderGroup headers = new HeaderGroup();
    headers.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "*/*"));
    httpGet.setHeaders(headers.getAllHeaders());

    responseHtml = HttpUtil.executeGeneralRequest(
        httpGet,
        loginInput.getLoginTimeout(),
        loginInput.getOCSPMode());

    // step 5
    String postBackUrl = getPostBackUrlFromHTML(responseHtml);
    if (!isPrefixEqual(postBackUrl, loginInput.getServerUrl()))
    {
      logger.debug("The specified authenticator {} and the destination URL " +
                   "in the SAML assertion {} do not match.",
                   loginInput.getAuthenticator(), postBackUrl);
      throw new SnowflakeSQLException(
          SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,
          ErrorCode.IDP_INCORRECT_DESTINATION.getMessageCode());
    }
  }
  catch (IOException | URISyntaxException ex)
  {
    handleFederatedFlowError(loginInput, ex);
  }
  return responseHtml;
}
 
Example 10
private String doGet(URI uri) throws DigitalOceanException, RequestUnsuccessfulException {
  HttpGet get = new HttpGet(uri);
  get.setHeaders(requestHeaders);
  return executeHttpRequest(get);
}