List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:org.eclipse.smarthome.io.net.http.HttpUtil.java
/** * Executes the given <code>url</code> with the given <code>httpMethod</code> * /*from www . j a va 2 s.c om*/ * @param httpMethod the HTTP method to use * @param url the url to execute (in milliseconds) * @param httpHeaders optional HTTP headers which has to be set on request * @param content the content to be send to the given <code>url</code> or * <code>null</code> if no content should be send. * @param contentType the content type of the given <code>content</code> * @param timeout the socket timeout to wait for data * @param proxyHost the hostname of the proxy * @param proxyPort the port of the proxy * @param proxyUser the username to authenticate with the proxy * @param proxyPassword the password to authenticate with the proxy * @param nonProxyHosts the hosts that won't be routed through the proxy * @return the response body or <code>NULL</code> when the request went wrong */ public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword, String nonProxyHosts) { HttpClient client = new HttpClient(); // only configure a proxy if a host is provided if (StringUtils.isNotBlank(proxyHost) && proxyPort != null && shouldUseProxy(url, nonProxyHosts)) { client.getHostConfiguration().setProxy(proxyHost, proxyPort); if (StringUtils.isNotBlank(proxyUser)) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword)); } } HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url); method.getParams().setSoTimeout(timeout); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); if (httpHeaders != null) { for (String httpHeaderKey : httpHeaders.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey))); } } // add content if a valid method is given ... if (method instanceof EntityEnclosingMethod && content != null) { EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method; eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType)); } Credentials credentials = extractCredentials(url); if (credentials != null) { client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, credentials); } if (logger.isDebugEnabled()) { try { logger.debug("About to execute '" + method.getURI().toString() + "'"); } catch (URIException e) { logger.debug(e.getMessage()); } } try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: " + method.getStatusLine()); } String responseBody = IOUtils.toString(method.getResponseBodyAsStream()); if (!responseBody.isEmpty()) { logger.debug(responseBody); } return responseBody; } catch (HttpException he) { logger.error("Fatal protocol violation: {}", he.toString()); } catch (IOException ioe) { logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection(); } return null; }
From source file:org.eclipsetrader.borsaitalia.internal.core.BackfillConnector.java
@Override public IOHLC[] backfillHistory(IFeedIdentifier identifier, Date from, Date to, TimeSpan timeSpan) { String code = identifier.getSymbol(); String isin = null;/*from w w w . ja v a2s . co m*/ IFeedProperties properties = (IFeedProperties) identifier.getAdapter(IFeedProperties.class); if (properties != null) { if (properties.getProperty(Activator.PROP_ISIN) != null) { isin = properties.getProperty(Activator.PROP_ISIN); } if (properties.getProperty(Activator.PROP_CODE) != null) { code = properties.getProperty(Activator.PROP_CODE); } } if (code == null || isin == null) { return null; } String period = String.valueOf(timeSpan.getLength()) + (timeSpan.getUnits() == Units.Minutes ? "MIN" : "DAY"); //$NON-NLS-1$ //$NON-NLS-2$ List<OHLC> list = new ArrayList<OHLC>(); try { HttpMethod method = new GetMethod("http://" + host + "/scripts/cligipsw.dll"); //$NON-NLS-1$ //$NON-NLS-2$ method.setQueryString(new NameValuePair[] { new NameValuePair("app", "tic_d"), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("action", "dwnld4push"), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("cod", code), //$NON-NLS-1$ new NameValuePair("codneb", isin), //$NON-NLS-1$ new NameValuePair("req_type", "GRAF_DS"), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("ascii", "1"), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("form_id", ""), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("period", period), //$NON-NLS-1$ new NameValuePair("From", new SimpleDateFormat("yyyyMMdd000000").format(from)), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("To", new SimpleDateFormat("yyyyMMdd000000").format(to)), //$NON-NLS-1$ //$NON-NLS-2$ }); method.setFollowRedirects(true); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); String inputLine = in.readLine(); if (inputLine.startsWith("@")) { //$NON-NLS-1$ while ((inputLine = in.readLine()) != null) { if (inputLine.startsWith("@") || inputLine.length() == 0) { //$NON-NLS-1$ continue; } try { String[] item = inputLine.split("\\|"); //$NON-NLS-1$ OHLC bar = new OHLC(df.parse(item[0]), nf.parse(item[1]).doubleValue(), nf.parse(item[2]).doubleValue(), nf.parse(item[3]).doubleValue(), nf.parse(item[4]).doubleValue(), nf.parse(item[5]).longValue()); list.add(bar); } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error parsing data: " + inputLine, e); //$NON-NLS-1$ Activator.getDefault().getLog().log(status); } } } else { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, NLS.bind("Unexpected response from {0}: {1}", new Object[] { //$NON-NLS-1$ method.getURI().toString(), inputLine }), null); Activator.getDefault().getLog().log(status); } in.close(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading data", e); //$NON-NLS-1$ Activator.getDefault().getLog().log(status); } return list.toArray(new IOHLC[list.size()]); }
From source file:org.eclipsetrader.directa.internal.core.connector.StreamingConnector.java
protected void fetchLatestBookSnapshot(String[] sTit) { Hashtable<String, String[]> hashtable = new Hashtable<String, String[]>(); try {/*from w w w . ja va2 s . co m*/ HttpMethod method = createMethod(sTit, "t", streamingServer, WebConnector.getInstance().getUrt(), //$NON-NLS-1$ WebConnector.getInstance().getPrt()); method.setFollowRedirects(true); HttpClient client = new HttpClient(); setupProxy(client, streamingServer); client.executeMethod(method); BufferedReader bufferedreader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream())); String s5; while ((s5 = bufferedreader.readLine()) != null) { String[] campo = s5.split("\\;"); //$NON-NLS-1$ if (campo.length != 0) { hashtable.put(campo[0], campo); } } } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e); //$NON-NLS-1$ Activator.log(status); } for (String symbol : sTit) { String sVal[] = hashtable.get(symbol); if (sVal == null) { continue; } FeedSubscription subscription = symbolSubscriptions.get(symbol); if (subscription == null) { continue; } try { Object oldValue = subscription.getBook(); IBookEntry[] bidEntry = new IBookEntry[20]; IBookEntry[] askEntry = new IBookEntry[20]; for (int x = 0, k = 9; x < 20; x++, k += 6) { bidEntry[x] = new BookEntry(null, Double.parseDouble(sVal[k + 2]), Long.parseLong(sVal[k + 1]), Long.parseLong(sVal[k]), null); askEntry[x] = new BookEntry(null, Double.parseDouble(sVal[k + 5]), Long.parseLong(sVal[k + 4]), Long.parseLong(sVal[k + 3]), null); } Object newValue = new org.eclipsetrader.core.feed.Book(bidEntry, askEntry); subscription.setBook((IBook) newValue); subscription.addDelta(new QuoteDelta(subscription.getIdentifier(), oldValue, newValue)); } catch (Exception e) { Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e)); //$NON-NLS-1$ } } wakeupNotifyThread(); }
From source file:org.eclipsetrader.directa.internal.core.connector.StreamingConnector.java
protected void fetchLatestSnapshot(String[] sTit) { int flag[] = new int[sTit.length]; for (int i = 0; i < flag.length; i++) { flag[i] = 1;//from www . j a v a 2s . c o m } try { String s = "[!QUOT]"; //$NON-NLS-1$ byte byte0 = 43; Hashtable<String, Map<String, String>> hashTable = new Hashtable<String, Map<String, String>>(); try { HttpMethod method = createSnapshotMethod(sTit, INFO, streamingServer, WebConnector.getInstance().getUrt(), WebConnector.getInstance().getPrt()); method.setFollowRedirects(true); logger.debug(method.getURI().toString()); HttpClient client = new HttpClient(); setupProxy(client, streamingServer); client.executeMethod(method); BufferedReader bufferedreader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream())); String s5; while ((s5 = bufferedreader.readLine()) != null && !s5.startsWith(s)) { logger.debug(s5); } if (s5 != null) { do { logger.debug(s5); if (s5.startsWith(s)) { Map<String, String> as = new HashMap<String, String>(); StringTokenizer stringtokenizer = new StringTokenizer(s5, ",\t"); //$NON-NLS-1$ String s2 = stringtokenizer.nextToken(); s2 = s2.substring(s2.indexOf(s) + s.length()).trim(); for (int j = 0; j < byte0; j++) { String s4; try { s4 = stringtokenizer.nextToken().trim(); int sq = s4.indexOf("]"); as.put(s4.substring(0, sq + 1), s4.substring(sq + 1)); } catch (NoSuchElementException nosuchelementexception) { hashTable.put(s2, as); break; } } hashTable.put(s2, as); } } while ((s5 = bufferedreader.readLine()) != null); } } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e); //$NON-NLS-1$ Activator.log(status); } processSnapshotData(sTit, hashTable); wakeupNotifyThread(); } catch (Exception e) { Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e)); //$NON-NLS-1$ } }
From source file:org.eclipsetrader.directaworld.internal.core.connector.SnapshotConnector.java
protected void fetchLatestSnapshot() { BufferedReader in = null;/* w ww .jav a 2 s . co m*/ try { String[] symbols; synchronized (symbolSubscriptions) { symbols = symbolSubscriptions.keySet().toArray(new String[symbolSubscriptions.size()]); } StringBuilder url = new StringBuilder("http://" + HOST + "/cgi-bin/qta?idx=alfa&modo=t&appear=n"); //$NON-NLS-1$ //$NON-NLS-2$ int x = 0; for (; x < symbols.length; x++) { url.append("&id" + (x + 1) + "=" + symbols[x]); //$NON-NLS-1$ //$NON-NLS-2$ } for (; x < 30; x++) { url.append("&id" + (x + 1) + "="); //$NON-NLS-1$ //$NON-NLS-2$ } url.append("&u=" + userName + "&p=" + password); //$NON-NLS-1$ //$NON-NLS-2$ HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); logger.debug(method.getURI().toString()); client.executeMethod(method); requiredDelay = 15; String inputLine; in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((inputLine = in.readLine()) != null) { logger.debug(inputLine); if (inputLine.indexOf("<!--QT START HERE-->") != -1) { //$NON-NLS-1$ while ((inputLine = in.readLine()) != null) { logger.debug(inputLine); if (inputLine.indexOf("<!--QT STOP HERE-->") != -1) { //$NON-NLS-1$ break; } try { parseLine(inputLine); } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error parsing line: " + inputLine, e); //$NON-NLS-1$ Activator.log(status); } } } else if (inputLine.indexOf("Sara' possibile ricaricare la pagina tra") != -1) { //$NON-NLS-1$ int beginIndex = inputLine.indexOf("tra ") + 4; //$NON-NLS-1$ int endIndex = inputLine.indexOf("sec") - 1; //$NON-NLS-1$ try { requiredDelay = Integer.parseInt(inputLine.substring(beginIndex, endIndex)) + 1; } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error parsing required delay", e); //$NON-NLS-1$ Activator.log(status); } } } in.close(); FeedSubscription[] subscriptions; synchronized (symbolSubscriptions) { Collection<FeedSubscription> c = symbolSubscriptions.values(); subscriptions = c.toArray(new FeedSubscription[c.size()]); } for (int i = 0; i < subscriptions.length; i++) { subscriptions[i].fireNotification(); } } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading data", e); //$NON-NLS-1$ Activator.log(status); } finally { try { if (in != null) { in.close(); } } catch (Exception e) { // We can't do anything at this time, ignore } } }
From source file:org.eclipsetrader.directaworld.internal.core.connector.SnapshotConnector.java
protected boolean checkLogin() { boolean result = false; BufferedReader in = null;//from ww w . j a va 2 s .co m try { StringBuilder url = new StringBuilder("http://" + HOST + "/cgi-bin/qta?idx=alfa&modo=t&appear=n"); //$NON-NLS-1$ //$NON-NLS-2$ int x = 0; for (; x < 30; x++) { url.append("&id" + (x + 1) + "="); //$NON-NLS-1$ //$NON-NLS-2$ } url.append("&u=" + userName + "&p=" + password); //$NON-NLS-1$ //$NON-NLS-2$ HttpMethod method = new GetMethod(url.toString()); method.setFollowRedirects(true); client.executeMethod(method); requiredDelay = 15; String inputLine; in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((inputLine = in.readLine()) != null) { if (inputLine.indexOf("<!--QT START HERE-->") != -1) { //$NON-NLS-1$ result = true; } } in.close(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading data", e); //$NON-NLS-1$ Activator.log(status); } finally { try { if (in != null) { in.close(); } } catch (Exception e) { // We can't do anything at this time, ignore } } return result; }
From source file:org.eclipsetrader.kdb.internal.core.connector.BackfillConnector.java
private IOHLC[] backfillDailyHistory(IFeedIdentifier identifier, Date from, Date to) { List<OHLC> list = new ArrayList<OHLC>(); Calendar c = Calendar.getInstance(); c.setTime(from);/*from w w w.ja v a2 s. c o m*/ int firstYear = c.get(Calendar.YEAR); c.setTime(to); int lastYear = c.get(Calendar.YEAR); firstYear = lastYear - 2; HttpClient client = new HttpClient(); for (int ys = lastYear; ys > firstYear; ys--) { try { HttpMethod method = Util.get1YearHistoryFeedMethod(identifier, ys); Util.setupProxy(client, method.getURI().getHost()); System.out.println(method.getURI()); client.getHttpConnectionManager().closeIdleConnections(100); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); readDailyBackfillStream(list, in); in.close(); method.releaseConnection(); Thread.sleep(3000); } catch (Exception e) { Status status = new Status(IStatus.ERROR, KdbActivator.PLUGIN_ID, 0, "Error reading data", e); KdbActivator.log(status); } } Collections.sort(list, new Comparator<OHLC>() { @Override public int compare(OHLC o1, OHLC o2) { return o1.getDate().compareTo(o2.getDate()); } }); for (Iterator<OHLC> iter = list.iterator(); iter.hasNext();) { OHLC ohlc = iter.next(); if (ohlc.getDate().before(from) || ohlc.getDate().after(to)) { iter.remove(); } } try { Thread.sleep(1000); } catch (Exception e) { } return list.toArray(new IOHLC[list.size()]); }
From source file:org.eclipsetrader.kdb.internal.core.connector.BackfillConnector.java
private IOHLC[] backfill1DayHistory(IFeedIdentifier identifier) throws Exception { List<OHLC> list = new ArrayList<OHLC>(); HttpMethod method = Util.get1DayHistoryFeedMethod(identifier); method.setFollowRedirects(true);//from w w w . ja v a 2 s . co m HttpClient client = new HttpClient(); Util.setupProxy(client, method.getURI().getHost()); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); read1DayBackfillStream(list, in); in.close(); method.releaseConnection(); return list.toArray(new IOHLC[list.size()]); }
From source file:org.eclipsetrader.kdb.internal.core.connector.SnapshotConnector.java
protected void fetchLatestSnapshot(HttpClient client, String[] symbols, boolean isStaleUpdate) { HttpMethod method = null; BufferedReader in = null;/*from www . ja va 2 s . c om*/ String line = ""; //$NON-NLS-1$ try { method = Util.getSnapshotFeedMethod(symbols); client.executeMethod(method); in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((line = in.readLine()) != null) { processSnapshotData(line, isStaleUpdate); } FeedSubscription[] subscriptions; synchronized (symbolSubscriptions) { Collection<FeedSubscription> c = symbolSubscriptions.values(); subscriptions = c.toArray(new FeedSubscription[c.size()]); } for (int i = 0; i < subscriptions.length; i++) { subscriptions[i].fireNotification(); } } catch (Exception e) { Status status = new Status(IStatus.ERROR, KdbActivator.PLUGIN_ID, 0, "Error reading data", e); KdbActivator.log(status); } finally { try { if (in != null) { in.close(); } if (method != null) { method.releaseConnection(); } } catch (Exception e) { Status status = new Status(IStatus.WARNING, KdbActivator.PLUGIN_ID, 0, "Connection wasn't closed cleanly", e); KdbActivator.log(status); } } }
From source file:org.eclipsetrader.yahoo.internal.core.connector.BackfillConnector.java
private IOHLC[] backfillDailyHistory(IFeedIdentifier identifier, Date from, Date to) { List<OHLC> list = new ArrayList<OHLC>(); Calendar c = Calendar.getInstance(); c.setTime(from);//from ww w . jav a 2 s . co m int firstYear = c.get(Calendar.YEAR); c.setTime(to); int lastYear = c.get(Calendar.YEAR); HttpClient client = new HttpClient(); for (int ys = firstYear; ys <= lastYear; ys++) { try { HttpMethod method = Util.get1YearHistoryFeedMethod(identifier, ys); Util.setupProxy(client, method.getURI().getHost()); client.executeMethod(method); BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); readDailyBackfillStream(list, in); in.close(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error reading data", e); YahooActivator.log(status); } } Collections.sort(list, new Comparator<OHLC>() { @Override public int compare(OHLC o1, OHLC o2) { return o1.getDate().compareTo(o2.getDate()); } }); for (Iterator<OHLC> iter = list.iterator(); iter.hasNext();) { OHLC ohlc = iter.next(); if (ohlc.getDate().before(from) || ohlc.getDate().after(to)) { iter.remove(); } } return list.toArray(new IOHLC[list.size()]); }