List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:mitm.common.security.ca.handlers.comodo.Tier2PartnerDetails.java
private void handleResponse(int statusCode, HttpMethod httpMethod) throws IOException { if (statusCode != HttpStatus.SC_OK) { throw new IOException("Error Tier2 partner details. Message: " + httpMethod.getStatusLine()); }//from w w w. j ava 2 s .c om InputStream input = httpMethod.getResponseBodyAsStream(); if (input == null) { throw new IOException("Response body is null."); } /* * we want to set a max on the number of bytes to download. We do not want a rogue server to return 1GB. */ InputStream limitInput = new SizeLimitedInputStream(input, MAX_HTTP_RESPONSE_SIZE); String response = IOUtils.toString(limitInput, CharEncoding.US_ASCII); if (logger.isDebugEnabled()) { logger.debug("Response:\r\n" + response); } Map<String, String[]> parameters = NetUtils.parseQuery(response); errorCode = CustomClientStatusCode.fromCode(getValue(parameters, "errorCode")); if (errorCode.getID() < CustomClientStatusCode.SUCCESSFUL.getID()) { error = true; errorMessage = getValue(parameters, "errorMessage"); } else { error = false; verificationLevel = getValue(parameters, "verificationLevel"); accountStatus = getValue(parameters, "accountStatus"); resellerStatus = getValue(parameters, "resellerStatus"); webHostResellerStatus = getValue(parameters, "webHostResellerStatus"); epkiStatus = getValue(parameters, "epkiStatus"); capLiveCCCs = getValue(parameters, "capLiveCCCs"); peakLiveCCCs = getValue(parameters, "peakLiveCCCs"); currentLiveCCCs = getValue(parameters, "currentLiveCCCs"); authorizedDomains = getValue(parameters, "authorizedDomains"); } }
From source file:mitm.common.security.ca.handlers.comodo.CollectCustomClientCert.java
private void handleResponse(int statusCode, HttpMethod httpMethod) throws IOException { if (statusCode != HttpStatus.SC_OK) { throw new IOException("Error Collecting certificate. Message: " + httpMethod.getStatusLine()); }/* ww w.j a v a2 s. c om*/ InputStream input = httpMethod.getResponseBodyAsStream(); if (input == null) { throw new IOException("Response body is null."); } /* * we want to set a max on the number of bytes to download. We do not want a rogue server to return 1GB. */ InputStream limitInput = new SizeLimitedInputStream(input, MAX_HTTP_RESPONSE_SIZE); String response = IOUtils.toString(limitInput, CharEncoding.US_ASCII); if (logger.isDebugEnabled()) { logger.debug("Response:\r\n" + response); } LineNumberReader lineReader = new LineNumberReader(new StringReader(response)); String statusParameter = lineReader.readLine(); errorCode = CustomClientStatusCode.fromCode(statusParameter); if (errorCode.getID() < CustomClientStatusCode.SUCCESSFUL.getID()) { error = true; errorMessage = lineReader.readLine(); } else { error = false; if (errorCode == CustomClientStatusCode.CERTIFICATES_ATTACHED) { /* * The certificate is base64 encoded between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- */ StrBuilder base64 = new StrBuilder(4096); /* * Skip -----BEGIN CERTIFICATE----- */ String line = lineReader.readLine(); if (!"-----BEGIN CERTIFICATE-----".equalsIgnoreCase(line)) { throw new IOException("-----BEGIN CERTIFICATE----- expected but got: " + line); } do { line = lineReader.readLine(); if ("-----END CERTIFICATE-----".equalsIgnoreCase(line)) { break; } if (line != null) { base64.append(line); } } while (line != null); try { byte[] decoded = Base64.decodeBase64(MiscStringUtils.toAsciiBytes(base64.toString())); Collection<X509Certificate> certificates = CertificateUtils .readX509Certificates(new ByteArrayInputStream(decoded)); if (certificates != null && certificates.size() > 0) { certificate = certificates.iterator().next(); } } catch (CertificateException e) { throw new IOException(e); } catch (NoSuchProviderException e) { throw new IOException(e); } } } }
From source file:net.sourceforge.eclipsetrader.yahoo.HistoryFeed.java
public void updateHistory(Security security, int interval) { if (interval == IHistoryFeed.INTERVAL_DAILY) { Calendar from = Calendar.getInstance(); Calendar to = Calendar.getInstance(); History history = security.getHistory(); if (history.size() == 0) from.add(Calendar.YEAR, -CorePlugin.getDefault().getPreferenceStore() .getInt(CorePlugin.PREFS_HISTORICAL_PRICE_RANGE)); else {/* ww w . j a v a 2 s. c om*/ Bar cd = history.getLast(); if (cd != null) { from.setTime(cd.getDate()); from.add(Calendar.DATE, 1); } } try { HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); String host = "ichart.finance.yahoo.com"; //$NON-NLS-1$ BundleContext context = YahooPlugin.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(host, IProxyData.HTTP_PROXY_TYPE); if (data != null) { if (data.getHost() != null) client.getHostConfiguration().setProxy(data.getHost(), data.getPort()); if (data.isRequiresAuthentication()) client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(data.getUserId(), data.getPassword())); } } String symbol = null; if (security.getHistoryFeed() != null) symbol = security.getHistoryFeed().getSymbol(); if (symbol == null || symbol.length() == 0) symbol = security.getCode(); // If the last bar from the data is from a date before today, then // download the historical data, otherwise it's enough to download the data for today. to.add(Calendar.DAY_OF_MONTH, -1); to.set(Calendar.HOUR, 0); to.set(Calendar.MINUTE, 0); to.set(Calendar.SECOND, 0); to.set(Calendar.MILLISECOND, 0); Bar lastHistoryBar = history.getLast(); Date lastDate; if (lastHistoryBar == null) lastDate = from.getTime(); else lastDate = lastHistoryBar.getDate(); if (lastDate.before(to.getTime())) { log.info("Updating historical data for " + security.getCode() + " - " //$NON-NLS-1$//$NON-NLS-2$ + security.getDescription()); StringBuffer url = new StringBuffer("http://" + host + "/table.csv" + "?s="); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ url.append(symbol); url.append("&d=" + to.get(Calendar.MONTH) + "&e=" + to.get(Calendar.DAY_OF_MONTH) + "&f=" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + to.get(Calendar.YEAR)); url.append("&g=d"); //$NON-NLS-1$ url.append("&a=" + from.get(Calendar.MONTH) + "&b=" + from.get(Calendar.DAY_OF_MONTH) + "&c=" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + from.get(Calendar.YEAR)); url.append("&ignore=.csv"); //$NON-NLS-1$ log.debug(url); HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); // The first line is the header, ignoring String inputLine = in.readLine(); log.trace(inputLine); while ((inputLine = in.readLine()) != null) { log.trace(inputLine); if (inputLine.startsWith("<")) //$NON-NLS-1$ continue; String[] item = inputLine.split(","); //$NON-NLS-1$ if (item.length < 6) continue; Calendar day = Calendar.getInstance(); try { day.setTime(df.parse(item[0])); } catch (Exception e) { try { day.setTime(dfAlt.parse(item[0])); } catch (Exception e1) { log.error(e1, e1); } } day.set(Calendar.HOUR, 0); day.set(Calendar.MINUTE, 0); day.set(Calendar.SECOND, 0); day.set(Calendar.MILLISECOND, 0); Bar bar = new Bar(); bar.setDate(day.getTime()); bar.setOpen(Double.parseDouble(item[1].replace(',', '.'))); bar.setHigh(Double.parseDouble(item[2].replace(',', '.'))); bar.setLow(Double.parseDouble(item[3].replace(',', '.'))); bar.setClose(Double.parseDouble(item[4].replace(',', '.'))); bar.setVolume(Long.parseLong(item[5])); // Remove the old bar, if exists int index = history.indexOf(bar.getDate()); if (index != -1) history.remove(index); history.add(bar); } in.close(); } // Get the data for today (to-date) using a different URL at Yahoo! //Bar lastbar = history.getLast log.debug("Get data for today using a separate URL..."); //$NON-NLS-1$ StringBuffer url = new StringBuffer( "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv"); //$NON-NLS-1$ //$NON-NLS-2$ log.debug(url); HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); String inputLine = in.readLine(); log.trace(inputLine); String[] item = inputLine.split(","); //$NON-NLS-1$ item[2] = item[2].replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US); //$NON-NLS-1$ Calendar day = Calendar.getInstance(); // Transfer the data from the string array to the Bar day.setTime(df.parse(item[2])); Bar bar = new Bar(); bar.setDate(day.getTime()); bar.setOpen(Double.parseDouble(item[5])); bar.setClose(Double.parseDouble(item[1])); bar.setHigh(Double.parseDouble(item[6])); bar.setLow(Double.parseDouble(item[7])); bar.setVolume(Long.parseLong(item[8])); // Remove the old bar, if exists int index = history.indexOf(bar.getDate()); if (index != -1) history.remove(index); history.add(bar); in.close(); } catch (Exception e) { log.error(e, e); } CorePlugin.getRepository().save(history); } else log.warn("Intraday data not supported for " + security.getCode() + " - " + security.getDescription()); //$NON-NLS-1$ //$NON-NLS-2$ }
From source file:net.sourceforge.eclipsetrader.yahoo.Feed.java
private void update() { // Builds the url for quotes download String host = "quote.yahoo.com"; //$NON-NLS-1$ StringBuffer url = new StringBuffer("http://" + host + "/download/javasoft.beans?symbols="); //$NON-NLS-1$ //$NON-NLS-2$ for (Iterator iter = map.values().iterator(); iter.hasNext();) url = url.append((String) iter.next() + "+"); //$NON-NLS-1$ if (url.charAt(url.length() - 1) == '+') url.deleteCharAt(url.length() - 1); url.append("&format=sl1d1t1c1ohgvbap"); //$NON-NLS-1$ log.debug(url.toString());//from ww w . j a v a 2s .c o m // Read the last prices String line = ""; //$NON-NLS-1$ try { HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); BundleContext context = YahooPlugin.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(host, IProxyData.HTTP_PROXY_TYPE); if (data != null) { if (data.getHost() != null) client.getHostConfiguration().setProxy(data.getHost(), data.getPort()); if (data.isRequiresAuthentication()) client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(data.getUserId(), data.getPassword())); } } HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((line = in.readLine()) != null) { String[] item = line.split(","); //$NON-NLS-1$ if (line.indexOf(";") != -1) //$NON-NLS-1$ item = line.split(";"); //$NON-NLS-1$ Double open = null, high = null, low = null, close = null; Quote quote = new Quote(); // 2 = Date // 3 = Time try { GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("EST"), Locale.US); //$NON-NLS-1$ usDateTimeParser.setTimeZone(c.getTimeZone()); usDateParser.setTimeZone(c.getTimeZone()); usTimeParser.setTimeZone(c.getTimeZone()); String date = stripQuotes(item[2]); if (date.indexOf("N/A") != -1) //$NON-NLS-1$ date = usDateParser.format(Calendar.getInstance().getTime()); String time = stripQuotes(item[3]); if (time.indexOf("N/A") != -1) //$NON-NLS-1$ time = usTimeParser.format(Calendar.getInstance().getTime()); c.setTime(usDateTimeParser.parse(date + " " + time)); //$NON-NLS-1$ c.setTimeZone(TimeZone.getDefault()); quote.setDate(c.getTime()); } catch (Exception e) { log.error(e.getMessage() + ": " + line); //$NON-NLS-1$ } // 1 = Last price or N/A if (item[1].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ quote.setLast(numberFormat.parse(item[1]).doubleValue()); // 4 = Change // 5 = Open if (item[5].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ open = new Double(numberFormat.parse(item[5]).doubleValue()); // 6 = Maximum if (item[6].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ high = new Double(numberFormat.parse(item[6]).doubleValue()); // 7 = Minimum if (item[7].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ low = new Double(numberFormat.parse(item[7]).doubleValue()); // 8 = Volume if (item[8].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ quote.setVolume(numberFormat.parse(item[8]).intValue()); // 9 = Bid Price if (item[9].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ quote.setBid(numberFormat.parse(item[9]).doubleValue()); // 10 = Ask Price if (item[10].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ quote.setAsk(numberFormat.parse(item[10]).doubleValue()); // 11 = Close Price if (item[11].equalsIgnoreCase("N/A") == false) //$NON-NLS-1$ close = new Double(numberFormat.parse(item[11]).doubleValue()); // 0 = Code String symbol = stripQuotes(item[0]); for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { Security security = (Security) iter.next(); if (symbol.equalsIgnoreCase((String) map.get(security))) { security.setQuote(quote, open, high, low, close); } } } in.close(); } catch (Exception e) { log.error(e); } }
From source file:com.testmax.util.FileDownLoader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }/*from w w w .j a v a 2 s. c o m*/ URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); String file_path = downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""); FileWriter downloadedFile = new FileWriter(file_path, true); try { int status = client.executeMethod(getRequest); WmLog.getCoreLogger().info("HTTP Status {} when getting '{}'" + status + downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.write(bytes); } downloadedFile.close(); in.close(); WmLog.getCoreLogger().info("File downloaded to '{}'" + file_path); } catch (Exception Ex) { WmLog.getCoreLogger().error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return file_path; }
From source file:de.ingrid.portal.scheduler.jobs.UpgradeClientJob.java
private InputStream getFeed(String url) { try {/*from w ww .j av a 2s. c o m*/ HttpClientParams httpClientParams = new HttpClientParams(); HttpConnectionManager httpConnectionManager = new SimpleHttpConnectionManager(); httpClientParams.setSoTimeout(30 * 1000); httpConnectionManager.getParams().setConnectionTimeout(30 * 1000); httpConnectionManager.getParams().setSoTimeout(30 * 1000); HttpClient client = new HttpClient(httpClientParams, httpConnectionManager); HttpMethod method = new GetMethod(url); setCredentialsIfAny(client); int status = client.executeMethod(method); if (status == 200) { log.debug("Successfully received: " + url); return method.getResponseBodyAsStream(); } else { log.error("Response code for '" + url + "' was: " + status); return null; } } catch (HttpException e) { log.error("An HTTP-Exception occured when calling: " + url + " -> " + e.getMessage()); } catch (IOException e) { log.error("An IO-Exception occured when calling: " + url + " -> " + e.getMessage()); } return null; }
From source file:com.cloud.test.stress.StressTestDirectAttach.java
public static Element queryAsyncJobResult(String host, InputStream inputStream) { Element returnBody = null;/*from w w w . j a v a2 s .com*/ Map<String, String> values = getSingleValueFromXML(inputStream, new String[] { "jobid" }); String jobId = values.get("jobid"); if (jobId == null) { s_logger.error("Unable to get a jobId"); return null; } //s_logger.info("Job id is " + jobId); String resultUrl = host + "?command=queryAsyncJobResult&jobid=" + jobId; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(resultUrl); while (true) { try { client.executeMethod(method); //s_logger.info("Method is executed successfully. Following url was sent " + resultUrl); InputStream is = method.getResponseBodyAsStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(is); returnBody = doc.getDocumentElement(); doc.getDocumentElement().normalize(); Element jobStatusTag = (Element) returnBody.getElementsByTagName("jobstatus").item(0); String jobStatus = jobStatusTag.getTextContent(); if (jobStatus.equals("0")) { try { Thread.sleep(1000); } catch (InterruptedException e) { } } else { break; } } catch (Exception ex) { s_logger.error(ex); } } return returnBody; }
From source file:eu.delving.services.TestDataSetCycle.java
@Test public void testSipZipCycle() throws Exception { ClientContext clientContext = new ClientContext(); // import//w w w. ja v a2 s . com Ear importEar = new Ear("Import First Time"); factory.getDataSetStore().importFile(MockInput.sampleFile(), importEar); Assert.assertTrue("import first time", importEar.getResultBoolean()); Facts facts = Facts.read(new FileInputStream(FACTS_FILE)); factory.getDataSetStore().setFacts(facts); // upload DataSetClient client = new DataSetClient(clientContext); Ear uploadFactsEar = new Ear("Upload facts first time"); client.uploadFile(FileType.FACTS, MockFileStoreFactory.SPEC, FACTS_FILE, uploadFactsEar); Assert.assertTrue("upload facts first time", uploadFactsEar.getResultBoolean()); Ear uploadSourceEar = new Ear("Upload source First Time"); client.uploadFile(FileType.SOURCE, MockFileStoreFactory.SPEC, factory.getDataSetStore().getSourceFile(), uploadSourceEar); Assert.assertTrue("upload source first time", uploadSourceEar.getResultBoolean()); client.setListFetchingEnabled(true); /* run it once */ client.setListFetchingEnabled(false); // delete local store factory.getDataSetStore().delete(); // download a new version factory.getFileStore().createDataSetStore(MockFileStoreFactory.SPEC); Thread.sleep(1000); Assert.assertNotNull("data set info missing", clientContext.dataSetInfo); Assert.assertEquals(14141L, (long) clientContext.dataSetInfo.recordCount); clientContext.dataSetInfo = null; HttpMethod method = new GetMethod(String.format("%s/fetch/%s-sip.zip?accessKey=%s", clientContext.getServerUrl(), MockFileStoreFactory.SPEC, clientContext.getAccessKey())); httpClient.executeMethod(method); factory.getDataSetStore().acceptSipZip(new ZipInputStream(method.getResponseBodyAsStream()), new Ear("Unzip")); Assert.assertTrue("Hash is wrong!", Hasher.checkHash(factory.getDataSetStore().getSourceFile())); // upload this new version uploadFactsEar = new Ear("Upload facts Again"); client.uploadFile(FileType.FACTS, MockFileStoreFactory.SPEC, factory.getDataSetStore().getFactsFile(), uploadFactsEar); Assert.assertTrue("upload facts again", uploadFactsEar.getResultBoolean()); uploadSourceEar = new Ear("Upload source Again"); client.uploadFile(FileType.SOURCE, MockFileStoreFactory.SPEC, factory.getDataSetStore().getSourceFile(), uploadSourceEar); Assert.assertFalse("upload source again should have been deemed unnecessary", uploadSourceEar.getResultBoolean()); }
From source file:com.zimbra.qa.unittest.TestZimbraHttpConnectionManager.java
public static void dumpResponse(int respCode, HttpMethod method, String prefix) throws IOException { prefix = prefix + " - "; // status/*from w w w .j a v a 2s . co m*/ int statusCode = method.getStatusCode(); String statusLine = method.getStatusLine().toString(); System.out.println(prefix + "respCode=" + respCode); System.out.println(prefix + "statusCode=" + statusCode); System.out.println(prefix + "statusLine=" + statusLine); // headers System.out.println(prefix + "Headers"); Header[] respHeaders = method.getResponseHeaders(); for (int i = 0; i < respHeaders.length; i++) { String header = respHeaders[i].toString(); // trim the CRLF at the end to save space System.out.println(prefix + header.trim()); } // body byte[] bytes = ByteUtil.getContent(method.getResponseBodyAsStream(), 0); System.out.println(prefix + bytes.length + " bytes read"); System.out.println(new String(bytes)); }
From source file:com.mrfeinberg.translation.AbstractTranslationService.java
public Runnable translate(final String phrase, final LanguagePair lp, final TranslationListener listener) { final Language b = lp.b(); final HttpClient httpClient = new HttpClient(); if (proxyPrefs.getUseProxy()) { httpClient.getHostConfiguration().setProxy(proxyPrefs.getProxyHost(), proxyPrefs.getProxyPort()); }//from w ww . j av a 2 s . c om final HttpMethod httpMethod = getHttpMethod(phrase, lp); final Callable<String> callable = new Callable<String>() { public String call() throws Exception { int result = httpClient.executeMethod(httpMethod); if (result != 200) { throw new Exception("Got " + result + " status for " + httpMethod.getURI()); } final BufferedReader in = new BufferedReader( new InputStreamReader(httpMethod.getResponseBodyAsStream(), "utf8")); try { final StringBuilder sb = new StringBuilder(); String line; while ((line = in.readLine()) != null) sb.append(line); return sb.toString(); } finally { in.close(); httpMethod.releaseConnection(); } } }; final FutureTask<String> tc = new FutureTask<String>(callable); return new Runnable() { public void run() { try { executor.execute(tc); final String result = tc.get(timeout, TimeUnit.MILLISECONDS); String found = findTranslatedText(result); if (found == null) { listener.error("Cannot find translated text in result."); } else { found = found.replaceAll("\\s+", " "); listener.result(found, b); } } catch (final TimeoutException e) { listener.timedOut(); } catch (final InterruptedException e) { listener.cancelled(); } catch (final Exception e) { e.printStackTrace(); listener.error(e.toString()); } } }; }