Search in sources :

Example 91 with HttpHead

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

the class SwiftAPIDirect method getTempUrlObjectLength.

/*
   * Sends a HEAD request to get an object's length
   */
public static long getTempUrlObjectLength(Path path, SwiftConnectionManager scm) throws IOException {
    final HttpHead head = new HttpHead(path.toString().replace("swift2d", "https"));
    final CloseableHttpResponse response = scm.createHttpConnection().execute(head);
    return Long.parseLong(response.getFirstHeader("Content-Length").getValue());
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 92 with HttpHead

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

the class HttpClientUnshortenLiveTest method expandSingleLevel.

private String expandSingleLevel(final String url) throws IOException {
    HttpHead request = null;
    try {
        request = new HttpHead(url);
        final HttpResponse httpResponse = client.execute(request);
        final int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != 301 && statusCode != 302) {
            return url;
        }
        final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
        Preconditions.checkState(headers.length == 1);
        return headers[0].getValue();
    } catch (final IllegalArgumentException uriEx) {
        return url;
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 93 with HttpHead

use of org.apache.http.client.methods.HttpHead in project triplea by triplea-game.

the class DownloadUtils method newHttpHeadRequest.

private static HttpRequestBase newHttpHeadRequest(final String uri) {
    final HttpHead request = new HttpHead(uri);
    HttpProxy.addProxy(request);
    return request;
}
Also used : HttpHead(org.apache.http.client.methods.HttpHead)

Example 94 with HttpHead

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

the class HttpClientFileSystemBrowser method runRequest.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser#runRequest()
	 */
protected void runRequest() throws Exception {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "runRequest");
    setupProxies();
    // set timeout
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
    String urlString = directoryOrFile.toString();
    // setup authentication
    setupAuthentication(urlString);
    headMethod = new HttpHead(urlString);
    // $NON-NLS-1$
    int maxAge = Integer.getInteger("org.eclipse.ecf.http.cache.max-age", 0).intValue();
    // fix the fix for bug 249990 with bug 410813
    if (maxAge == 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        headMethod.addHeader("Cache-Control", "max-age=0");
    } else if (maxAge > 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        headMethod.addHeader("Cache-Control", "max-age=" + maxAge);
    }
    long lastModified = 0;
    long fileLength = -1;
    connectingSockets.clear();
    int code = -1;
    try {
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "browse=" + urlString);
        httpContext = new BasicHttpContext();
        httpResponse = httpClient.execute(headMethod, httpContext);
        code = httpResponse.getStatusLine().getStatusCode();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "browse resp=" + code);
        // Check for NTLM proxy in response headers
        // This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002
        boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext);
        if (ntlmProxyFound && !hasForceNTLMProxyOption())
            // $NON-NLS-1$
            throw new BrowseFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE);
        if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
            // $NON-NLS-1$
            throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication.");
        if (code == HttpURLConnection.HTTP_OK) {
            Header contentLength = httpResponse.getLastHeader(CONTENT_LENGTH_HEADER);
            if (contentLength != null) {
                fileLength = Integer.parseInt(contentLength.getValue());
            }
            lastModified = getLastModifiedTimeFromHeader();
        } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
            // $NON-NLS-1$
            throw new BrowseFileTransferException(NLS.bind("File not found: {0}", urlString), code);
        } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
        } else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
            // $NON-NLS-1$
            throw new BrowseFileTransferException("Forbidden", code);
        } else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
            throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
        } else {
            throw new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code);
        }
        remoteFiles = new IRemoteFile[1];
        remoteFiles[0] = new URLRemoteFile(lastModified, fileLength, fileID);
    } catch (Exception e) {
        // $NON-NLS-1$
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "runRequest", e);
        BrowseFileTransferException ex = (BrowseFileTransferException) ((e instanceof BrowseFileTransferException) ? e : new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code));
        throw ex;
    }
}
Also used : BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) Header(org.apache.http.Header) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) URLRemoteFile(org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile) HttpHead(org.apache.http.client.methods.HttpHead) BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) IOException(java.io.IOException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException)

Example 95 with HttpHead

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

the class UserMgmtTest method testPortrait_HEAD.

@Test
public void testPortrait_HEAD() throws IOException, URISyntaxException {
    Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-1");
    Identity idWithoutPortrait = JunitTestHelper.createAndPersistIdentityAsRndUser("portrait-2");
    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 headRequest = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("portrait").build();
    HttpHead headMethod = conn.createHead(headRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse headResponse = conn.execute(headMethod);
    assertEquals(200, headResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(headResponse.getEntity());
    // check 404
    URI headNoRequest = UriBuilder.fromUri(getContextURI()).path("users").path(idWithoutPortrait.getKey().toString()).path("portrait").build();
    HttpHead headNoMethod = conn.createHead(headNoRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    HttpResponse headNoResponse = conn.execute(headNoMethod);
    assertEquals(404, headNoResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(headNoResponse.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)

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