List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection
public abstract void releaseConnection();
From source file:org.archive.wayback.resourceindex.ziplines.Http11BlockLoader.java
/** * Fetch a range of bytes from a particular URL. Note that the bytes are * read into memory all at once, so care should be taken with the length * argument./*from w ww .jav a 2s .com*/ * * @param url String URL to fetch * @param offset byte start offset of the desired range * @param length number of octets to fetch * @return a new byte[] containing the octets fetched * @throws IOException on HTTP and Socket failures, as well as Timeouts */ public byte[] getBlock(String url, long offset, int length) throws IOException { HttpMethod method = null; try { method = new GetMethod(url); } catch (IllegalArgumentException e) { LOGGER.warning("Bad URL for block fetch:" + url); throw new IOException("Url:" + url + " does not look like an URL?"); } StringBuilder sb = new StringBuilder(16); sb.append(ZiplinedBlock.BYTES_HEADER).append(offset); sb.append(ZiplinedBlock.BYTES_MINUS).append((offset + length) - 1); String rangeHeader = sb.toString(); method.addRequestHeader(ZiplinedBlock.RANGE_HEADER, rangeHeader); //uc.setRequestProperty(RANGE_HEADER, sb.toString()); long start = System.currentTimeMillis(); try { LOGGER.fine("Reading block:" + url + "(" + rangeHeader + ")"); int status = http.executeMethod(method); if ((status == 200) || (status == 206)) { InputStream is = method.getResponseBodyAsStream(); byte[] block = new byte[length]; ByteStreams.readFully(is, block); long elapsed = System.currentTimeMillis() - start; PerformanceLogger.noteElapsed("CDXBlockLoad", elapsed, url); return block; } else { throw new IOException("Bad status for " + url); } } finally { method.releaseConnection(); } }
From source file:org.artifactory.cli.rest.RestClient.java
/** * Executes a configured HTTP//from ww w. j a va 2s .com * * @param uri Target URL * @param method Method to execute * @param expectedStatus Expected return status * @param expectedResultType Expected response media type * @param timeout Request timeout * @param credentials For authentication * @throws Exception */ private static byte[] executeMethod(String uri, HttpMethod method, int expectedStatus, String expectedResultType, int timeout, Credentials credentials, boolean printStream) throws IOException { try { getHttpClient(uri, timeout, credentials).executeMethod(method); checkStatus(uri, expectedStatus, method); Header contentTypeHeader = method.getResponseHeader("content-type"); if (contentTypeHeader != null) { //Check result content type String contentType = contentTypeHeader.getValue(); checkContentType(uri, expectedResultType, contentType); } return analyzeResponse(method, printStream); } catch (SSLException ssle) { throw new RemoteCommandException("\nThe host you are trying to reach does not support SSL."); } catch (ConnectTimeoutException cte) { throw new RemoteCommandException("\n" + cte.getMessage()); } catch (UnknownHostException uhe) { throw new RemoteCommandException("\nThe host of the specified URL: " + uri + " could not be found.\n" + "Please make sure you have specified the correct path. The default should be:\n" + "http://myhost:8081/artifactory/api/system"); } catch (ConnectException ce) { throw new RemoteCommandException("\nCannot not connect to: " + uri + ". " + "Please make sure to specify a valid host (--host <host>:<port>) or URL (--url http://...)."); } catch (NoRouteToHostException nrthe) { throw new RemoteCommandException("\nCannot reach: " + uri + ".\n" + "Please make sure that the address is valid and that the port is open (firewall, router, etc')."); } finally { method.releaseConnection(); } }
From source file:org.attribyte.api.http.impl.commons.Commons3Client.java
@Override public Response send(Request request, RequestOptions options) throws IOException { HttpMethod method = null; switch (request.getMethod()) { case GET:/*from w w w .j a v a 2 s .c o m*/ method = new GetMethod(request.getURI().toString()); method.setFollowRedirects(options.followRedirects); break; case DELETE: method = new DeleteMethod(request.getURI().toString()); break; case HEAD: method = new HeadMethod(request.getURI().toString()); method.setFollowRedirects(options.followRedirects); break; case POST: method = new PostMethod(request.getURI().toString()); if (request.getBody() != null) { ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray()); ((EntityEnclosingMethod) method).setRequestEntity(requestEntity); } else { PostMethod postMethod = (PostMethod) method; Collection<Parameter> parameters = request.getParameters(); for (Parameter parameter : parameters) { String[] values = parameter.getValues(); for (String value : values) { postMethod.addParameter(parameter.getName(), value); } } } break; case PUT: method = new PutMethod(request.getURI().toString()); if (request.getBody() != null) { ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray()); ((EntityEnclosingMethod) method).setRequestEntity(requestEntity); } break; } if (userAgent != null && Strings.isNullOrEmpty(request.getHeaderValue("User-Agent"))) { method.setRequestHeader("User-Agent", userAgent); } Collection<Header> headers = request.getHeaders(); for (Header header : headers) { String[] values = header.getValues(); for (String value : values) { method.setRequestHeader(header.getName(), value); } } int responseCode; InputStream is = null; try { responseCode = httpClient.executeMethod(method); is = method.getResponseBodyAsStream(); if (is != null) { byte[] body = Request.bodyFromInputStream(is, options.maxResponseBytes); ResponseBuilder builder = new ResponseBuilder(); builder.setStatusCode(responseCode); builder.addHeaders(getMap(method.getResponseHeaders())); return builder.setBody(body.length != 0 ? body : null).create(); } else { ResponseBuilder builder = new ResponseBuilder(); builder.setStatusCode(responseCode); builder.addHeaders(getMap(method.getResponseHeaders())); return builder.create(); } } finally { if (is != null) { try { is.close(); } catch (IOException ioe) { //Ignore } } method.releaseConnection(); } }
From source file:org.bibsonomy.util.WebUtils.java
/** * Reads from a URL and writes the content into a string. * /* www . jav a 2s .c o m*/ * @param url * @param cookie * @param postData * @param visitBefore * * @return String which holds the page content. * * @throws IOException */ public static String getContentAsString(final String url, final String cookie, final String postData, final String visitBefore) throws IOException { if (present(visitBefore)) { /* * visit URL to get cookies if needed */ client.executeMethod(new GetMethod(visitBefore)); } final HttpMethod method; if (present(postData)) { /* * do a POST request */ final List<NameValuePair> data = new ArrayList<NameValuePair>(); for (final String s : postData.split(AMP_SIGN)) { final String[] p = s.split(EQUAL_SIGN); if (p.length != 2) { continue; } data.add(new NameValuePair(p[0], p[1])); } method = new PostMethod(url); ((PostMethod) method).setRequestBody(data.toArray(new NameValuePair[data.size()])); } else { /* * do a GET request */ method = new GetMethod(url); method.setFollowRedirects(true); } /* * set cookie */ if (present(cookie)) { method.setRequestHeader(COOKIE_HEADER_NAME, cookie); } /* * do request */ final int status = client.executeMethod(method); if (status != HttpStatus.SC_OK) { throw new IOException(url + " returns: " + status); } /* * FIXME: check content type header to ensure that we only read textual * content (and not a PDF, radio stream or DVD image ...) */ /* * collect response */ final String charset = extractCharset(method.getResponseHeader(CONTENT_TYPE_HEADER_NAME).getValue()); final StringBuilder content = inputStreamToStringBuilder(method.getResponseBodyAsStream(), charset); method.releaseConnection(); final String string = content.toString(); if (string.length() > 0) { return string; } return null; }
From source file:org.biomart.martservice.MartServiceUtils.java
/** * Sends a registry request to the Biomart webservice and constructs a * MartRegistry from the XML returned by the webservice. * //w ww . j av a 2 s.com * @param martServiceLocation * the URL of the Biomart webservice * @return a MartRegistry * @throws MartServiceException * if the Biomart webservice returns an error or is unavailable */ public static MartRegistry getRegistry(String martServiceLocation, String requestId) throws MartServiceException { List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new NameValuePair(TYPE_ATTRIBUTE, REGISTRY_VALUE)); if (requestId != null) { data.add(new NameValuePair(REQUEST_ID_ATTRIBUTE, requestId)); } HttpMethod method = new GetMethod(martServiceLocation); method.setQueryString(data.toArray(new NameValuePair[data.size()])); try { InputStream in = executeMethod(method, martServiceLocation); Document document = new SAXBuilder().build(in); Element root = document.getRootElement(); return MartServiceXMLHandler.elementToRegistry(root, Namespace.NO_NAMESPACE); } catch (IOException e) { String errorMessage = "Error getting registry from " + martServiceLocation; throw new MartServiceException(errorMessage, e); } catch (JDOMException e) { String errorMessage = "Error getting registry from " + martServiceLocation; throw new MartServiceException(errorMessage, e); } finally { method.releaseConnection(); } }
From source file:org.biomart.martservice.MartServiceUtils.java
public static String getVersion(String martServiceLocation, String requestId, MartURLLocation mart) throws MartServiceException { String errorMessage = "Error getting version from " + martServiceLocation; List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new NameValuePair(TYPE_ATTRIBUTE, VERSION_VALUE)); if (mart.getVirtualSchema() != null) { data.add(new NameValuePair(SCHEMA_ATTRIBUTE, mart.getVirtualSchema())); }/* w ww.ja v a 2 s . c om*/ data.add(new NameValuePair(MART_ATTRIBUTE, mart.getName())); if (requestId != null) { data.add(new NameValuePair(REQUEST_ID_ATTRIBUTE, requestId)); } if (requestId != null) { data.add(new NameValuePair(REQUEST_ID_ATTRIBUTE, requestId)); } HttpMethod method = new GetMethod(martServiceLocation); method.setQueryString(data.toArray(new NameValuePair[data.size()])); try { InputStream in = executeMethod(method, martServiceLocation); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); String version = bufferedReader.readLine(); if (version == null) { throw new MartServiceException(errorMessage + ": No version returned"); } version = version.trim(); // fix for biomart's 'let's add a blank line' thing if ("".equals(version)) { version = bufferedReader.readLine(); if (version == null) { throw new MartServiceException(errorMessage + ": No version returned"); } version = version.trim(); } bufferedReader.close(); return version; } catch (IOException e) { throw new MartServiceException(errorMessage, e); } finally { method.releaseConnection(); } }
From source file:org.biomart.martservice.MartServiceUtils.java
/** * Sends a datasets request to the Biomart webservice and constructs an * array of MartDataset from the tab separated rows of data returned by the * webservice.// w w w .j av a2 s . c o m * * @param martServiceLocation * the URL of the Biomart webservice * @param mart * the mart to get datasets from * @return an array of MartDataset * @throws MartServiceException * if the Biomart webservice returns an error or is unavailable */ public static MartDataset[] getDatasets(String martServiceLocation, String requestId, MartURLLocation mart) throws MartServiceException { List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new NameValuePair(TYPE_ATTRIBUTE, DATASETS_VALUE)); if (mart.getVirtualSchema() != null) { data.add(new NameValuePair(SCHEMA_ATTRIBUTE, mart.getVirtualSchema())); } data.add(new NameValuePair(MART_ATTRIBUTE, mart.getName())); if (mart.getMartUser() != null) { data.add(new NameValuePair(MART_USER_ATTRIBUTE, mart.getMartUser())); } if (requestId != null) { data.add(new NameValuePair(REQUEST_ID_ATTRIBUTE, requestId)); } HttpMethod method = new GetMethod(martServiceLocation); method.setQueryString(data.toArray(new NameValuePair[data.size()])); try { InputStream in = executeMethod(method, martServiceLocation); MartDataset[] datasets = tabSeparatedReaderToDatasets(new InputStreamReader(in), mart); in.close(); return datasets; } catch (IOException e) { String errorMessage = "Error getting datasets from " + martServiceLocation; throw new MartServiceException(errorMessage, e); } finally { method.releaseConnection(); } }
From source file:org.biomart.martservice.MartServiceUtils.java
/** * Sends a configuration request to the Biomart webservice and constructs a * DatasetConfig from the XML returned by the webservice. * /*w w w. ja v a 2 s.co m*/ * @param martServiceLocation * the URL of the Biomart webservice * @param dataset * the dataset to get the configuration for * @return a DatasetConfig * @throws MartServiceException * if the Biomart webservice returns an error or is unavailable */ public static DatasetConfig getDatasetConfig(String martServiceLocation, String requestId, MartDataset dataset) throws MartServiceException { List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new NameValuePair(TYPE_ATTRIBUTE, CONFIGURATION_VALUE)); MartURLLocation mart = dataset.getMartURLLocation(); // if the dataset has a location specify the virtual schema to uniquely // identify the dataset if (mart != null && mart.getVirtualSchema() != null) { data.add(new NameValuePair(SCHEMA_ATTRIBUTE, mart.getVirtualSchema())); } data.add(new NameValuePair(DATASET_VALUE, dataset.getName())); // if (dataset.getInterface() != null) { // data.add(new NameValuePair(INTERFACE_ATTRIBUTE, dataset // .getInterface())); // } if (mart != null && mart.getMartUser() != null) { data.add(new NameValuePair(MART_USER_ATTRIBUTE, mart.getMartUser())); } if (requestId != null) { data.add(new NameValuePair(REQUEST_ID_ATTRIBUTE, requestId)); } HttpMethod method = new GetMethod(martServiceLocation); method.setQueryString(data.toArray(new NameValuePair[data.size()])); try { InputStream in = executeMethod(method, martServiceLocation); DatasetConfigXMLUtils datasetConfigXMLUtils = new DatasetConfigXMLUtils(true); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new InputSource(in)); // Document doc = datasetConfigXMLUtils.getDocumentForXMLStream(in); DatasetConfig datasetConfig = datasetConfigXMLUtils.getDatasetConfigForDocument(doc); datasetConfigXMLUtils.loadDatasetConfigWithDocument(datasetConfig, doc); return datasetConfig; } catch (ConfigurationException e) { String errorMessage = "Error parsing configuration from " + martServiceLocation; logger.debug(errorMessage, e); throw new MartServiceException(errorMessage, e); } catch (JDOMException e) { String errorMessage = "Error parsing configuration from " + martServiceLocation; logger.debug(errorMessage, e); throw new MartServiceException(errorMessage, e); } catch (IOException e) { String errorMessage = "Error getting configuration from " + martServiceLocation; logger.debug(errorMessage, e); throw new MartServiceException(errorMessage, e); } finally { method.releaseConnection(); } }
From source file:org.cancergrid.ws.util.HttpContentReader.java
public static String getHttpContent(String httpUrl, String query, Method method) { LOG.debug("getHttpContent(httpUrl): " + httpUrl); LOG.debug("getHttpContent(query): " + query); LOG.debug("getHttpContent(method): " + method); HttpMethod httpMethod = null; if (httpUrl.contains("&")) { httpUrl = httpUrl.replace("&", "&"); }//w ww . jav a 2 s . c om if (query != null && query.length() > 0 && query.startsWith("?") && query.contains("&")) { query = query.replace("&", "&"); } try { //LOG.debug("Querying: " + httpUrl); if (method == Method.GET) { httpMethod = new GetMethod(httpUrl); if (query != null && query.length() > 0) { httpMethod.setQueryString(query); } } else if (method == Method.POST) { httpMethod = new PostMethod(httpUrl); if (query != null && query.length() > 0) { RequestEntity entity = new StringRequestEntity(query, "text/xml", "UTF-8"); ((PostMethod) httpMethod).setRequestEntity(entity); } } httpMethod.setFollowRedirects(true); httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); Protocol.registerProtocol("https", new Protocol("https", new org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory(), 443)); HttpClient client = new HttpClient(); int statusCode = client.executeMethod(httpMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + httpMethod.getStatusLine()); LOG.error("Error querying: " + httpMethod.getURI().toString()); throw new Exception("Method failed: " + httpMethod.getStatusLine()); } byte[] responseBody = httpMethod.getResponseBody(); return new String(responseBody, "UTF-8"); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); } catch (Exception e) { LOG.error(e.getMessage()); } finally { httpMethod.releaseConnection(); } return null; }
From source file:org.codehaus.httpcache4j.client.HTTPClientResponseResolver.java
private HTTPResponse convertResponse(HttpMethod method) { Headers headers = new Headers(); for (Header header : method.getResponseHeaders()) { headers = headers.add(header.getName(), header.getValue()); }/* ww w . j a v a2s. c om*/ InputStream stream = null; HTTPResponse response; try { stream = getInputStream(method); StatusLine line = new StatusLine(HTTPVersion.get(method.getStatusLine().getHttpVersion()), Status.valueOf(method.getStatusCode()), method.getStatusText()); response = getResponseCreator().createResponse(line, headers, stream); } finally { if (stream == null) { method.releaseConnection(); } } return response; }