List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:org.apache.cocoon.transformation.SparqlTransformer.java
private void executeRequest(String url, String method, Map httpHeaders, SourceParameters requestParameters) throws ProcessingException, IOException, SAXException { HttpClient httpclient = new HttpClient(); if (System.getProperty("http.proxyHost") != null) { // getLogger().warn("PROXY: "+System.getProperty("http.proxyHost")); String nonProxyHostsRE = System.getProperty("http.nonProxyHosts", ""); if (nonProxyHostsRE.length() > 0) { String[] pHosts = nonProxyHostsRE.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*").split("\\|"); nonProxyHostsRE = ""; for (String pHost : pHosts) { nonProxyHostsRE += "|(^https?://" + pHost + ".*$)"; }// w w w . j a v a2s . c om nonProxyHostsRE = nonProxyHostsRE.substring(1); } if (nonProxyHostsRE.length() == 0 || !url.matches(nonProxyHostsRE)) { try { HostConfiguration hostConfiguration = httpclient.getHostConfiguration(); hostConfiguration.setProxy(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort", "80"))); httpclient.setHostConfiguration(hostConfiguration); } catch (Exception e) { throw new ProcessingException("Cannot set proxy!", e); } } } // Make the HttpMethod. HttpMethod httpMethod = null; // Do not use empty query parameter. if (requestParameters.getParameter(parameterName).trim().equals("")) { requestParameters.removeParameter(parameterName); } // Instantiate different HTTP methods. if ("GET".equalsIgnoreCase(method)) { httpMethod = new GetMethod(url); if (requestParameters.getEncodedQueryString() != null) { httpMethod.setQueryString( requestParameters.getEncodedQueryString().replace("\"", "%22")); /* Also escape '"' */ } else { httpMethod.setQueryString(""); } } else if ("POST".equalsIgnoreCase(method)) { PostMethod httpPostMethod = new PostMethod(url); if (httpHeaders.containsKey(HTTP_CONTENT_TYPE) && ((String) httpHeaders.get(HTTP_CONTENT_TYPE)) .startsWith("application/x-www-form-urlencoded")) { // Encode parameters in POST body. Iterator parNames = requestParameters.getParameterNames(); while (parNames.hasNext()) { String parName = (String) parNames.next(); httpPostMethod.addParameter(parName, requestParameters.getParameter(parName)); } } else { // Use query parameter as POST body httpPostMethod.setRequestBody(requestParameters.getParameter(parameterName)); // Add other parameters to query string requestParameters.removeParameter(parameterName); if (requestParameters.getEncodedQueryString() != null) { httpPostMethod.setQueryString( requestParameters.getEncodedQueryString().replace("\"", "%22")); /* Also escape '"' */ } else { httpPostMethod.setQueryString(""); } } httpMethod = httpPostMethod; } else if ("PUT".equalsIgnoreCase(method)) { PutMethod httpPutMethod = new PutMethod(url); httpPutMethod.setRequestBody(requestParameters.getParameter(parameterName)); requestParameters.removeParameter(parameterName); httpPutMethod.setQueryString(requestParameters.getEncodedQueryString()); httpMethod = httpPutMethod; } else if ("DELETE".equalsIgnoreCase(method)) { httpMethod = new DeleteMethod(url); httpMethod.setQueryString(requestParameters.getEncodedQueryString()); } else { throw new ProcessingException("Unsupported method: " + method); } // Authentication (optional). if (credentials != null && credentials.length() > 0) { String[] unpw = credentials.split("\t"); httpclient.getParams().setAuthenticationPreemptive(true); httpclient.getState().setCredentials(new AuthScope(httpMethod.getURI().getHost(), httpMethod.getURI().getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(unpw[0], unpw[1])); } // Add request headers. Iterator headers = httpHeaders.entrySet().iterator(); while (headers.hasNext()) { Map.Entry header = (Map.Entry) headers.next(); httpMethod.addRequestHeader((String) header.getKey(), (String) header.getValue()); } // Declare some variables before the try-block. XMLizer xmlizer = null; try { // Execute the request. int responseCode; responseCode = httpclient.executeMethod(httpMethod); // Handle errors, if any. if (responseCode < 200 || responseCode >= 300) { if (showErrors) { AttributesImpl attrs = new AttributesImpl(); attrs.addCDATAAttribute("status", "" + responseCode); xmlConsumer.startElement(SPARQL_NAMESPACE_URI, "error", "sparql:error", attrs); String responseBody = httpMethod.getStatusText(); //httpMethod.getResponseBodyAsString(); xmlConsumer.characters(responseBody.toCharArray(), 0, responseBody.length()); xmlConsumer.endElement(SPARQL_NAMESPACE_URI, "error", "sparql:error"); return; // Not a nice, but quick and dirty way to end. } else { throw new ProcessingException("Received HTTP status code " + responseCode + " " + httpMethod.getStatusText() + ":\n" + httpMethod.getResponseBodyAsString()); } } // Parse the response if (responseCode == 204) { // No content. String statusLine = httpMethod.getStatusLine().toString(); xmlConsumer.startElement(SPARQL_NAMESPACE_URI, "result", "sparql:result", EMPTY_ATTRIBUTES); xmlConsumer.characters(statusLine.toCharArray(), 0, statusLine.length()); xmlConsumer.endElement(SPARQL_NAMESPACE_URI, "result", "sparql:result"); } else if (parse.equalsIgnoreCase("xml")) { InputStream responseBodyStream = httpMethod.getResponseBodyAsStream(); xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE); xmlizer.toSAX(responseBodyStream, "text/xml", httpMethod.getURI().toString(), new IncludeXMLConsumer(xmlConsumer)); responseBodyStream.close(); } else if (parse.equalsIgnoreCase("text")) { xmlConsumer.startElement(SPARQL_NAMESPACE_URI, "result", "sparql:result", EMPTY_ATTRIBUTES); String responseBody = httpMethod.getResponseBodyAsString(); xmlConsumer.characters(responseBody.toCharArray(), 0, responseBody.length()); xmlConsumer.endElement(SPARQL_NAMESPACE_URI, "result", "sparql:result"); } else { throw new ProcessingException("Unknown parse type: " + parse); } } catch (ServiceException e) { throw new ProcessingException("Cannot find the right XMLizer for " + XMLizer.ROLE, e); } finally { if (xmlizer != null) manager.release((Component) xmlizer); httpMethod.releaseConnection(); } }
From source file:org.apache.cxf.aegis.jaxws.AegisJaxwsGetTest.java
@Test public void testGetEcho() throws Exception { HttpClient httpClient = createClient(); String url = "http://localhost:" + PORT + "/Echo/echo/echo/hello"; HttpMethod method = null; method = new GetMethod(url); int status = httpClient.executeMethod(method); assertEquals(HttpStatus.SC_OK, status); String result = method.getResponseBodyAsString(); assertTrue(result.contains("hello")); method.releaseConnection();/* w w w.ja v a 2s . c o m*/ httpClient = createClient(); url = "http://localhost:" + PORT + "/Echo/echo/echo/hello?wsdl"; method = new GetMethod(url); status = httpClient.executeMethod(method); assertEquals(HttpStatus.SC_OK, status); Document doc = StaxUtils.read(method.getResponseBodyAsStream()); Map<String, String> ns = new HashMap<String, String>(); ns.put("xsd", "http://www.w3.org/2001/XMLSchema"); NodeList nl = XPathAssert.assertValid("//xsd:element[@name='firstHeader']", doc.getDocumentElement(), ns); assertEquals(1, nl.getLength()); }
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Method to obtain an {@link InputStream} to read the response * from the server.//from ww w .j a v a2s . c o m * * @return {@link InputStream} containing data sent from the server. * @throws IOException if some I/O problem occurs. * @throws SourceNotFoundException if the source doesn't exist. */ public InputStream getInputStream() throws IOException, SourceNotFoundException { final HttpMethod method = this.getMethod(); int response = this.executeMethod(method); this.m_dataValid = true; // throw SourceNotFoundException - according to Source API we // need to throw this if the source doesn't exist. if (!this.exists()) { final StringBuffer error = new StringBuffer(); error.append("Unable to retrieve URI: "); error.append(this.m_uri); error.append(" ("); error.append(response); error.append(")"); throw new SourceNotFoundException(error.toString()); } return method.getResponseBodyAsStream(); }
From source file:org.apache.hadoop.fs.swift.http.HttpInputStreamWithRelease.java
public HttpInputStreamWithRelease(URI uri, HttpMethod method) throws IOException { this.uri = uri; this.method = method; if (method == null) { throw new IllegalArgumentException("Null 'method' parameter "); }/*from w w w . ja va2s . c o m*/ try { inStream = method.getResponseBodyAsStream(); } catch (IOException e) { inStream = new ByteArrayInputStream(new byte[] {}); throw releaseAndRethrow("getResponseBodyAsStream() in constructor", e); } }
From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java
/** * Download link and have it be the response. * @param req the http request/*from w ww . j a v a 2s . co m*/ * @param resp the http response * @param link the link to download * @param c the cookie to set if any * @throws IOException on any error. */ private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c, String proxyHost) throws IOException { org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(link.toString(), false); HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); // Make sure we send the request from the proxy address in the config // since that is what the AM filter checks against. IP aliasing or // similar could cause issues otherwise. HostConfiguration config = new HostConfiguration(); InetAddress localAddress = InetAddress.getByName(proxyHost); if (LOG.isDebugEnabled()) { LOG.debug("local InetAddress for proxy host: " + localAddress.toString()); } config.setLocalAddress(localAddress); HttpMethod method = new GetMethod(uri.getEscapedURI()); @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (passThroughHeaders.contains(name)) { String value = req.getHeader(name); LOG.debug("REQ HEADER: " + name + " : " + value); method.setRequestHeader(name, value); } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } OutputStream out = resp.getOutputStream(); try { resp.setStatus(client.executeMethod(config, method)); for (Header header : method.getResponseHeaders()) { resp.setHeader(header.getName(), header.getValue()); } if (c != null) { resp.addCookie(c); } InputStream in = method.getResponseBodyAsStream(); if (in != null) { IOUtils.copyBytes(in, out, 4096, true); } } finally { method.releaseConnection(); } }
From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java
/** * @param method//w w w .j a v a2 s . c om * @param bodyIsXml if true the body will be parsed as xml else the body will be inserted as string * @return * @throws IOException */ public static Element prepareDetailsElement(HttpMethod method, boolean bodyIsXml) throws IOException { Document doc = DOMUtils.newDocument(); Element detailsEl = doc.createElementNS(null, "details"); Element statusLineEl = statusLineToElement(doc, method.getStatusLine()); detailsEl.appendChild(statusLineEl); // set the body if any final InputStream bodyAsStream = method.getResponseBodyAsStream(); if (bodyAsStream != null) { Element bodyEl = doc.createElementNS(null, "responseBody"); detailsEl.appendChild(bodyEl); // first, try to parse the body as xml // if it fails, put it as string in the body element boolean exceptionDuringParsing = false; if (bodyIsXml) { try { Element parsedBodyEl = DOMUtils.parse(bodyAsStream).getDocumentElement(); bodyEl.appendChild(parsedBodyEl); } catch (Exception e) { String errmsg = "Unable to parse the response body as xml. Body will be inserted as string."; if (log.isDebugEnabled()) log.debug(errmsg, e); exceptionDuringParsing = true; } } if (!bodyIsXml || exceptionDuringParsing) { bodyEl.setTextContent(method.getResponseBodyAsString()); } } return detailsEl; }
From source file:org.apache.oodt.cas.filemgr.catalog.solr.SolrClient.java
/** * Common functionality for HTTP GET and POST requests. * @param method// ww w . j a v a 2 s .co m * @return * @throws Exception */ private String doHttp(HttpMethod method) throws IOException, CatalogException { StringBuilder response = new StringBuilder(); BufferedReader br = null; try { // send request HttpClient httpClient = new HttpClient(); // OODT-719 Prevent httpclient from spawning closewait tcp connections method.setRequestHeader("Connection", "close"); int statusCode = httpClient.executeMethod(method); // read response if (statusCode != HttpStatus.SC_OK) { // still consume the response method.getResponseBodyAsString(); throw new CatalogException("HTTP method failed: " + method.getStatusLine()); } else { // read the response body. br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); String readLine; while (((readLine = br.readLine()) != null)) { response.append(readLine); } } } finally { // must release the connection even if an exception occurred method.releaseConnection(); if (br != null) { try { br.close(); } catch (Exception ignored) { } } } return response.toString(); }
From source file:org.apache.sling.discovery.impl.topology.connector.TopologyRequestValidator.java
/** * @param method the response method./*from www . ja va 2s .c o m*/ * @return the body of the response from the server. * @throws IOException */ private String getResponseBody(HttpMethod method) throws IOException { final Header contentEncoding = method.getResponseHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) { // then the server sent gzip - treat it so: final GZIPInputStream gzipIn = new GZIPInputStream(method.getResponseBodyAsStream()); final String gunzippedEncodedJson = IOUtils.toString(gzipIn); gzipIn.close(); return gunzippedEncodedJson; } else { // otherwise the server sent plaintext: if (method instanceof HttpMethodBase) { return ((HttpMethodBase) method).getResponseBodyAsString(16 * 1024 * 1024); } return method.getResponseBodyAsString(); } }
From source file:org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.java
public NamedList<Object> request(final SolrRequest request, ResponseParser processor) throws SolrServerException, IOException { HttpMethod method = null; InputStream is = null;//from ww w . j a v a2s. c om SolrParams params = request.getParams(); Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = "/select"; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = _parser; } // The parser 'wt=' and 'version=' params are used instead of the original params ModifiableSolrParams wparams = new ModifiableSolrParams(); wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); if (params == null) { params = wparams; } else { params = new DefaultSolrParams(wparams, params); } if (_invariantParams != null) { params = new DefaultSolrParams(_invariantParams, params); } int tries = _maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new GetMethod(_baseURL + path + ClientUtils.toQueryString(params, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = _baseURL + path; boolean isMultipart = (streams != null && streams.size() > 1); if (streams == null || isMultipart) { PostMethod post = new PostMethod(url); post.getParams().setContentCharset("UTF-8"); if (!this.useMultiPartPost && !isMultipart) { post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<Part> parts = new LinkedList<Part>(); Iterator<String> iter = params.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = params.getParams(p); if (vals != null) { for (String v : vals) { if (this.useMultiPartPost || isMultipart) { parts.add(new StringPart(p, v, "UTF-8")); } else { post.addParameter(p, v); } } } } if (isMultipart) { int i = 0; for (ContentStream content : streams) { final ContentStream c = content; String charSet = null; PartSource source = new PartSource() { public long getLength() { return c.getSize(); } public String getFileName() { return c.getName(); } public InputStream createInputStream() throws IOException { return c.getStream(); } }; parts.add(new FilePart(c.getName(), source, c.getContentType(), charSet)); } } if (parts.size() > 0) { post.setRequestEntity(new MultipartRequestEntity( parts.toArray(new Part[parts.size()]), post.getParams())); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = ClientUtils.toQueryString(params, false); PostMethod post = new PostMethod(url + pstr); // post.setRequestHeader("connection", "close"); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setRequestEntity(new RequestEntity() { public long getContentLength() { return -1; } public String getContentType() { return contentStream[0].getContentType(); } public boolean isRepeatable() { return false; } public void writeRequest(OutputStream outputStream) throws IOException { ((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream); } }); } else { is = contentStream[0].getStream(); post.setRequestEntity( new InputStreamRequestEntity(is, contentStream[0].getContentType())); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { // This is generally safe to retry on method.releaseConnection(); method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if ((tries < 1)) { throw r; } //log.warn( "Caught: " + r + ". Retrying..." ); } } } catch (IOException ex) { log.error("####request####", ex); throw new SolrServerException("error reading streams", ex); } method.setFollowRedirects(_followRedirects); method.addRequestHeader("User-Agent", AGENT); if (_allowCompression) { method.setRequestHeader(new Header("Accept-Encoding", "gzip,deflate")); } // method.setRequestHeader("connection", "close"); try { // Execute the method. //System.out.println( "EXECUTE:"+method.getURI() ); int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { StringBuilder msg = new StringBuilder(); msg.append(method.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append(method.getStatusText()); msg.append("\n\n"); msg.append("request: " + method.getURI()); throw new SolrException(statusCode, java.net.URLDecoder.decode(msg.toString(), "UTF-8")); } // Read the contents String charset = "UTF-8"; if (method instanceof HttpMethodBase) { charset = ((HttpMethodBase) method).getResponseCharSet(); } InputStream respBody = method.getResponseBodyAsStream(); // Jakarta Commons HTTPClient doesn't handle any // compression natively. Handle gzip or deflate // here if applicable. if (_allowCompression) { Header contentEncodingHeader = method.getResponseHeader("Content-Encoding"); if (contentEncodingHeader != null) { String contentEncoding = contentEncodingHeader.getValue(); if (contentEncoding.contains("gzip")) { //log.debug( "wrapping response in GZIPInputStream" ); respBody = new GZIPInputStream(respBody); } else if (contentEncoding.contains("deflate")) { //log.debug( "wrapping response in InflaterInputStream" ); respBody = new InflaterInputStream(respBody); } } else { Header contentTypeHeader = method.getResponseHeader("Content-Type"); if (contentTypeHeader != null) { String contentType = contentTypeHeader.getValue(); if (contentType != null) { if (contentType.startsWith("application/x-gzip-compressed")) { //log.debug( "wrapping response in GZIPInputStream" ); respBody = new GZIPInputStream(respBody); } else if (contentType.startsWith("application/x-deflate")) { //log.debug( "wrapping response in InflaterInputStream" ); respBody = new InflaterInputStream(respBody); } } } } } return processor.processResponse(respBody, charset); } catch (HttpException e) { throw new SolrServerException(e); } catch (IOException e) { throw new SolrServerException(e); } finally { method.releaseConnection(); if (is != null) { is.close(); } } }
From source file:org.apache.wookie.proxy.ProxyClient.java
private String executeMethod(HttpMethod method, Configuration properties) throws Exception, AuthenticationException { // Execute the method. try {/*from www. j a va 2s .co m*/ HttpClient client = new HttpClient(); // set the clients proxy values if needed ConnectionsPrefsManager.setProxySettings(client, properties); if (fUseProxyAuthentication) { if (fBase64Auth != null) { method.setRequestHeader("Authorization", fBase64Auth); } else { List<String> authPrefs = new ArrayList<String>(2); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); // send the basic authentication response even before the server gives an unauthorized response client.getParams().setAuthenticationPreemptive(true); // Pass our credentials to HttpClient client.getState().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(fProxyUsername, fProxyPassword)); } } // Add user language to http request in order to notify server of user's language Locale locale = Locale.getDefault(); method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$ method.removeRequestHeader("Content-Type"); //method.setRequestHeader("Content-Type","application/json"); //method.setRequestHeader("Referer", ""); //method.removeRequestHeader("Referer"); method.setRequestHeader("Accept", "*/*"); int statusCode = client.executeMethod(method); //System.out.println("response="+method.getResponseBodyAsString()); //System.out.println("method="+method.toString()); //System.out.println("response="+method.getResponseBodyAsStream()); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { Header hType = method.getResponseHeader("Content-Type"); if (hType != null) { fContentType = hType.getValue(); } // for now we are only expecting Strings //return method.getResponseBodyAsString(); return readFully(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8")); } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED || statusCode == HttpStatus.SC_UNAUTHORIZED) { throw new AuthenticationException("Authentication failed:" + method.getStatusLine() + ' ' + method.getURI() + ' ' + method.getStatusText()); } else { throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$ + method.getStatusText()); } } catch (IOException e) { throw e; } finally { // Release the connection. method.releaseConnection(); } }