public static void checkUrlExistence(String url) { if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) { HttpClient httpClient = getHttpClient(); HeadMethod httphead = new HeadMethod(url); try { if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) { throw new IllegalArgumentException("Invalid URL: " + url); } if (url.endsWith("metalink") && !checkUrlExistenceMetalink(url)) { throw new IllegalArgumentException("Invalid URLs defined on metalink: " + url); } } catch (HttpException hte) { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + hte.getMessage()); } catch (IOException ioe) { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + ioe.getMessage()); } finally { httphead.releaseConnection(); } } }
@Override public String getCharset() throws DereferenceException { if (charsetFetched) return charsetName; charsetFetched = true; if (!httpUrl.getProtocol().equals("http") && !httpUrl.getProtocol().equals("https")) { charsetName = null; return null; } HeadMethod method = new HeadMethod(httpUrl.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); charsetName = method.getResponseCharSet(); return charsetName; } catch (HttpException e) { // throw new DereferenceException(e); } catch (IOException e) { // throw new DereferenceException(e); } finally { method.releaseConnection(); } charsetName = null; return null; }
/** * {@inheritDoc} */ @Override public String getCharset() throws DereferenceException { if (charsetFetched) { return charsetName; } charsetFetched = true; if (!httpUrl.getProtocol().equals("http") && !httpUrl.getProtocol().equals("https")) { charsetName = null; return null; } HeadMethod method = new HeadMethod(httpUrl.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); charsetName = method.getResponseCharSet(); return charsetName; } catch (HttpException e) { // throw new DereferenceException(e); } catch (IOException e) { // throw new DereferenceException(e); } finally { method.releaseConnection(); } charsetName = null; return null; }
public String getCharset() throws DereferenceException { if (!url.getProtocol().equals("http")) { return null; // Don't know } HeadMethod method = new HeadMethod(url.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); return method.getResponseCharSet(); } catch (HttpException e) { throw new DereferenceException(e); } catch (IOException e) { throw new DereferenceException(e); } finally { method.releaseConnection(); } } }
/** * {@inheritDoc} */ @Override public String getCharset() throws DereferenceException { if (!httpUrl.getProtocol().equals("http")) { return null; // Don't know } HeadMethod method = new HeadMethod(httpUrl.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); return method.getResponseCharSet(); } catch (HttpException e) { throw new DereferenceException(e); } catch (IOException e) { throw new DereferenceException(e); } finally { method.releaseConnection(); } }
/** * {@inheritDoc} */ @Override public String getCharset() throws DereferenceException { if (!httpUrl.getProtocol().equals("http")) { return null; // Don't know } HeadMethod method = new HeadMethod(httpUrl.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); return method.getResponseCharSet(); } catch (HttpException e) { throw new DereferenceException(e); } catch (IOException e) { throw new DereferenceException(e); } finally { method.releaseConnection(); } }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }
HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; } method = new HeadMethod(); setupMethod(method); final HttpClient client = getAbstractFileSystem().getClient(); client.executeMethod(method); method.releaseConnection(); return method; }
/** Determine the value for the Content-Length header using a HEAD call * * @param url * @return * @throws HttpException */ public static int getContentLength(String url) throws HttpException, IOException { int result = 0; //Execute a GET method on the URL HeadMethod head = new HeadMethod(url); head.setFollowRedirects(true); head.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)"); try { _httpClient.executeMethod(head); // Retrieve just the Content-Length header value. String length = head.getResponseHeader("Content-Length").getValue(); result = Integer.parseInt(length); } catch (NumberFormatException nfe) { //header not found } finally { head.releaseConnection(); } return result; }
/** {@inheritDoc} */ public boolean exists() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { return false; } return true; } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } finally { headMethod.releaseConnection(); } }
/** {@inheritDoc} */ public DateTime getLastModifiedTime() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { throw new ResourceException("Unable to retrieve resource URL " + resourceUrl + ", received HTTP status code " + headMethod.getStatusCode()); } Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified"); if (lastModifiedHeader != null && !DatatypeHelper.isEmpty(lastModifiedHeader.getValue())) { long lastModifiedTime = DateUtil.parseDate(lastModifiedHeader.getValue()).getTime(); return new DateTime(lastModifiedTime, ISOChronology.getInstanceUTC()); } return new DateTime(); } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } catch (DateParseException e) { throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e); } finally { headMethod.releaseConnection(); } }
/** * Send a HEAD request * @param cluster the cluster definition * @param path the path or URI * @param headers the HTTP headers to include in the request * @return a Response object with response detail * @throws IOException */ public Response head(Cluster cluster, String path, Header[] headers) throws IOException { HeadMethod method = new HeadMethod(); try { int code = execute(cluster, method, null, path); headers = method.getResponseHeaders(); return new Response(code, headers, null); } finally { method.releaseConnection(); } }
/** * Determines the type of this file. Must not return null. The return * value of this method is cached, so the implementation can be expensive. */ protected FileType doGetType() throws Exception { // Use the HEAD method to probe the file. method = new HeadMethod(); setupMethod(method); final HttpClient client = fileSystem.getClient(); final int status = client.executeMethod(method); method.releaseConnection(); if (status == HttpURLConnection.HTTP_OK) { return FileType.FILE; } else if (status == HttpURLConnection.HTTP_NOT_FOUND || status == HttpURLConnection.HTTP_GONE) { return FileType.IMAGINARY; } else { throw new FileSystemException("vfs.provider.http/head.error", getName()); } }
/** * Check if a file exists in the OC server * * @deprecated Use ExistenceCheckOperation instead * * @return 'true' if the file exists; 'false' it doesn't exist * @throws Exception When the existence could not be determined */ @Deprecated public boolean existsFile(String path) throws IOException { HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path)); try { int status = executeMethod(head); Log_OC.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":"")); exhaustResponse(head.getResponseBodyAsStream()); return (status == HttpStatus.SC_OK); } finally { head.releaseConnection(); // let the connection available for other methods } }
headMethod.releaseConnection();
head.releaseConnection(); client.setFollowRedirects(previousFollowRedirects);