List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:com.starit.diamond.client.processor.ServerAddressProcessor.java
/** * ?diamond??//from w w w . ja v a2 s. c o m * * @param acquireCount * ?01? * @return */ private boolean acquireServerAddressOnce(int acquireCount) { HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration(); String configServerAddress; int port; if (null != diamondConfigure.getConfigServerAddress()) { configServerAddress = diamondConfigure.getConfigServerAddress(); port = diamondConfigure.getConfigServerPort(); } else { if (acquireCount == 0) { configServerAddress = Constants.DEFAULT_DOMAINNAME; port = Constants.DEFAULT_PORT; } else { configServerAddress = Constants.DAILY_DOMAINNAME; port = Constants.DEFAULT_PORT; } } hostConfiguration.setHost(configServerAddress, port); // ??serverURL by leiwen String serverAddressUrl = Constants.BASE_URI + "/" + clusterType; HttpMethod httpMethod = new GetMethod(serverAddressUrl); // HttpMethod? HttpMethodParams params = new HttpMethodParams(); params.setSoTimeout(diamondConfigure.getOnceTimeout()); // /////////////////////// httpMethod.setParams(params); try { if (SC_OK == configHttpClient.executeMethod(httpMethod)) { InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream()); BufferedReader bufferedReader = new BufferedReader(reader); String address = null; List<String> newDomainNameList = new LinkedList<String>(); while ((address = bufferedReader.readLine()) != null) { address = address.trim(); if (StringUtils.isNotBlank(address)) { newDomainNameList.add(address); } } if (newDomainNameList.size() > 0) { log.debug("?"); this.diamondConfigure.setDomainNameList(newDomainNameList); return true; } } else { log.warn("??"); } } catch (HttpException e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } catch (IOException e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } catch (Exception e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } finally { httpMethod.releaseConnection(); } return false; }
From source file:com.mengka.diamond.client.processor.ServerAddressProcessor.java
/** * ?diamond??/*from www .j a v a 2s. co m*/ * * @param acquireCount * ?01? * @return */ private boolean acquireServerAddressOnce(int acquireCount) { HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration(); String configServerAddress; int port; if (null != diamondConfigure.getConfigServerAddress()) { configServerAddress = diamondConfigure.getConfigServerAddress(); port = diamondConfigure.getConfigServerPort(); } else { if (acquireCount == 0) { configServerAddress = Constants.CONFIG_DEFAULT_DOMAINNAME; port = Constants.CONFIG_DEFAULT_PORT; } else { configServerAddress = Constants.CONFIG_DAILY_DOMAINNAME; port = Constants.CONFIG_DEFAULT_PORT; } } hostConfiguration.setHost(configServerAddress, port); String serverAddressUrl = Constants.SERVER_ADDRESS_URI; HttpMethod httpMethod = new GetMethod(serverAddressUrl); // HttpMethod? HttpMethodParams params = new HttpMethodParams(); params.setSoTimeout(diamondConfigure.getOnceTimeout()); // /////////////////////// httpMethod.setParams(params); try { if (SC_OK == configHttpClient.executeMethod(httpMethod)) { InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream()); BufferedReader bufferedReader = new BufferedReader(reader); String address = null; List<String> newDomainNameList = new LinkedList<String>(); while ((address = bufferedReader.readLine()) != null) { address = address.trim(); if (StringUtils.isNotBlank(address) && !address.startsWith("#")) { if (address.contains(":")) { address = address.substring(0, address.indexOf(":")); } if (address.contains("\\")) { address = address.substring(0, address.indexOf("\\")); } if (address.contains("/")) { address = address.substring(0, address.indexOf("/")); } newDomainNameList.add(address); } } if (newDomainNameList.size() > 0) { log.debug("?"); this.diamondConfigure.setDomainNameList(newDomainNameList); return true; } } else { log.error("??, hostsconfig.tbj.com"); } } catch (HttpException e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } catch (IOException e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } catch (Exception e) { log.error(getErrorMessage(configServerAddress) + ", " + e); } finally { httpMethod.releaseConnection(); } return false; }
From source file:it.infn.ct.aleph_portlet.java
public static int getNumRec(String date, int jrec, int num_rec) { HttpClient client = new HttpClient(); HttpMethod method = callAPIOAR(date, jrec, num_rec); double numRec = 0; int numFor = 0; String responseXML = null;/*from w ww . j av a 2 s. com*/ BufferedReader br = null; try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { method.getResponseBody(); responseXML = convertStreamToString(method.getResponseBodyAsStream()); numRec = Double.parseDouble(responseXML.split("Results:")[1].split("-->")[0].replace(" ", "")); System.out.println("NUM REC=>" + numRec / 100); numFor = (int) Math.ceil(numRec / 100); System.out.println("NUM REC=>" + numFor); method.releaseConnection(); } } catch (IOException ex) { Logger.getLogger(aleph_portlet.class.getName()).log(Level.SEVERE, null, ex); } return numFor; }
From source file:ch.lipsch.subsonic4j.internal.SubsonicServiceImpl.java
private Response fetchResponse(String connectionUrl) throws SubsonicException { HttpMethod method = new GetMethod(connectionUrl); Response response = null;//w ww.j a va2 s . c om try { httpClient.executeMethod(method); InputStream responseStream = method.getResponseBodyAsStream(); response = unmarshalResponse(responseStream); } catch (HttpException e) { throw new SubsonicException(SubsonicException.ErrorType.GENERIC, e); } catch (IOException e) { throw new SubsonicException(SubsonicException.ErrorType.GENERIC, e); } catch (JAXBException e) { throw new SubsonicException(SubsonicException.ErrorType.GENERIC, e); } finally { method.releaseConnection(); } SubsonicUtil.throwExceptionIfNecessary(response); return response; }
From source file:ehu.ned.DBpediaSpotlightClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;// w w w .j ava2 s.c o m // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. // // Deal with the response. // // Use caution: ensure correct character encoding and is not binary data InputStream responseBody = method.getResponseBodyAsStream(); response = IOUtils.toString(responseBody, "UTF-8"); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:CertStreamCallback.java
private static void invokeActions(String url, String params, byte[] data, Part[] parts, String accept, String contentType, String sessionID, String language, String method, Callback callback) { if (params != null && !"".equals(params)) { if (url.contains("?")) { url = url + "&" + params; } else {//from w w w . j av a 2s . c om url = url + "?" + params; } } if (language != null && !"".equals(language)) { if (url.contains("?")) { url = url + "&language=" + language; } else { url = url + "?language=" + language; } } HttpMethod httpMethod = null; try { HttpClient httpClient = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true)); httpMethod = getHttpMethod(url, method); if ((httpMethod instanceof PostMethod) || (httpMethod instanceof PutMethod)) { if (null != data) ((EntityEnclosingMethod) httpMethod).setRequestEntity(new ByteArrayRequestEntity(data)); if (httpMethod instanceof PostMethod && null != parts) ((PostMethod) httpMethod) .setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams())); } if (sessionID != null) { httpMethod.addRequestHeader("Cookie", "JSESSIONID=" + sessionID); } if (!(httpMethod instanceof PostMethod && null != parts)) { if (null != accept && !"".equals(accept)) httpMethod.addRequestHeader("Accept", accept); if (null != contentType && !"".equals(contentType)) httpMethod.addRequestHeader("Content-Type", contentType); } int statusCode = httpClient.executeMethod(httpMethod); if (statusCode != HttpStatus.SC_OK) { throw ActionException.create(ClientMessages.CON_ERROR1, httpMethod.getStatusLine()); } contentType = null != httpMethod.getResponseHeader("Content-Type") ? httpMethod.getResponseHeader("Content-Type").getValue() : accept; InputStream o = httpMethod.getResponseBodyAsStream(); if (callback != null) { callback.execute(o, contentType, sessionID); } } catch (Exception e) { String result = BizClientUtils.doError(e, contentType); ByteArrayInputStream in = null; try { in = new ByteArrayInputStream(result.getBytes("UTF-8")); if (callback != null) { callback.execute(in, contentType, null); } } catch (UnsupportedEncodingException e1) { throw new RuntimeException(e1.getMessage() + "", e1); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } finally { // if (httpMethod != null) { httpMethod.releaseConnection(); } } }
From source file:at.ait.dme.yuma.suite.apps.map.server.geo.GeocoderServiceImpl.java
private ArrayList<PlainLiteral> getAlternativePlacenames(String placename) { ArrayList<PlainLiteral> altLabels = new ArrayList<PlainLiteral>(); try {//from w w w . j a v a 2 s .c om HttpClient client = new HttpClient(); HttpMethod get = new GetMethod("http://dbpedia.org/resource/" + URLEncoder.encode(placename, "UTF-8")); get.setRequestHeader("Content-Type", "application/rdf+xml; charset=UTF-8"); get.setRequestHeader("Accept", "application/rdf+xml"); int statusCode = client.executeMethod(get); if (statusCode == HttpStatus.SC_OK) { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder .parse(new InputSource(new InputStreamReader(get.getResponseBodyAsStream(), "UTF-8"))); NodeList labels = doc.getElementsByTagName("rdfs:label"); for (int i = 0; i < labels.getLength(); i++) { altLabels.add(new PlainLiteral(labels.item(i).getTextContent())); } } } catch (Exception e) { // Just ignore for now - doesn't have adverse effect on functionality } return altLabels; }
From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java
private HttpStreamResponse createStreamResponse(HttpMethod method) throws IOException { StatusLine statusLine = method.getStatusLine(); String filename = getFileName(method); long length = getContentLength(method); String contentType = getContentType(method); HttpStreamResponse streamResponse = new HttpStreamResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase(), method.getResponseBodyAsStream(), filename, contentType, length); return streamResponse; }
From source file:ensen.controler.AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//from www . ja va 2 s . c om // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. client.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.out.println("Method failed: " + method.getStatusLine()); } // Read the response body. InputStream responseBodyStream = method.getResponseBodyAsStream(); //Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. int b = responseBodyStream.read(); ArrayList<Integer> bytes = new ArrayList<Integer>(); while (b != -1) { bytes.add(b); b = responseBodyStream.read(); } byte[] responseBody = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); i++) { responseBody[i] = bytes.get(i).byteValue(); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { System.out.println("Fatal protocol violation: " + e.getMessage()); try { System.err.println(method.getURI()); } catch (URIException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { System.out.println("Fatal transport error: " + e.getMessage()); System.out.println(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:ixa.pipe.ned.DBpediaSpotlightClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;/* w w w . j a v a 2 s .c o m*/ // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. // // Deal with the response. // // Use caution: ensure correct character encoding and is not binary data InputStream responseBody = method.getResponseBodyAsStream(); response = StreamUtils.copyToString(responseBody, Charset.forName("UTF-8")); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }