List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
From source file:org.eclipsetrader.kdb.internal.core.Util.java
public static void setupProxy(HttpClient client, String host) throws URISyntaxException { if (KdbActivator.getDefault() == null) { return;//from w w w.ja va 2 s.co m } BundleContext context = KdbActivator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxyService = (IProxyService) context.getService(reference); IProxyData[] proxyData = proxyService .select(new java.net.URI(IProxyData.HTTP_PROXY_TYPE, "//" + host, null)); if (proxyData != null && proxyData.length != 0) { if (proxyData[0].getHost() != null) { client.getHostConfiguration().setProxy(proxyData[0].getHost(), proxyData[0].getPort()); } if (proxyData[0].isRequiresAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyData[0].getUserId(), proxyData[0].getPassword())); } } context.ungetService(reference); } }
From source file:org.eclipsetrader.news.internal.connectors.RSSNewsProvider.java
private HeadLine[] update(URL feedUrl, String source) { Map<String, SyndEntry> titles = new HashMap<String, SyndEntry>(); Map<SyndEntry, Set<ISecurity>> entries = new HashMap<SyndEntry, Set<ISecurity>>(); Set<HeadLine> headLines = new HashSet<HeadLine>(); try {/*w ww . j a v a 2 s .c o m*/ HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); if (Activator.getDefault() != null) { BundleContext context = Activator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(feedUrl.getHost(), 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())); } } context.ungetService(reference); } } SyndFeed feed = fetcher.retrieveFeed(feedUrl, client); for (Iterator<?> iter = feed.getEntries().iterator(); iter.hasNext();) { SyndEntry entry = (SyndEntry) iter.next(); if (titles.containsKey(entry.getTitle())) { entry = titles.get(entry.getTitle()); } else { titles.put(entry.getTitle(), entry); } Set<ISecurity> set = entries.get(entry); if (set == null) { set = new HashSet<ISecurity>(); entries.put(entry, set); } } for (SyndEntry entry : entries.keySet()) { Set<ISecurity> set = entries.get(entry); String url = entry.getLink(); if (url.indexOf('*') != -1) { url = url.substring(url.indexOf('*') + 1); } String title = entry.getTitle(); if (title.endsWith(")")) { int s = title.lastIndexOf('('); if (s != -1) { source = title.substring(s + 1, title.length() - 1); if (source.startsWith("at ")) { source = source.substring(3); } title = title.substring(0, s - 1).trim(); } } HeadLine headLine = new HeadLine(entry.getPublishedDate(), source, title, set.toArray(new ISecurity[set.size()]), url); headLines.add(headLine); } } catch (Exception e) { Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 0, "Error reading RSS subscription " + feedUrl.toString(), e); //$NON-NLS-1$ Activator.getDefault().getLog().log(status); } return headLines.toArray(new HeadLine[headLines.size()]); }
From source file:org.eclipsetrader.yahoo.internal.core.Util.java
public static void setupProxy(HttpClient client, String host) throws URISyntaxException { if (YahooActivator.getDefault() == null) { return;//from w ww .j av a 2 s .c o m } BundleContext context = YahooActivator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxyService = (IProxyService) context.getService(reference); IProxyData[] proxyData = proxyService .select(new java.net.URI(IProxyData.HTTP_PROXY_TYPE, "//" + host, null)); if (proxyData != null && proxyData.length != 0) { if (proxyData[0].getHost() != null) { client.getHostConfiguration().setProxy(proxyData[0].getHost(), proxyData[0].getPort()); } if (proxyData[0].isRequiresAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyData[0].getUserId(), proxyData[0].getPassword())); } } context.ungetService(reference); } }
From source file:org.eclipsetrader.yahoo.internal.news.NewsProvider.java
protected IStatus jobRunner(IProgressMonitor monitor) { IPreferenceStore store = YahooActivator.getDefault().getPreferenceStore(); Date limitDate = getLimitDate(); Calendar today = Calendar.getInstance(); int hoursAsRecent = YahooActivator.getDefault().getPreferenceStore() .getInt(YahooActivator.PREFS_HOURS_AS_RECENT); today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent); Date recentLimitDate = today.getTime(); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); try {// w w w.j a v a2s. c o m JAXBContext jaxbContext = JAXBContext.newInstance(Category[].class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); InputStream inputStream = FileLocator.openStream(YahooActivator.getDefault().getBundle(), new Path("data/news_feeds.xml"), false); JAXBElement<Category[]> element = unmarshaller.unmarshal(new StreamSource(inputStream), Category[].class); int total = 0; for (Category category : element.getValue()) { for (Page page : category.getPages()) { if (store.getBoolean(YahooActivator.PREFS_SUBSCRIBE_PREFIX + page.getId())) { total++; } } } ISecurity[] securities = getRepositoryService().getSecurities(); total += securities.length; monitor.beginTask("Fetching Yahoo! News", total); resetRecentFlag(); final Set<HeadLine> added = new HashSet<HeadLine>(); final Set<HeadLine> updated = new HashSet<HeadLine>(); Category[] category = element.getValue(); for (int i = 0; i < category.length && !monitor.isCanceled(); i++) { INewsHandler handler = new RSSNewsHandler(); if (category[i].getHandler() != null) { try { handler = (INewsHandler) Class.forName(category[i].getHandler()).newInstance(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Invalid news handler class", e); YahooActivator.log(status); } } List<URL> l = new ArrayList<URL>(); Page[] page = category[i].getPages(); for (int ii = 0; ii < page.length && !monitor.isCanceled(); ii++) { if (store.getBoolean(YahooActivator.PREFS_SUBSCRIBE_PREFIX + page[ii].getId())) { l.add(new URL(page[ii].getUrl())); } } HeadLine[] list = handler.parseNewsPages(l.toArray(new URL[l.size()]), monitor); for (HeadLine headLine : list) { if (headLine.getDate() == null || headLine.getDate().before(limitDate)) { continue; } if (!oldItems.contains(headLine)) { if (!headLine.getDate().before(recentLimitDate)) { headLine.setRecent(true); } oldItems.add(headLine); added.add(headLine); } } } if (store.getBoolean(YahooActivator.PREFS_UPDATE_SECURITIES_NEWS)) { for (int i = 0; i < securities.length && !monitor.isCanceled(); i++) { URL feedUrl = Util.getRSSNewsFeedForSecurity(securities[i]); if (feedUrl == null) { continue; } monitor.subTask(feedUrl.toString()); try { if (YahooActivator.getDefault() != null) { BundleContext context = YahooActivator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(feedUrl.getHost(), 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())); } } context.ungetService(reference); } } SyndFeed feed = fetcher.retrieveFeed(feedUrl, client); for (Iterator<?> iter = feed.getEntries().iterator(); iter.hasNext();) { SyndEntry entry = (SyndEntry) iter.next(); String link = entry.getLink(); if (link == null && entry.getLinks().size() != 0) { link = (String) entry.getLinks().get(0); } if (link != null) { while (link.indexOf('*') != -1) { link = link.substring(link.indexOf('*') + 1); } link = URLDecoder.decode(link, "UTF-8"); } if (link == null) { continue; } String source = null; String title = entry.getTitle(); if (title.startsWith("[$$]")) { title = title.substring(4, title.length()); } if (title.endsWith(")")) { int s = title.lastIndexOf('('); if (s != -1) { source = title.substring(s + 1, title.length() - 1); if (source.startsWith("at ")) { source = source.substring(3); } title = title.substring(0, s - 1).trim(); } } HeadLine headLine = new HeadLine(entry.getPublishedDate(), source, title, new ISecurity[] { securities[i] }, link); int index = oldItems.indexOf(headLine); if (index != -1) { if (headLine.getDate().before(limitDate)) { continue; } headLine = oldItems.get(index); if (!headLine.contains(securities[i])) { headLine.addMember(securities[i]); updated.add(headLine); } } else { if (!headLine.getDate().before(recentLimitDate)) { headLine.setRecent(true); } oldItems.add(headLine); added.add(headLine); } } } catch (Exception e) { String msg = "Error fetching news from " + feedUrl.toString(); Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, msg, e); YahooActivator.log(status); } monitor.worked(1); } } if (added.size() != 0 || updated.size() != 0) { final INewsService service = getNewsService(); service.runInService(new INewsServiceRunnable() { @Override public IStatus run(IProgressMonitor monitor) throws Exception { service.addHeadLines(added.toArray(new IHeadLine[added.size()])); service.updateHeadLines(updated.toArray(new IHeadLine[updated.size()])); return Status.OK_STATUS; } }, null); } save(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooActivator.PLUGIN_ID, 0, "Error fetching news", e); YahooActivator.log(status); } finally { monitor.done(); } return Status.OK_STATUS; }
From source file:org.eclipsetrader.yahoojapan.internal.core.Util.java
public static void setupProxy(HttpClient client, String host) throws URISyntaxException { if (YahooJapanActivator.getDefault() == null) { return;// www . ja va 2s .c o m } BundleContext context = YahooJapanActivator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxyService = (IProxyService) context.getService(reference); IProxyData[] proxyData = proxyService .select(new java.net.URI(IProxyData.HTTP_PROXY_TYPE, "//" + host, null)); if (proxyData != null && proxyData.length != 0) { if (proxyData[0].getHost() != null) { client.getHostConfiguration().setProxy(proxyData[0].getHost(), proxyData[0].getPort()); } if (proxyData[0].isRequiresAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyData[0].getUserId(), proxyData[0].getPassword())); } } context.ungetService(reference); } }
From source file:org.eclipsetrader.yahoojapan.internal.news.NewsProvider.java
protected IStatus jobRunner(IProgressMonitor monitor) { IPreferenceStore store = YahooJapanActivator.getDefault().getPreferenceStore(); Date limitDate = getLimitDate(); Calendar today = Calendar.getInstance(); int hoursAsRecent = YahooJapanActivator.getDefault().getPreferenceStore() .getInt(YahooJapanActivator.PREFS_HOURS_AS_RECENT); today.add(Calendar.HOUR_OF_DAY, -hoursAsRecent); Date recentLimitDate = today.getTime(); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); try {/* w w w . j av a 2 s . c o m*/ JAXBContext jaxbContext = JAXBContext.newInstance(Category[].class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); InputStream inputStream = FileLocator.openStream(YahooJapanActivator.getDefault().getBundle(), new Path("data/news_feeds.xml"), false); JAXBElement<Category[]> element = unmarshaller.unmarshal(new StreamSource(inputStream), Category[].class); int total = 0; for (Category category : element.getValue()) { for (Page page : category.getPages()) { if (store.getBoolean(YahooJapanActivator.PREFS_SUBSCRIBE_PREFIX + page.getId())) { total++; } } } ISecurity[] securities = getRepositoryService().getSecurities(); total += securities.length; monitor.beginTask("Fetching Yahoo! News", total); resetRecentFlag(); final Set<HeadLine> added = new HashSet<HeadLine>(); final Set<HeadLine> updated = new HashSet<HeadLine>(); Category[] category = element.getValue(); for (int i = 0; i < category.length && !monitor.isCanceled(); i++) { INewsHandler handler = new RSSNewsHandler(); if (category[i].getHandler() != null) { try { handler = (INewsHandler) Class.forName(category[i].getHandler()).newInstance(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Invalid news handler class", e); YahooJapanActivator.log(status); } } List<URL> l = new ArrayList<URL>(); Page[] page = category[i].getPages(); for (int ii = 0; ii < page.length && !monitor.isCanceled(); ii++) { if (store.getBoolean(YahooJapanActivator.PREFS_SUBSCRIBE_PREFIX + page[ii].getId())) { l.add(new URL(page[ii].getUrl())); } } HeadLine[] list = handler.parseNewsPages(l.toArray(new URL[l.size()]), monitor); for (HeadLine headLine : list) { if (headLine.getDate() == null || headLine.getDate().before(limitDate)) { continue; } if (!oldItems.contains(headLine)) { if (!headLine.getDate().before(recentLimitDate)) { headLine.setRecent(true); } oldItems.add(headLine); added.add(headLine); } } } if (store.getBoolean(YahooJapanActivator.PREFS_UPDATE_SECURITIES_NEWS)) { for (int i = 0; i < securities.length && !monitor.isCanceled(); i++) { URL feedUrl = Util.getRSSNewsFeedForSecurity(securities[i]); if (feedUrl == null) { continue; } monitor.subTask(feedUrl.toString()); try { if (YahooJapanActivator.getDefault() != null) { BundleContext context = YahooJapanActivator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxy = (IProxyService) context.getService(reference); IProxyData data = proxy.getProxyDataForHost(feedUrl.getHost(), 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())); } } context.ungetService(reference); } } SyndFeed feed = fetcher.retrieveFeed(feedUrl, client); for (Iterator<?> iter = feed.getEntries().iterator(); iter.hasNext();) { SyndEntry entry = (SyndEntry) iter.next(); String link = entry.getLink(); if (link == null && entry.getLinks().size() != 0) { link = (String) entry.getLinks().get(0); } if (link != null) { while (link.indexOf('*') != -1) { link = link.substring(link.indexOf('*') + 1); } link = URLDecoder.decode(link, "UTF-8"); } if (link == null) { continue; } String source = null; String title = entry.getTitle(); if (title.startsWith("[$$]")) { title = title.substring(4, title.length()); } if (title.endsWith(")")) { int s = title.lastIndexOf('('); if (s != -1) { source = title.substring(s + 1, title.length() - 1); if (source.startsWith("at ")) { source = source.substring(3); } title = title.substring(0, s - 1).trim(); } } HeadLine headLine = new HeadLine(entry.getPublishedDate(), source, title, new ISecurity[] { securities[i] }, link); int index = oldItems.indexOf(headLine); if (index != -1) { if (headLine.getDate().before(limitDate)) { continue; } headLine = oldItems.get(index); if (!headLine.contains(securities[i])) { headLine.addMember(securities[i]); updated.add(headLine); } } else { if (!headLine.getDate().before(recentLimitDate)) { headLine.setRecent(true); } oldItems.add(headLine); added.add(headLine); } } } catch (Exception e) { String msg = "Error fetching news from " + feedUrl.toString(); Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, msg, e); YahooJapanActivator.log(status); } monitor.worked(1); } } if (added.size() != 0 || updated.size() != 0) { final INewsService service = getNewsService(); service.runInService(new INewsServiceRunnable() { @Override public IStatus run(IProgressMonitor monitor) throws Exception { service.addHeadLines(added.toArray(new IHeadLine[added.size()])); service.updateHeadLines(updated.toArray(new IHeadLine[updated.size()])); return Status.OK_STATUS; } }, null); } save(); } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error fetching news", e); YahooJapanActivator.log(status); } finally { monitor.done(); } return Status.OK_STATUS; }
From source file:org.eclipsetrader.yahoojapanfx.internal.core.Util.java
public static void setupProxy(HttpClient client, String host) throws URISyntaxException { if (Activator.getDefault() == null) { return;/*from www .j a v a2s . com*/ } BundleContext context = Activator.getDefault().getBundle().getBundleContext(); ServiceReference reference = context.getServiceReference(IProxyService.class.getName()); if (reference != null) { IProxyService proxyService = (IProxyService) context.getService(reference); IProxyData[] proxyData = proxyService .select(new java.net.URI(IProxyData.HTTP_PROXY_TYPE, "//" + host, null)); if (proxyData != null && proxyData.length != 0) { if (proxyData[0].getHost() != null) { client.getHostConfiguration().setProxy(proxyData[0].getHost(), proxyData[0].getPort()); } if (proxyData[0].isRequiresAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyData[0].getUserId(), proxyData[0].getPassword())); } } context.ungetService(reference); } }
From source file:org.executequery.http.spi.DefaultRemoteHttpClient.java
private HttpClient createHttpClientForManager(String host, HttpConnectionManager httpConnectionManager) { HttpClient client = new HttpClient(httpConnectionManager); client.getHostConfiguration().setHost(host, HTTP_PORT, HTTP); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); if (isUsingProxy()) { client.getHostConfiguration().setProxy(getProxyHost(), getProxyPort()); if (hasProxyAuthentication()) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(getProxyUser(), getProxyPassword())); }//from ww w . j a v a2 s .co m } return client; }
From source file:org.exist.xquery.modules.httpclient.BaseHTTPClientFunction.java
/** * Performs a HTTP Request.//from w w w .j ava 2 s. c om * * @param context The context of the calling XQuery * @param method The HTTP method for the request * @param persistState If true existing HTTP state (cookies, credentials, etc) are re-used and athe state is persisted for future HTTP Requests * @param parserFeatures Map of NekoHtml parser features to be used for the HTML parser. If null, the session-wide options will be used. * @param parserProperties Map of NekoHtml parser properties to be used for the HTML parser. If null, the session-wide options will be used. * * @return DOCUMENT ME! * * @throws IOException * @throws XPathException */ protected Sequence doRequest(final XQueryContext context, final HttpMethod method, boolean persistState, Map<String, Boolean> parserFeatures, Map<String, String> parserProperties) throws IOException, XPathException { Sequence encodedResponse = null; final HttpClient http = new HttpClient(); FeaturesAndProperties defaultFeaturesAndProperties = (FeaturesAndProperties) context .getXQueryContextVar(HTTP_MODULE_PERSISTENT_OPTIONS); if (defaultFeaturesAndProperties != null) { if (parserFeatures == null) parserFeatures = defaultFeaturesAndProperties.getFeatures(); if (parserProperties == null) parserProperties = defaultFeaturesAndProperties.getProperties(); } //execute the request try { //use existing state? if (persistState) { //get existing state final HttpState state = (HttpState) context.getXQueryContextVar(HTTP_MODULE_PERSISTENT_STATE); if (state != null) { http.setState(state); } } final String configFile = System.getProperty("http.configfile"); if (configFile != null) { final File f = new File(configFile); if (f.exists()) { setConfigFromFile(f, http); } else { logger.warn("http.configfile '" + f.getAbsolutePath() + "' does not exist!"); } } // Legacy: set the proxy server (if any) final String proxyHost = System.getProperty("http.proxyHost"); if (proxyHost != null) { //TODO: support for http.nonProxyHosts e.g. -Dhttp.nonProxyHosts="*.devonline.gov.uk|*.devon.gov.uk" final ProxyHost proxy = new ProxyHost(proxyHost, Integer.parseInt(System.getProperty("http.proxyPort"))); http.getHostConfiguration().setProxyHost(proxy); } //perform the request final int statusCode = http.executeMethod(method); encodedResponse = encodeResponseAsXML(context, method, statusCode, parserFeatures, parserProperties); //persist state? if (persistState) { context.setXQueryContextVar(HTTP_MODULE_PERSISTENT_STATE, http.getState()); } } catch (final Exception e) { LOG.error(e.getMessage(), e); encodedResponse = encodeErrorResponse(context, e.getMessage()); } return encodedResponse; }
From source file:org.exist.xquery.modules.httpclient.BaseHTTPClientFunction.java
private void setConfigFromFile(final File configFile, final HttpClient http) { if (logger.isDebugEnabled()) { logger.debug("http.configfile='" + configFile.getAbsolutePath() + "'"); }//from ww w . j a v a 2 s . com final Properties props = new Properties(); InputStream is = null; try { if (logger.isDebugEnabled()) { logger.debug("Loading proxy settings from " + configFile.getAbsolutePath()); } is = new FileInputStream(configFile); props.load(is); // Hostname / port final String proxyHost = props.getProperty("proxy.host"); final int proxyPort = Integer.valueOf(props.getProperty("proxy.port", "8080")); // Username / password final String proxyUser = props.getProperty("proxy.user"); final String proxyPassword = props.getProperty("proxy.password"); // NTLM specifics String proxyDomain = props.getProperty("proxy.ntlm.domain"); if ("NONE".equalsIgnoreCase(proxyDomain)) { if (logger.isDebugEnabled()) { logger.debug("Forcing removal NTLM"); } proxyDomain = null; } // Set scope final AuthScope authScope = new AuthScope(proxyHost, proxyPort); // Setup right credentials final Credentials credentials; if (proxyDomain == null) { credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); } else { if (logger.isDebugEnabled()) { logger.debug("Using NTLM authentication for '" + proxyDomain + "'"); } credentials = new NTCredentials(proxyUser, proxyPassword, proxyHost, proxyDomain); } // Set details final HttpState state = http.getState(); http.getHostConfiguration().setProxy(proxyHost, proxyPort); state.setProxyCredentials(authScope, credentials); if (logger.isDebugEnabled()) { logger.info("Set proxy: " + proxyUser + "@" + proxyHost + ":" + proxyPort + (proxyDomain == null ? "" : " (NTLM:'" + proxyDomain + "')")); } } catch (final IOException ex) { logger.error("Failed to read proxy configuration from '" + configFile + "'"); } finally { if (is != null) { try { is.close(); } catch (final IOException ioe) { logger.warn(ioe.getMessage(), ioe); } } } }