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());
}
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();
}
}
}
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;
}
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;
}
}
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());
}
Aggregations