Java Code Examples for org.apache.http.client.methods.HttpHead#setHeader()

The following examples show how to use org.apache.http.client.methods.HttpHead#setHeader() . 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: HeadIT.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testHeadToGetSwitch() throws Exception {
    HttpHead head = new HttpHead(getHttpURl("/hello/html"));
    // When checking the content length, we must disable the compression:
    head.setHeader(HeaderNames.ACCEPT_ENCODING, "identity");
    HttpResponse<String> response;
    try {
        org.apache.http.HttpResponse resp = ClientFactory.getHttpClient().execute(head);
        response = new HttpResponse<>(resp, String.class);
    } finally {
        head.releaseConnection();
    }

    assertThat(response.code()).isEqualTo(OK);
    assertThat(response.contentType()).isEqualTo(MimeTypes.HTML);
    System.out.println(response.headers());
    assertThat(Integer.valueOf(response.header(CONTENT_LENGTH))).isEqualTo(20);
}
 
Example 2
Source File: Http2CurlTest.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void shouldPrintMultipleCookiesInOneParameter() throws Exception {
  HttpHead headRequest = new HttpHead("http://test.com/items/12345");
  headRequest.setHeader("Cookie", "X=Y; A=B");
  assertThat(getNonWindowsHttp2Curl().generateCurl(headRequest),
      equalTo("curl 'http://test.com/items/12345' -X HEAD -b 'X=Y; A=B' --compressed -k -v"));
}
 
Example 3
Source File: MavenRepositoryDeployer.java    From maven-repository-tools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if POM file for provided gav can be found in target. Just does
 * a HTTP get of the header and verifies http status OK 200.
 * @param targetUrl url of the target repository
 * @param gav group artifact version string
 * @return {@code true} if the pom.xml already exists in the target repository
 */
private boolean checkIfPomInTarget( String targetUrl, String username, String password, Gav gav )
{
    boolean alreadyInTarget = false;
    
    String artifactUrl = targetUrl + gav.getRepositoryURLPath() + gav.getPomFilename();
    logger.debug( "Headers for {}", artifactUrl );

    HttpHead httphead = new HttpHead( artifactUrl );

    if ( !StringUtils.isEmpty( username ) && ! StringUtils.isEmpty( password ) )
    {
      String encoding = java.util.Base64.getEncoder().encodeToString( ( username + ":" + password ).getBytes() );
      httphead.setHeader( "Authorization", "Basic " + encoding );
    }

    try ( CloseableHttpClient httpClient = HttpClientBuilder.create().build() )
    {
      HttpResponse response = httpClient.execute( httphead );
      int statusCode = response.getStatusLine().getStatusCode();
      if ( statusCode == HttpURLConnection.HTTP_OK )
      {
          alreadyInTarget = true;
      }
      else
      {
          logger.debug( "Headers not found HTTP: {}", statusCode );
      }
    } 
    catch ( IOException ioe )
    {
      logger.warn( "Could not check target repository for already existing pom.xml.", ioe );
    }
    return alreadyInTarget;
}
 
Example 4
Source File: SwiftManagerHTTPS.java    From sync-service with Apache License 2.0 4 votes vote down vote up
private String getWorkspacePermissions(User user, Workspace workspace) throws Exception {

        if (!isTokenActive()) {
            login();
        }

        TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] certificate, String authType) {
                return true;
            }
        };

        SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", 5000, sf));
        ClientConnectionManager ccm = new SingleClientConnManager(registry);

        HttpClient httpClient = new DefaultHttpClient(ccm);

        String url = this.storageUrl + "/" + workspace.getSwiftContainer();

        try {

            HttpHead request = new HttpHead(url);
            request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);

            HttpResponse response = httpClient.execute(request);

            SwiftResponse swiftResponse = new SwiftResponse(response);

            if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
                throw new UnauthorizedException("404 User unauthorized");
            }

            if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
                throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
            }

            // We suppose there are the same permissions for read and write
            Header containerWriteHeader = swiftResponse.getResponseHeader(SwiftResponse.X_CONTAINER_WRITE);

            if (containerWriteHeader == null) {
                return "";
            }

            return containerWriteHeader.getValue();

        } finally {
            httpClient.getConnectionManager().shutdown();
        }
    }
 
Example 5
Source File: SwiftManager.java    From sync-service with Apache License 2.0 3 votes vote down vote up
private String getWorkspacePermissions(User user, Workspace workspace) throws Exception {

        if (!isTokenActive()) {
            login();
        }

        HttpClient httpClient = new DefaultHttpClient();

        String url = this.storageUrl + "/" + workspace.getSwiftContainer();

        try {

            HttpHead request = new HttpHead(url);
            request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);

            HttpResponse response = httpClient.execute(request);

            SwiftResponse swiftResponse = new SwiftResponse(response);

            if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
                throw new UnauthorizedException("404 User unauthorized");
            }

            if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
                throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
            }

            // We suppose there are the same permissions for read and write
            Header containerWriteHeader = swiftResponse.getResponseHeader(SwiftResponse.X_CONTAINER_WRITE);

            if (containerWriteHeader == null) {
                return "";
            }

            return containerWriteHeader.getValue();

        } finally {
            httpClient.getConnectionManager().shutdown();
        }
    }