Search in sources :

Example 96 with HttpHead

use of org.apache.http.client.methods.HttpHead in project openolat by klemens.

the class UserMgmtTest method testPortrait_HEAD_sizes.

/**
 * Check the 3 sizes
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testPortrait_HEAD_sizes() throws IOException, URISyntaxException {
    Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-3");
    URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
    Assert.assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(id.getName(), "A6B7C8"));
    // upload portrait
    URI request = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").build();
    HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
    conn.addMultipart(method, "portrait.jpg", portrait);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // check 200
    URI headMasterRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("master").build();
    HttpHead headMasterMethod = conn.createHead(headMasterRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse headMasterResponse = conn.execute(headMasterMethod);
    assertEquals(200, headMasterResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(headMasterResponse.getEntity());
    // check 200
    URI headBigRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("big").build();
    HttpHead headBigMethod = conn.createHead(headBigRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse headBigResponse = conn.execute(headBigMethod);
    assertEquals(200, headBigResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(headBigResponse.getEntity());
    // check 200
    URI headSmallRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").path("small").build();
    HttpHead headSmallMethod = conn.createHead(headSmallRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse headSmallResponse = conn.execute(headSmallMethod);
    assertEquals(200, headSmallResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(headSmallResponse.getEntity());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) File(java.io.File) URI(java.net.URI) URL(java.net.URL) HttpHead(org.apache.http.client.methods.HttpHead) Test(org.junit.Test)

Example 97 with HttpHead

use of org.apache.http.client.methods.HttpHead in project openolat by klemens.

the class RestConnection method createHead.

public HttpHead createHead(URI uri, String accept, boolean cookie) {
    HttpHead head = new HttpHead(uri);
    decorateHttpMessage(head, accept, "en", cookie);
    return head;
}
Also used : HttpHead(org.apache.http.client.methods.HttpHead)

Example 98 with HttpHead

use of org.apache.http.client.methods.HttpHead in project ant-ivy by apache.

the class HttpClientHandler method doHead.

private CloseableHttpResponse doHead(final URL url, final int connectionTimeout, final int readTimeout) throws IOException {
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout).setConnectTimeout(connectionTimeout).setAuthenticationEnabled(hasCredentialsConfigured(url)).setTargetPreferredAuthSchemes(getAuthSchemePreferredOrder()).setProxyPreferredAuthSchemes(getAuthSchemePreferredOrder()).build();
    final HttpHead httpHead = new HttpHead(normalizeToString(url));
    httpHead.setConfig(requestConfig);
    return this.httpClient.execute(httpHead);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpHead(org.apache.http.client.methods.HttpHead)

Example 99 with HttpHead

use of org.apache.http.client.methods.HttpHead in project structr by structr.

the class HttpHelper method head.

public static Map<String, String> head(final String address, final String username, final String password, final String proxyUrl, final String proxyUsername, final String proxyPassword, final String cookie, final Map<String, String> headers) {
    final Map<String, String> responseHeaders = new HashMap<>();
    try {
        final URI url = URI.create(address);
        final HttpHead req = new HttpHead(url);
        configure(req, username, password, proxyUrl, proxyUsername, proxyPassword, cookie, headers, false);
        final CloseableHttpResponse response = client.execute(req);
        responseHeaders.put("status", Integer.toString(response.getStatusLine().getStatusCode()));
        for (final Header header : response.getAllHeaders()) {
            responseHeaders.put(header.getName(), header.getValue());
        }
    } catch (final Throwable t) {
        logger.error("Unable to get headers from address {}, {}", new Object[] { address, t.getMessage() });
    }
    return responseHeaders;
}
Also used : Header(org.apache.http.Header) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead)

Example 100 with HttpHead

use of org.apache.http.client.methods.HttpHead in project collect by openforis.

the class SaikuService method testUrl.

private boolean testUrl(String url) {
    try {
        HttpClientBuilder cb = HttpClientBuilder.create();
        CloseableHttpClient httpClient = cb.build();
        HttpHead req = new HttpHead(url);
        CloseableHttpResponse resp = httpClient.execute(req);
        int statusCode = resp.getStatusLine().getStatusCode();
        return statusCode == 200;
    } catch (Exception e) {
        return false;
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6