List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString
public abstract String getResponseBodyAsString() throws IOException;
From source file:com.sun.socialsite.web.rest.opensocial.ConsumerContext.java
/** * Adds an element to this object's context chain. If the specified * element has a "delegate" attribute, this method will retrieve the * delegation element and recursively add it also. * * @param element the ChainElement to be added to the context chain. *///from w w w.j a v a 2s .c o m private void appendToChain(URL source, boolean loadedDirectly, JSONObject contents) throws SocialSiteException { try { ChainElement element = null; if (contents.has("attributes")) { element = new LegacyChainElement(source, loadedDirectly, contents); } else { element = new ChainElement(source, loadedDirectly, contents); } elements.add(element); if (element.contents.has("delegate")) { JSONObject delegate = element.contents.getJSONObject("delegate"); URL url = null; if (element.source != null) { url = new URL(element.source, delegate.getString("url")); } else { url = new URL(delegate.getString("url")); } HttpClient httpClient = new HttpClient(); HttpMethod method = null; if ("GET".equalsIgnoreCase(delegate.getString("method"))) { method = new GetMethod(url.toExternalForm()); } if (delegate.has("headers")) { JSONObject headers = delegate.getJSONObject("headers"); for (Iterator<?> iterator = headers.keys(); iterator.hasNext();) { String name = iterator.next().toString(); String value = headers.get(name).toString(); method.addRequestHeader(name, value); } } if (element.source != null) { method.setRequestHeader("Referer", element.source.toString()); } int responseCode = httpClient.executeMethod(method); if (responseCode == 200) { String responseBody = method.getResponseBodyAsString(); appendToChain(url, true, new JSONObject(responseBody)); if (log.isDebugEnabled()) { String msg = String.format("%s %s returned %d: %s", method.getName(), url, responseCode, responseBody); log.debug(msg); } } else { String msg = String.format("%s %s returned %d", method.getName(), url, responseCode); throw new SocialSiteException(msg); } } } catch (SocialSiteException e) { throw e; } catch (Exception e) { throw new SocialSiteException(e); } }
From source file:edu.du.penrose.systems.fedoraProxy.web.bus.OaiAggregator.java
/** * Return result of type...//from w w w. j a v a 2 s . co m * * <?xml version="1.0" encoding="UTF-8"?> * <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> * <responseDate>2011-06-07T11:30:42Z</responseDate> * * <request verb="ListRecords" metadataPrefix="oai_dc" resumptionToken="">http://adr.coalliance.org/codu/fez/oai.php</request> * <ListRecords> * <record> * <header> * <identifier>oai:adr.coalliance.org:codu:48566</identifier> * <datestamp>2010-04-25T17:35:02Z</datestamp> * <setSpec>oai:adr.coalliance.org:codu:48565</setSpec> * </header> * <metadata> * <oai_dc:dc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/"> * <dc:title>Girls Stretch at the University of Denver Lamont School of Music</dc:title> * <dc:identifier>http://adr.coalliance.org/codu/fez/view/codu:48566</dc:identifier> * <dc:description>A young female student (foreground) at the University of Denver (DU) Lamont School of Music Dance Department stretches during a dance class in Denver, Colorado. Other female students are visible in the background. Children's classes at the Lamont School of Music were held at 909 Grant Street, Denver, Colorado. The Children's Dance Theatre was the performance group composed of students from the children's dance classes at Lamont.</dc:description> * <dc:type>DU Image</dc:type> * <dc:date>Array</dc:date> * <dc:creator>Brooks, Marshall</dc:creator> * * <dc:subject>Dance</dc:subject> * <dc:subject>Modern dance</dc:subject> * <dc:subject>Children</dc:subject> * <dc:subject>Girls</dc:subject> * <dc:publisher>University of Denver</dc:publisher> * <dc:relation>Vera Sears Papers</dc:relation> * <dc:format>http://adr.coalliance.org/codu/fez/eserv/codu:48566/M272.01.0001.0001.00002.tif</dc:format> * dc:format>http://adr.coalliance.org/codu/fez/eserv/codu:48566/M272.01.0001.0001.00002_access.jpg</dc:format> * </oai_dc:dc> * </metadata> * </record> * <resumptionToken></resumptionToken> * </listRecords> * </OAI-PMH> * * @param getString the get url string * @param response * @param authenicate * @param aggregateSetName * @param metadataPrefix * @return * @throws IOException */ String executeOaiListSetRecords(String getString, HttpServletResponse response, boolean authenicate, String aggregateSetName, String metadataPrefix) throws IOException { HttpClient theClient = new HttpClient(); HttpMethod method = new GetMethod(getString); FedoraDatastream_get.setDefaultHeaders(method); FedoraDatastream_get.setAdrCredentials(theClient, authenicate); String setRecords = null; try { theClient.executeMethod(method); // Set outgoing headers, to match headers from the server Header[] headers = method.getResponseHeaders(); for (Header header : headers) { if ("Content-Type".equalsIgnoreCase(header.getName())) { response.setContentType(header.getValue()); } response.setHeader(header.getName(), header.getValue()); } // Write the body, flush and close setRecords = method.getResponseBodyAsString(); response.getWriter().print(setRecords); } finally { method.releaseConnection(); } // response.getOutputStream().flush(); // response.getOutputStream().close(); return setRecords; }
From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java
Set<String> getUpdateDataIdsInBody(HttpMethod httpMethod) { Set<String> modifiedDataIdSet = new HashSet<String>(); try {/*from w w w. j a va 2 s . c om*/ String modifiedDataIdsString = httpMethod.getResponseBodyAsString(); return convertStringToSet(modifiedDataIdsString); } catch (Exception e) { } return modifiedDataIdSet; }
From source file:edu.du.penrose.systems.fedoraProxy.web.bus.OaiAggregator.java
/** * Get a list of all aggregate sets that an AggregateList object knows about.<br><br> * //from w ww . ja v a 2 s. c o m * NOTE: I am using HttpMethod.getResponseBodyAsString() instead of HttpMethod.getResponseBodyAsStream(), to make it easy to parse, * unfortunately it reads the entire response at once. Is this a potential memory overflow? Since we are reading text not large * binary files, such as a pdf or an image, hopefully we be OK. TBD * <br> * The response is of type... * <br> * <?xml version="1.0" encoding="UTF-8"?> * <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> * <responseDate>2011-06-06T08:46:07Z</responseDate> * * <request verb="ListSets" resumptionToken="">http://adr.coalliance.org/codu/fez/oai.php</request> * <ListSets> * <set> * <setSpec>oai:adr.coalliance.org:codu:37742</setSpec> * <setName>A Nation In Time And Space</setName> * </set> * <set> * inject our aggregate set spec and name * </set> 0..n * </ListSets> * </OAI-PMH> * * @param getString the get url string * @param response * @param authenicate * @param set * @param metadataPrefix * @throws IOException */ private void executeOaiListSetsWithAggregates(String getString, HttpServletResponse response, boolean authenicate) throws IOException { HttpClient theClient = new HttpClient(); HttpMethod method = new GetMethod(getString); AggregateList aggregateSets = new AggregateList(); FedoraDatastream_get.setDefaultHeaders(method); FedoraDatastream_get.setAdrCredentials(theClient, authenicate); try { theClient.executeMethod(method); // Set the content type, as it comes from the server Header[] headers = method.getResponseHeaders(); for (Header header : headers) { if ("Content-Type".equalsIgnoreCase(header.getName())) { response.setContentType(header.getValue()); } response.setHeader(header.getName(), header.getValue()); } // Write the body, flush and close InputStream is = method.getResponseBodyAsStream(); String sets = method.getResponseBodyAsString(); Iterator<AggregateSet> aggreateSetsIterator = aggregateSets.getIterator(); StringBuffer aggSetsString = new StringBuffer(); while (aggreateSetsIterator.hasNext()) { AggregateSet currentAggSet = aggreateSetsIterator.next(); aggSetsString.append("<set>\n"); aggSetsString.append("<setSpec>" + currentAggSet.getUniqueID() + "</setSpec>\n"); aggSetsString.append("<setName>" + currentAggSet.getSetName() + "</setName>\n"); aggSetsString.append("</set>"); } String allSets = sets.replace("<resumptionToken>", aggSetsString.toString() + "\n<resumptionToken>"); response.getWriter().print(allSets); // response.getOutputStream().flush(); // response.getOutputStream().close(); } finally { method.releaseConnection(); } }
From source file:eu.delving.services.controller.SolrProxyController.java
@RequestMapping("/api/solr/select") public void searchController(HttpServletRequest request, HttpServletResponse response) throws Exception { final String solrQueryString = request.getQueryString(); HttpMethod method = new GetMethod( String.format("%s/select?%s", ThemeFilter.getTheme().getSolrSelectUrl(), solrQueryString)); httpClient.executeMethod(method);//from w ww . j ava 2s. c om Boolean getAsStream = false; for (Header header : method.getResponseHeaders()) { if (header.getName().equalsIgnoreCase("content-type")) { final String contentType = method.getResponseHeader("Content-Type").getValue(); response.setContentType(contentType); response.setHeader(header.getName(), header.getValue()); if (contentType.equalsIgnoreCase("application/octet-stream")) { getAsStream = true; } } else if (header.getName().equalsIgnoreCase("server")) { //ignore } else { response.setHeader(header.getName(), header.getValue()); } } response.setCharacterEncoding("UTF-8"); if (getAsStream) { OutputStream out = response.getOutputStream(); try { IOUtils.copy(method.getResponseBodyAsStream(), out); } finally { out.close(); } } else { response.getWriter().write(method.getResponseBodyAsString()); //todo add response from SolrProxy here response.getWriter().close(); } }
From source file:com.worldline.easycukes.rest.client.RestService.java
/** * Gets the result of the execution of a get request. the attempt will be * repeated several times in case of failures * * @param path the path on which the request should be sent * @throws Exception if something's going wrong... *///from ww w .j a v a2s . c o m public void retryGetRequestUntilSucceed(@NonNull String path) throws Exception { String fullpath = path; if (path.startsWith("/")) fullpath = baseUrl + path; log.debug("Sending GET request to " + fullpath + " with several attemps"); final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY)); final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY)); final HttpMethod method = new GetMethod(fullpath); try { for (final Map.Entry<String, String> header : requestHeaders.entrySet()) method.setRequestHeader(header.getKey(), header.getValue()); String responseAsString = null; int statusCode; int attempts = 0; boolean success = false; do { // waiting timeToWait seconds Thread.sleep(timeToWait * 1000); statusCode = httpClient.executeMethod(method); attempts++; // check for status code 200 if (statusCode == HttpStatus.SC_OK) { responseAsString = method.getResponseBodyAsString(); success = true; log.info("The result is available! "); } else log.warn("unsuccessful GET request : " + method.getStatusLine() + " | Waiting " + timeToWait + " seconds ..."); } while (!success && maxAttempts > attempts); response = new ResponseWrapper(responseAsString, statusCode); } catch (final Exception e) { log.error(e.getMessage(), e); throw e; } finally { method.releaseConnection(); } }
From source file:com.sourcesense.confluence.servlets.CMISProxyServlet.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response * back to the client via the given {@link HttpServletResponse} * * @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 * @param httpServletRequest Request object pertaining to the proxied HTTP request * @throws IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws ServletException Can be thrown to indicate that another error has occurred *//* w ww .j av a 2s . c o m*/ private void executeProxyRequest(HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { // Create a default HttpClient HttpClient httpClient = new HttpClient(); getCredential(httpServletRequest.getParameter("servername")); if (credentials != null) { httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } httpMethodProxyRequest.setFollowRedirects(true); // 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(STRING_LOCATION_HEADER).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " 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) { if (stringLocation.contains("jsessionid")) { Cookie cookie = new Cookie("JSESSIONID", stringLocation.substring(stringLocation.indexOf("jsessionid=") + 11)); cookie.setPath("/"); httpServletResponse.addCookie(cookie); //debug("redirecting: set jessionid (" + cookie.getValue() + ") cookie from URL"); } else if (httpMethodProxyRequest.getResponseHeader("Set-Cookie") != null) { Header header = httpMethodProxyRequest.getResponseHeader("Set-Cookie"); String[] cookieDetails = header.getValue().split(";"); String[] nameValue = cookieDetails[0].split("="); Cookie cookie = new Cookie(nameValue[0], nameValue[1]); cookie.setPath("/"); //debug("redirecting: setting cookie: " + cookie.getName() + ":" + cookie.getValue() + " on " + cookie.getPath()); httpServletResponse.addCookie(cookie); } httpServletResponse.sendRedirect(stringLocation .replace(getProxyHostAndPort(httpServletRequest) + this.getProxyPath(), 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(STRING_CONTENT_LENGTH_HEADER_NAME, 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") || // don't copy gzip header header.getName().equals("WWW-Authenticate")) { // don't copy WWW-Authenticate header so browser doesn't prompt on failed basic auth // proxy servlet does not support chunked encoding } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } List<Header> responseHeaders = Arrays.asList(headerArrayResponse); if (isBodyParameterGzipped(responseHeaders)) { debug("GZipped: true"); if (!followRedirects && intProxyResponseCode == HttpServletResponse.SC_MOVED_TEMPORARILY) { response = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); httpServletResponse.setStatus(HttpServletResponse.SC_OK); intProxyResponseCode = HttpServletResponse.SC_OK; httpServletResponse.setHeader(STRING_LOCATION_HEADER, response); } else { response = new String(ungzip(httpMethodProxyRequest.getResponseBody())); } httpServletResponse.setContentLength(response.length()); } // Send the content to the client if (intProxyResponseCode == 200) httpServletResponse.getWriter().write(response); else httpServletResponse.getWriter().write(intProxyResponseCode); }
From source file:com.worldline.easycukes.rest.client.RestService.java
/** * Gets the result of the execution of a get request. the attempt will be * repeated until obtain the exepected result * * @param path the path on which the request should be executed * @param expression the result that should be returned by the GET request, which * allows to know if that request is completely processed or not * @throws Exception if something's going wrong... *///from w ww.java 2 s. c om public void retryGetRequestUntilObtainExpectedResponse(@NonNull String path, @NonNull String expression) throws Exception { String fullpath = path; if (path.startsWith("/")) fullpath = baseUrl + path; log.debug("Sending GET request to " + fullpath + " with several attemps"); final int maxAttempts = Integer.parseInt(ExecutionContext.get(RestConstants.MAX_ATTEMPTS_KEY)); final int timeToWait = Integer.parseInt(ExecutionContext.get(RestConstants.TIME_TO_WAIT_KEY)); final HttpMethod method = new GetMethod(fullpath); try { for (final Map.Entry<String, String> header : requestHeaders.entrySet()) method.setRequestHeader(header.getKey(), header.getValue()); String responseAsString = null; String toCheck = null; String expected = expression; String prop = null; final int idx = expression.indexOf("="); if (idx > 0) { prop = expression.substring(0, idx); expected = expression.substring(idx + 1); } int statusCode; int attempts = 0; boolean success = false; do { // waiting timeToWait seconds Thread.sleep(timeToWait * 1000); statusCode = httpClient.executeMethod(method); attempts++; if (statusCode == HttpStatus.SC_OK) { responseAsString = method.getResponseBodyAsString(); toCheck = responseAsString; if (prop != null) toCheck = JSONHelper.getPropertyValue(responseAsString, prop); if (toCheck.contains(expected)) { success = true; log.debug("The result is available! "); } else log.warn("The result is not yet available! | Waiting " + timeToWait + " seconds ..."); } else log.warn("unsuccessful GET request : " + method.getStatusLine() + " | Waiting " + timeToWait + " seconds ..."); } while (!success && maxAttempts > attempts); response = new ResponseWrapper(responseAsString, statusCode); } catch (final Exception e) { log.error(e.getMessage(), e); throw e; } finally { method.releaseConnection(); } }
From source file:com.zimbra.cs.index.elasticsearch.ElasticSearchConnector.java
public int executeMethod(HttpMethod method) throws IndexStoreException, IOException { String reqBody = ""; if (ZimbraLog.elasticsearch.isTraceEnabled() && method instanceof EntityEnclosingMethod) { EntityEnclosingMethod eem = (EntityEnclosingMethod) method; RequestEntity re = eem.getRequestEntity(); if (re instanceof StringRequestEntity) { StringRequestEntity sre = (StringRequestEntity) re; reqBody = Strings.nullToEmpty(sre.getContent()); if (reqBody.length() > 0) { reqBody = String.format("\nREQUEST BODY=%s", reqBody); }/*from w w w.j a va2 s . c om*/ } } try { HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); statusCode = client.executeMethod(method); } catch (ConnectException ce) { throw new ZimbraElasticSearchDownException(ce); } catch (NoHttpResponseException nhre) { // managed to connect to the ElasticSearch service but it either crashed or timed out before // we got sent back the response. // Could be a temporary problem or a problem with this particular request. // In the longer term we need to track failures related to particular items at a higher level and discard // them after retrying a number of times. throw new ZimbraElasticSearchNoResponseException(nhre); } body = method.getResponseBodyAsString(); ZimbraLog.elasticsearch.trace("ElasticSearch request:%s %s - statusCode=%d%s\nRESPONSE BODY=%s", method.getName(), method.getURI(), statusCode, reqBody, body); return statusCode; }
From source file:it.drwolf.ridire.session.CrawlerManager.java
private String getJobStatus_old(String encodedJobName) throws HttpException, IOException, DocumentException { Pattern finishedPattern = Pattern.compile("<h1>Job <i>" + encodedJobName + "</i> \\(0 launches"); HttpMethod method = null; String ret = ""; try {/* w ww . j a v a 2 s . c om*/ method = new GetMethod(this.engineUri + "job/" + URLEncoder.encode(encodedJobName, "UTF-8")); // TODO check status int status = this.httpClient.executeMethod(method); String body = method.getResponseBodyAsString(); Matcher mFinished = finishedPattern.matcher(body); boolean finished = false; if (mFinished.find()) { finished = true; } Matcher m = CrawlerManager.pStatus.matcher(body); if (m.find()) { ret = m.group(1).trim(); if (ret.equals("Active")) { ret = m.group(2).substring(1).trim(); if (ret.equalsIgnoreCase("Pausing")) { ret = CrawlStatus.PAUSED.toString(); } } } if (ret.equalsIgnoreCase("Unbuilt") && finished) { ret = CrawlStatus.FINISHED.toString(); } method.releaseConnection(); } finally { if (method != null) { method.releaseConnection(); } } return ret; }