List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:org.araqne.pkg.HttpWagon.java
public static InputStream openDownloadStream(URL url, TrustManagerFactory tmf, KeyManagerFactory kmf) throws KeyManagementException, IOException { SSLContext ctx = null;//from w w w . j a va 2 s . c om try { ctx = SSLContext.getInstance("SSL"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (tmf != null) trustManagers = tmf.getTrustManagers(); if (kmf != null) keyManagers = kmf.getKeyManagers(); ctx.init(keyManagers, trustManagers, new SecureRandom()); HttpsSocketFactory h = new HttpsSocketFactory(kmf, tmf); Protocol https = new Protocol("https", (ProtocolSocketFactory) h, 443); Protocol.registerProtocol("https", https); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url.toString()); client.executeMethod(method); return method.getResponseBodyAsStream(); }
From source file:org.araqne.pkg.HttpWagon.java
public static InputStream openDownloadStream(URL url, boolean useAuth, String username, String password) throws IOException { Logger logger = LoggerFactory.getLogger(HttpWagon.class.getName()); logger.trace("http wagon: downloading {}", url); int socketTimeout = getSocketTimeout(); int connectionTimeout = getConnectTimeout(); HttpClient client = new HttpClient(); client.getParams().setParameter("http.socket.timeout", socketTimeout); client.getParams().setParameter("http.connection.timeout", connectionTimeout); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, null); String realm = ""; if (useAuth) { client.getParams().setAuthenticationPreemptive(true); if (realmCache.containsKey(getRealmCacheKey(url))) realm = realmCache.get(getRealmCacheKey(url)); setClientAuth(url, username, password, realm, client); }/*from w w w . ja v a 2s.c om*/ HttpMethod method = new GetMethod(url.toString()); int statusCode = client.executeMethod(method); if (useAuth && statusCode == HttpStatus.SC_UNAUTHORIZED) { realm = getRealm(method); setClientAuth(url, username, password, realm, client); method = new GetMethod(url.toString()); statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new IOException("digest auth failed: " + method.getStatusLine()); } else { realmCache.put(getRealmCacheKey(url), realm); } } if (statusCode != HttpStatus.SC_OK) { throw new IOException("method failed: " + method.getStatusLine()); } return method.getResponseBodyAsStream(); }
From source file:org.archive.wayback.liveweb.ARCUnwrappingProxy.java
public boolean handleRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException { StringBuffer sb = httpRequest.getRequestURL(); String query = httpRequest.getQueryString(); if (query != null) { sb.append("?").append(query); }/*from w w w . j a v a 2 s.co m*/ HttpMethod method = new GetMethod(sb.toString()); boolean got200 = false; try { HttpClient http = new HttpClient(connectionManager); http.setHostConfiguration(hostConfiguration); int status = http.executeMethod(method); if (status == 200) { ARCRecord r = new ARCRecord(new GZIPInputStream(method.getResponseBodyAsStream()), "id", 0L, false, false, true); Resource res = null; try { res = ResourceFactory.ARCArchiveRecordToResource(r, null); } catch (ResourceNotAvailableException e) { LOGGER.severe(e.getMessage()); throw new IOException(e); } httpResponse.setStatus(res.getStatusCode()); Map<String, String> headers = res.getHttpHeaders(); Iterator<String> keys = headers.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if (!key.equalsIgnoreCase("Connection") && !key.equalsIgnoreCase("Content-Length") && !key.equalsIgnoreCase("Transfer-Encoding")) { String value = headers.get(key); httpResponse.addHeader(key, value); } } ByteOp.copyStream(res, httpResponse.getOutputStream()); got200 = true; } } finally { method.releaseConnection(); } return got200; }
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 ww w . j ava2 s .c o m * * @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
/** * Writes the response stream to the selected outputs * * @param method The method that was executed * @param printStream True if should print response stream to system.out * @return byte[] Response//w w w . j a va2 s.co m * @throws IOException */ private static byte[] analyzeResponse(HttpMethod method, boolean printStream) throws IOException { InputStream is = method.getResponseBodyAsStream(); if (is == null) { return null; } byte[] buffer = new byte[1024]; int r; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream os = baos; if (printStream) { os = new TeeOutputStream(baos, System.out); } while ((r = is.read(buffer)) != -1) { os.write(buffer, 0, r); } if (printStream) { System.out.println(""); } return baos.toByteArray(); } catch (SocketTimeoutException e) { CliLog.error("Communication with the server has timed out: " + e.getMessage()); CliLog.error("ATTENTION: The command on the server may still be running!"); String url = method.getURI().toString(); int apiPos = url.indexOf("/api"); String logsUrl; if (apiPos != -1) { logsUrl = url.substring(0, apiPos) + "/webapp/systemlogs.html"; } else { logsUrl = "http://" + method.getURI().getHost() + "/artifactory/webapp/systemlogs.html"; } CliLog.error("Please check the server logs " + logsUrl + " before re-running the command."); return null; } }
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:// w w w. ja v a 2 s.c om 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. * /* ww w.jav a2 s .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
/** * @param martServiceLocation/*from www. j a va 2s. c o m*/ * @param data * @return * @throws MartServiceException */ private static InputStream executeMethod(HttpMethod method, String martServiceLocation) throws MartServiceException { HttpClient client = new HttpClient(); if (isProxyHost(martServiceLocation)) { setProxy(client); } method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // method.getParams().setSoTimeout(60000); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw constructException(method, martServiceLocation, null); } return method.getResponseBodyAsStream(); } catch (IOException e) { throw constructException(method, martServiceLocation, e); } }
From source file:org.chiba.xml.xforms.connector.http.AbstractHTTPConnector.java
protected void handleHttpMethod(HttpMethod httpMethod) throws Exception { Header[] headers = httpMethod.getResponseHeaders(); this.responseHeader = new HashMap(); for (int index = 0; index < headers.length; index++) { responseHeader.put(headers[index].getName(), headers[index].getValue()); }/* ww w. j av a2s . c o m*/ this.responseBody = httpMethod.getResponseBodyAsStream(); }
From source file:org.codeartisans.proxilet.Proxilet.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response back to the client via the given * {@link HttpServletResponse}.//from ww w . j a v a2 s. c o m * * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied response back to the client * @throws IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws ServletException Can be thrown to indicate that another error has occurred */ private void executeProxyRequest(HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { // Create a default HttpClient HttpClient httpClient; httpClient = createClientWithLogin(); httpMethodProxyRequest.setFollowRedirects(false); // Execute the request int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); // String response = httpMethodProxyRequest.getResponseBodyAsString(); // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */ ) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(HEADER_LOCATION).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + HEADER_LOCATION + " header was found in the response"); } // Modify the redirect to go to this proxy servlet rather that the proxied host String stringMyHostName = httpServletRequest.getServerName(); if (httpServletRequest.getServerPort() != 80) { stringMyHostName += ":" + httpServletRequest.getServerPort(); } stringMyHostName += httpServletRequest.getContextPath(); if (followRedirects) { httpServletResponse .sendRedirect(stringLocation.replace(getProxyHostAndPort() + proxyPath, stringMyHostName)); return; } } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. httpServletResponse.setIntHeader(HEADER_CONTENT_LENGTH, 0); httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } // Pass the response code back to the client httpServletResponse.setStatus(intProxyResponseCode); // Pass response headers back to the client Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); for (Header header : headerArrayResponse) { if (header.getName().equals("Transfer-Encoding") && header.getValue().equals("chunked") || header.getName().equals("Content-Encoding") && header.getValue().equals("gzip")) { // proxy servlet does not support chunked encoding } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } List<Header> responseHeaders = Arrays.asList(headerArrayResponse); // FIXME We should handle both String and bytes response in the same way: String response = null; byte[] bodyBytes = null; if (isBodyParameterGzipped(responseHeaders)) { LOGGER.trace("GZipped: true"); if (!followRedirects && intProxyResponseCode == HttpServletResponse.SC_MOVED_TEMPORARILY) { response = httpMethodProxyRequest.getResponseHeader(HEADER_LOCATION).getValue(); httpServletResponse.setStatus(HttpServletResponse.SC_OK); intProxyResponseCode = HttpServletResponse.SC_OK; httpServletResponse.setHeader(HEADER_LOCATION, response); httpServletResponse.setContentLength(response.length()); } else { bodyBytes = ungzip(httpMethodProxyRequest.getResponseBody()); httpServletResponse.setContentLength(bodyBytes.length); } } if (httpServletResponse.getContentType() != null && httpServletResponse.getContentType().contains("text")) { LOGGER.trace("Received status code: {} Response: {}", intProxyResponseCode, response); } else { LOGGER.trace("Received status code: {} [Response is not textual]", intProxyResponseCode); } // Send the content to the client if (response != null) { httpServletResponse.getWriter().write(response); } else if (bodyBytes != null) { httpServletResponse.getOutputStream().write(bodyBytes); } else { IOUtils.copy(httpMethodProxyRequest.getResponseBodyAsStream(), httpServletResponse.getOutputStream()); } }