List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseBody
@Override public byte[] getResponseBody() throws IOException
From source file:fr.eurecom.nerd.core.proxy.ExtractivClient.java
/** * Get the output from the REST server/*from ww w. ja va 2 s .c o m*/ */ private static InputStream fetchHttpRequest(final HttpMethodBase method) throws BadInputException { try { final int statusCode = getClient().executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new BadInputException( "webpage status " + HttpStatus.getStatusText(statusCode) + "(" + statusCode + ")"); } final InputStream is = new ByteArrayInputStream(method.getResponseBody()); final GZIPInputStream zippedInputStream = new GZIPInputStream(is); return zippedInputStream; } catch (HttpException e) { throw new BadInputException("Fatal protocol violation " + e.getMessage(), e); } catch (IOException e) { //e.g. www.google.assadsaddsa throw new BadInputException("Fatal transport error " + e.getMessage(), e); } finally { method.releaseConnection(); } }
From source file:de.juwimm.cms.common.http.HttpClientWrapper.java
public synchronized byte[] getByte(String destUrl, String userName, String password) throws IOException { HttpMethodBase method = invoke(destUrl, userName, password); byte[] retArr; retArr = method.getResponseBody(); method.releaseConnection();//ww w . j a va2s . com return retArr; }
From source file:de.avanux.livetracker.sender.LocationSender.java
private String executeHttpMethod(HttpMethodBase method) { String response = null;/* w w w . ja v a 2 s. c o m*/ HttpClient client = new HttpClient(); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); response = new String(responseBody); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:com.sittinglittleduck.DirBuster.Worker.java
private String getResponseAsString(HttpMethodBase httpMethod) throws IOException { Charset chartSet = getCharsetFrom(httpMethod); return new String(httpMethod.getResponseBody(), chartSet); }
From source file:jeeves.utils.XmlRequest.java
private Element doExecute(HttpMethodBase httpMethod) throws IOException, BadXmlResponseEx { config.setHost(host, port, Protocol.getProtocol(protocol)); if (useProxy) config.setProxy(proxyHost, proxyPort); byte[] data = null; try {//w w w.ja v a 2s.c om client.executeMethod(httpMethod); data = httpMethod.getResponseBody(); // HttpClient is unable to automatically handle redirects of entity // enclosing methods such as POST and PUT. // Get the location header and run the request against it. String redirectLocation; Header locationHeader = httpMethod.getResponseHeader("location"); if (locationHeader != null) { redirectLocation = locationHeader.getValue(); httpMethod.setPath(redirectLocation); client.executeMethod(httpMethod); data = httpMethod.getResponseBody(); } return Xml.loadStream(new ByteArrayInputStream(data)); } catch (JDOMException e) { throw new BadXmlResponseEx(new String(data, "UTF8")); } finally { httpMethod.releaseConnection(); sentData = getSentData(httpMethod); receivedData = getReceivedData(httpMethod, data); } }
From source file:com.progress.codeshare.esbservice.http.HTTPService.java
public void service(final XQServiceContext ctx) throws XQServiceException { try {/*from w w w . ja v a 2 s.co m*/ final XQMessageFactory factory = ctx.getMessageFactory(); final XQParameters params = ctx.getParameters(); final int messagePart = params.getIntParameter(PARAM_MESSAGE_PART, XQConstants.PARAM_STRING); final String method = params.getParameter(PARAM_METHOD, XQConstants.PARAM_STRING); final String uri = params.getParameter(PARAM_URI, XQConstants.PARAM_STRING); while (ctx.hasNextIncoming()) { final XQEnvelope env = ctx.getNextIncoming(); final XQMessage origMsg = env.getMessage(); final XQMessage newMsg = factory.createMessage(); final HttpClient client = new HttpClient(); final Iterator headerIterator = origMsg.getHeaderNames(); if (METHOD_DELETE.equals(method)) { final HttpMethodBase req = new DeleteMethod(uri); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher matcher = PATTERN_HEADER.matcher(header); if (matcher.find()) req.addRequestHeader(matcher.group(1), (String) origMsg.getHeaderValue(matcher.group())); } client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_GET.equals(method)) { final HttpMethodBase req = new GetMethod(); final List paramList = new ArrayList(); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher headerMatcher = PATTERN_HEADER.matcher(header); if (headerMatcher.find()) { req.addRequestHeader(headerMatcher.group(1), (String) origMsg.getHeaderValue(headerMatcher.group())); continue; } final Matcher paramMatcher = PATTERN_PARAM.matcher(header); if (paramMatcher.find()) paramList.add(new NameValuePair(paramMatcher.group(1), (String) origMsg.getHeaderValue(paramMatcher.group()))); } req.setQueryString((NameValuePair[]) paramList.toArray(new NameValuePair[] {})); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_POST.equals(method)) { final PostMethod req = new PostMethod(uri); /* * Copy all XQ headers and extract HTTP headers and * parameters */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher headerMatcher = PATTERN_HEADER.matcher(header); if (headerMatcher.find()) { req.addRequestHeader(headerMatcher.group(1), (String) origMsg.getHeaderValue(headerMatcher.group())); continue; } final Matcher paramMatcher = PATTERN_PARAM.matcher(header); if (paramMatcher.find()) req.addParameter(new NameValuePair(paramMatcher.group(1), (String) origMsg.getHeaderValue(paramMatcher.group()))); } final XQPart origPart = origMsg.getPart(messagePart); req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(), origPart.getContentType(), null)); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } else if (METHOD_PUT.equals(method)) { final EntityEnclosingMethod req = new PutMethod(uri); /* Copy all XQ headers and extract HTTP headers */ while (headerIterator.hasNext()) { final String header = (String) headerIterator.next(); newMsg.setHeaderValue(header, origMsg.getHeaderValue(header)); final Matcher matcher = PATTERN_HEADER.matcher(header); if (matcher.find()) req.addRequestHeader(matcher.group(1), (String) origMsg.getHeaderValue(matcher.group())); } final XQPart origPart = origMsg.getPart(messagePart); req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(), origPart.getContentType(), null)); client.executeMethod(req); /* Transform all HTTP to XQ headers */ final Header[] headers = req.getResponseHeaders(); for (int i = 0; i < headers.length; i++) newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue()); final XQPart newPart = newMsg.createPart(); newPart.setContentId("Result"); newPart.setContent(new String(req.getResponseBody()), req.getResponseHeader("Content-Type").getValue()); newMsg.addPart(newPart); } env.setMessage(newMsg); final Iterator addressIterator = env.getAddresses(); if (addressIterator.hasNext()) ctx.addOutgoing(env); } } catch (final Exception e) { throw new XQServiceException(e); } }
From source file:fr.openwide.talendalfresco.rest.client.AlfrescoRestClient.java
public void execute(ClientCommand clientCommand) throws RestClientException { int statusCode = -1; HttpMethodBase method = null; try {/* www . j ava 2s . co m*/ // building method (and body entity if any) method = clientCommand.createMethod(); // setting server URL method.setURI(new URI(restCommandUrlPrefix + clientCommand.getName(), false)); method.getParams().setContentCharset(this.restEncoding); // building params (adding ticket) List<NameValuePair> params = clientCommand.getParams(); params.add(new NameValuePair(TICKET_PARAM, ticket)); method.setQueryString(params.toArray(EMPTY_NAME_VALUE_PAIR)); // Execute the method. statusCode = client.executeMethod(method); // checking HTTP status if (statusCode != HttpStatus.SC_OK) { throw new RestClientException("Bad HTTP Status : " + statusCode); } // parsing response XMLEventReader xmlReader = null; try { xmlReader = XmlHelper.createXMLEventReader(method.getResponseBodyAsStream(), this.restEncoding); clientCommand.handleResponse(xmlReader); if (!RestConstants.CODE_OK.equals(clientCommand.getResultCode())) { //String msg = "Business error in command " + clientCommand.toString(); //logger.error(msg, e); throw new RestClientException(clientCommand.getResultMessage(), new RestClientException(clientCommand.getResultError())); } } catch (XMLStreamException e) { String msg = "XML parsing error on response body : "; try { msg += new String(method.getResponseBody()); } catch (IOException ioex) { msg += "[unreadable]"; } ; //logger.error(msg, e); throw new RestClientException(msg, e); } catch (IOException e) { String msg = "IO Error when parsing XML response body : "; //logger.error(msg, e); throw new RestClientException(msg, e); } finally { if (xmlReader != null) { try { xmlReader.close(); } catch (Throwable t) { } } } } catch (RestClientException rcex) { throw rcex; } catch (URIException e) { throw new RestClientException("URI error while executing command " + clientCommand, e); } catch (HttpException e) { throw new RestClientException("HTTP error while executing command " + clientCommand, e); } catch (IOException e) { throw new RestClientException("IO error while executing command " + clientCommand, e); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:com.snaker.DownloadManager.java
private Runnable createRunnable(final Downloader d) { final Task task = d.getTask(); return new Runnable() { @Override//from w ww.j a v a 2 s . co m public void run() { logger.info("start download:" + d.getUrl()); HttpClient client = clients.get(task.getId()); DownloadHandler handler = d.getHandler(); HttpMethodBase m = null; d.setStatus(Status.STARTED); d.setStartTime(System.currentTimeMillis()); try { String url = d.getUrl(); if (d.isGet()) { GetMethod get = new GetMethod(url); m = get; } else { final String requestCharset = d.getRequestCharset(); PostMethod post = new PostMethod(url) { public String getRequestCharSet() { if (requestCharset != null) return requestCharset; else return super.getRequestCharSet(); } public boolean getFollowRedirects() { return d.isFollowRedirects(); } }; if (requestCharset != null) { post.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=" + requestCharset); } DownloadParams parms = d.getParms(); if (parms != null) post.setRequestBody(parms.toNVP()); m = post; } { // set the headers m.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"); m.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); m.setRequestHeader("Accept-Language", "en-us,zh-cn;q=0.5"); m.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); m.setRequestHeader("Referer", url); } client.executeMethod(m); //check status int sc = m.getStatusCode(); d.setStatusCode(sc); if (isBadStatusCode(sc)) { logger.error("download failed,url:" + d.getUrl() + ",Status Code:" + sc); d.setStatus(Status.FAILED); d.setDescription(m.getStatusText()); return; } else if (sc == 404 || sc == 410) { d.setStatus(Status.FINISHED); d.setDescription("NOT FOUND"); return; } long size = m.getResponseContentLength(); d.setFileSize(size); // get File Name if (d.getFileName() == null) { Header h = m.getResponseHeader("Content-Disposition"); String fileName = null; if (h != null) { String f = h.getValue(); int tag = f.indexOf("filename="); if (tag != -1 && tag != f.length() - 1) fileName = f.substring(tag + 1); } if (fileName == null || fileName.length() == 0) { int tag1 = url.lastIndexOf("/"); int tag2 = url.lastIndexOf("?"); if (tag1 != -1 && tag1 != url.length() - 1) { if (tag2 > tag1) { fileName = url.substring(tag1 + 1, tag2); } else { fileName = url.substring(tag1 + 1); } } } d.setFileName(fileName); } // set the all headers Header[] headers = m.getResponseHeaders(); if (headers != null) { for (Header header : headers) { d.addResponseHeader(header.getName(), header.getValue()); } } d.setStatus(Status.RUNNING); // recv the body if (handler == null) { byte[] content = m.getResponseBody(); int len = content.length; d.setFileSize(len); d.setReceived(len); d.setResponseCharset(m.getResponseCharSet()); d.setResponseBody(content); } else { InputStream is = m.getResponseBodyAsStream(); handler.start(d); byte[] buffer = new byte[102400]; long count = 0; while (true) { int r = is.read(buffer); if (r > 0) { count += r; d.setReceived(count); handler.handle(buffer, r); } else { break; } } is.close(); } d.setStatus(Status.FINISHED); } catch (Exception e) { logger.error("download failed,url:" + d.getUrl(), e); d.setStatus(Status.FAILED); d.setDescription(e.getMessage()); } finally { m.releaseConnection(); if (handler != null) { handler.stop(); } downloadings.remove(d); d.setEndTime(System.currentTimeMillis()); while (downloaded.size() >= maxDownloadedCount) { downloaded.poll(); } downloaded.offer(d); task.downloadFininshed(d); } } }; }
From source file:org.alfresco.repo.remoteconnector.RemoteConnectorServiceImpl.java
/** * Executes the specified request, and return the response *//*from w w w. j a va 2 s . c o m*/ public RemoteConnectorResponse executeRequest(RemoteConnectorRequest request) throws IOException, AuthenticationException, RemoteConnectorClientException, RemoteConnectorServerException { RemoteConnectorRequestImpl reqImpl = (RemoteConnectorRequestImpl) request; HttpMethodBase httpRequest = reqImpl.getMethodInstance(); // Attach the headers to the request for (Header hdr : request.getRequestHeaders()) { httpRequest.addRequestHeader(hdr); } // Attach the body, if possible if (httpRequest instanceof EntityEnclosingMethod) { if (request.getRequestBody() != null) { ((EntityEnclosingMethod) httpRequest).setRequestEntity(reqImpl.getRequestBody()); } } // Grab our thread local HttpClient instance // Remember - we must then clean it up! HttpClient httpClient = HttpClientHelper.getHttpClient(); // The url should already be vetted by the RemoteConnectorRequest URL url = new URL(request.getURL()); // Use the appropriate Proxy Host if required if (httpProxyHost != null && url.getProtocol().equals("http") && requiresProxy(url.getHost())) { httpClient.getHostConfiguration().setProxyHost(httpProxyHost); if (logger.isDebugEnabled()) logger.debug(" - using HTTP proxy host for: " + url); if (httpProxyCredentials != null) { httpClient.getState().setProxyCredentials(httpAuthScope, httpProxyCredentials); if (logger.isDebugEnabled()) logger.debug(" - using HTTP proxy credentials for proxy: " + httpProxyHost.getHostName()); } } else if (httpsProxyHost != null && url.getProtocol().equals("https") && requiresProxy(url.getHost())) { httpClient.getHostConfiguration().setProxyHost(httpsProxyHost); if (logger.isDebugEnabled()) logger.debug(" - using HTTPS proxy host for: " + url); if (httpsProxyCredentials != null) { httpClient.getState().setProxyCredentials(httpsAuthScope, httpsProxyCredentials); if (logger.isDebugEnabled()) logger.debug(" - using HTTPS proxy credentials for proxy: " + httpsProxyHost.getHostName()); } } else { //host should not be proxied remove any configured proxies httpClient.getHostConfiguration().setProxyHost(null); httpClient.getState().clearProxyCredentials(); } // Log what we're doing if (logger.isDebugEnabled()) { logger.debug("Performing " + request.getMethod() + " request to " + request.getURL()); for (Header hdr : request.getRequestHeaders()) { logger.debug("Header: " + hdr); } Object requestBody = null; if (request != null) { requestBody = request.getRequestBody(); } if (requestBody != null && requestBody instanceof StringRequestEntity) { StringRequestEntity re = (StringRequestEntity) request.getRequestBody(); logger.debug("Payload (string): " + re.getContent()); } else if (requestBody != null && requestBody instanceof ByteArrayRequestEntity) { ByteArrayRequestEntity re = (ByteArrayRequestEntity) request.getRequestBody(); logger.debug("Payload (byte array): " + re.getContent().toString()); } else { logger.debug("Payload is not of a readable type."); } } // Perform the request, and wrap the response int status = -1; String statusText = null; RemoteConnectorResponse response = null; try { status = httpClient.executeMethod(httpRequest); statusText = httpRequest.getStatusText(); Header[] responseHdrs = httpRequest.getResponseHeaders(); Header responseContentTypeH = httpRequest .getResponseHeader(RemoteConnectorRequestImpl.HEADER_CONTENT_TYPE); String responseCharSet = httpRequest.getResponseCharSet(); String responseContentType = (responseContentTypeH != null ? responseContentTypeH.getValue() : null); if (logger.isDebugEnabled()) { logger.debug( "response url=" + request.getURL() + ", length =" + httpRequest.getResponseContentLength() + ", responceContentType " + responseContentType + ", statusText =" + statusText); } // Decide on how best to handle the response, based on the size // Ideally, we want to close the HttpClient resources immediately, but // that isn't possible for very large responses // If we can close immediately, it makes cleanup simpler and fool-proof if (httpRequest.getResponseContentLength() > MAX_BUFFER_RESPONSE_SIZE || httpRequest.getResponseContentLength() == -1) { if (logger.isTraceEnabled()) { logger.trace("large response (or don't know length) url=" + request.getURL()); } // Need to wrap the InputStream in something that'll close InputStream wrappedStream = new HttpClientReleasingInputStream(httpRequest); httpRequest = null; // Now build the response response = new RemoteConnectorResponseImpl(request, responseContentType, responseCharSet, status, responseHdrs, wrappedStream); } else { if (logger.isTraceEnabled()) { logger.debug("small response for url=" + request.getURL()); } // Fairly small response, just keep the bytes and make life simple response = new RemoteConnectorResponseImpl(request, responseContentType, responseCharSet, status, responseHdrs, httpRequest.getResponseBody()); // Now we have the bytes, we can close the HttpClient resources httpRequest.releaseConnection(); httpRequest = null; } } finally { // Make sure, problems or not, we always tidy up (if not large stream based) // This is important because we use a thread local HttpClient instance if (httpRequest != null) { httpRequest.releaseConnection(); httpRequest = null; } } // Log the response if (logger.isDebugEnabled()) logger.debug("Response was " + status + " " + statusText); // Decide if we should throw an exception if (status >= 300) { // Tidy if needed if (httpRequest != null) httpRequest.releaseConnection(); // Specific exceptions if (status == Status.STATUS_FORBIDDEN || status == Status.STATUS_UNAUTHORIZED) { // TODO Forbidden may need to be handled differently. // TODO Need to get error message into the AuthenticationException throw new AuthenticationException(statusText); } // Server side exceptions if (status >= 500 && status <= 599) { logger.error("executeRequest: remote connector server exception: [" + status + "] " + statusText); throw new RemoteConnectorServerException(status, statusText); } if (status == Status.STATUS_PRECONDITION_FAILED) { logger.error("executeRequest: remote connector client exception: [" + status + "] " + statusText); throw new RemoteConnectorClientException(status, statusText, response); } else { // Client request exceptions if (httpRequest != null) { // Response wasn't too big and is available, supply it logger.error( "executeRequest: remote connector client exception: [" + status + "] " + statusText); throw new RemoteConnectorClientException(status, statusText, response); } else { // Response was too large, report without it logger.error( "executeRequest: remote connector client exception: [" + status + "] " + statusText); throw new RemoteConnectorClientException(status, statusText, null); } } } // If we get here, then the request/response was all fine // So, return our created response return response; }
From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClientTest.java
@Test public void restRequestCanReturnBinaryContent() throws IOException { // given// w w w.java 2 s. co m final TypeToken<Byte[]> byteArrayType = new TypeToken<Byte[]>() { }; Request<byte[]> request = new GerritHttpClient(abstractWebLocation).new RestRequest<byte[]>(HttpMethod.GET, "serviceUri", null /*input*/, byteArrayType.getType(), null /*error handler*/); //$NON-NLS-1$ HttpMethodBase httpMethodBase = mock(HttpMethodBase.class); byte[] binary = "binary".getBytes(); //$NON-NLS-1$ when(httpMethodBase.getResponseBody()).thenReturn(binary); // when byte[] result = request.process(httpMethodBase); // then assertArrayEquals(binary, result); }