List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration
public HostConfiguration()
From source file:org.jivesoftware.openfire.update.UpdateManager.java
/** * Download and install latest version of plugin. * * @param url the URL of the latest version of the plugin. * @return true if the plugin was successfully downloaded and installed. *//*from w w w .java 2 s. c o m*/ public boolean downloadPlugin(String url) { boolean installed = false; // Download and install new version of plugin HttpClient httpClient = new HttpClient(); // Check if a proxy should be used if (isUsingProxy()) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(getProxyHost(), getProxyPort()); httpClient.setHostConfiguration(hc); } GetMethod getMethod = new GetMethod(url); //execute the method try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode == 200) { //get the resonse as an InputStream InputStream in = getMethod.getResponseBodyAsStream(); String pluginFilename = url.substring(url.lastIndexOf("/") + 1); installed = XMPPServer.getInstance().getPluginManager().installPlugin(in, pluginFilename); in.close(); if (installed) { // Remove the plugin from the list of plugins to update for (Update update : pluginUpdates) { if (update.getURL().equals(url)) { update.setDownloaded(true); } } // Save response in a file for later retrieval saveLatestServerInfo(); } } } catch (IOException e) { Log.warn("Error downloading new plugin version", e); } return installed; }
From source file:org.kei.android.phone.cellhistory.towers.CellIdHelper.java
public static int tryToLocate(final Context context, final TowerInfo ti, int cfg_timeout, final String mode, final String apiKeyOpenCellID) { int timeout = cfg_timeout * 1000, ret = CellIdRequestEntity.OK; HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); connectionManager.getParams().setConnectionTimeout(timeout); connectionManager.getParams().setSoTimeout(timeout); // Create a connection to some 'hidden' Google-API String baseURL = null;/*from w ww . java 2s .c o m*/ if (mode.equals(OPEN_CELL_ID_API)) { ti.lock(); try { baseURL = "http://opencellid.org/cell/get?key=" + apiKeyOpenCellID + "&mcc=" + ti.getMCC() + "&mnc=" + ti.getMNC() + "&cellid=" + ti.getCellId() + "&lac=" + ti.getLac() + "&format=json"; } finally { ti.unlock(); } } else baseURL = "http://www.google.com/glm/mmap"; HttpConnection connection = null; ti.setCellLatitude(Double.NaN); ti.setCellLongitude(Double.NaN); try { // Setup the connection HttpURL httpURL = null; if (baseURL.startsWith("https")) httpURL = new HttpsURL(baseURL); else httpURL = new HttpURL(baseURL); final HostConfiguration host = new HostConfiguration(); host.setHost(httpURL.getHost(), httpURL.getPort()); connection = connectionManager.getConnection(host); // Open it connection.open(); if (mode.equals(OPEN_CELL_ID_API)) ret = new OpenCellIdRequestEntity(ti).decode(baseURL, connection, timeout); else ret = new GoogleHiddenRequestEntity(ti).decode(baseURL, connection, timeout); } catch (Exception e) { Log.e(CellIdHelper.class.getSimpleName(), "Exception: " + e.getMessage(), e); ret = CellIdRequestEntity.EXCEPTION; CellHistoryApp.addLog(context, "tryToLocate::Exception: " + e.getMessage()); } finally { connection.close(); } connectionManager.releaseConnection(connection); return ret; }
From source file:org.lucterios.engine.transport.HttpTransportImpl.java
private HostConfiguration getHostCnx() { HostConfiguration host_cnx = new HostConfiguration(); if (getUseProxy() && (!"".equals(mProxyServer)) && (mProxyPort != 0)) { String[] proxy_param = mProxyServer.split(":|@"); if (proxy_param.length != 3) host_cnx.setProxy(mProxyServer, mProxyPort); else {//from ww w .ja v a2 s . c o m host_cnx.setProxy(proxy_param[2], mProxyPort); Credentials credentials = new UsernamePasswordCredentials(proxy_param[0], proxy_param[1]); AuthScope authscope = new AuthScope(proxy_param[2], mProxyPort); m_Cnx.getState().setProxyCredentials(authscope, credentials); } } return host_cnx; }
From source file:org.manalang.monkeygrease.utils.HttpClient.java
public void init() throws ServletException { // Sets HTTP proxy host and port if it exists in the web.xml super.init(); String proxyHost = this.getInitParameter("ProxyHost"); String proxyPortStr = this.getInitParameter("ProxyPort"); int proxyPort = 0; try {// w ww. ja va 2s. co m if (proxyPortStr != null || proxyPortStr != "") proxyPort = Integer.parseInt(proxyPortStr); } catch (NumberFormatException e) { org.manalang.monkeygrease.MonkeygreaseFilter.log .config("Proxy port number is invalid: " + proxyPortStr); } if (proxyHost != null && proxyHost != "" && proxyPort != 0) { hc = new HostConfiguration(); hc.setProxy(proxyHost, proxyPort); } // Create an instance of HttpClient. client = new org.apache.commons.httpclient.HttpClient(); // Sets proxy if it exists if (hc != null) client.setHostConfiguration(hc); }
From source file:org.methodize.nntprss.feed.Channel.java
/** * Retrieves the latest RSS doc from the remote site */// ww w.jav a2 s.c om public synchronized void poll() { // Use method-level variable // Guard against change in history mid-poll polling = true; // boolean keepHistory = historical; long keepExpiration = expiration; lastPolled = new Date(); int statusCode = -1; HttpMethod method = null; String urlString = url.toString(); try { HttpClient httpClient = getHttpClient(); channelManager.configureHttpClient(httpClient); HttpResult result = null; try { connected = true; boolean redirected = false; int count = 0; do { URL currentUrl = new URL(urlString); method = new GetMethod(urlString); method.setRequestHeader("User-agent", AppConstants.getUserAgent()); method.setRequestHeader("Accept-Encoding", "gzip"); method.setFollowRedirects(false); method.setDoAuthentication(true); // ETag if (lastETag != null) { method.setRequestHeader("If-None-Match", lastETag); } // Last Modified if (lastModified != 0) { final String NAME = "If-Modified-Since"; //defend against such fun like net.freeroller.rickard got If-Modified-Since "Thu, 24 Aug 2028 12:29:54 GMT" if (lastModified < System.currentTimeMillis()) { final String DATE = httpDate.format(new Date(lastModified)); method.setRequestHeader(NAME, DATE); log.debug("channel " + this.name + " using " + NAME + " " + DATE); //ALEK } } method.setFollowRedirects(false); method.setDoAuthentication(true); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(currentUrl.getHost(), currentUrl.getPort(), currentUrl.getProtocol()); result = executeHttpRequest(httpClient, hostConfig, method); statusCode = result.getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { redirected = true; // Resolve against current URI - may be a relative URI try { urlString = new java.net.URI(urlString).resolve(result.getLocation()).toString(); } catch (URISyntaxException use) { // Fall back to just using location from result urlString = result.getLocation(); } if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY && channelManager.isObserveHttp301()) { try { url = new URL(urlString); if (log.isInfoEnabled()) { log.info("Channel = " + this.name + ", updated URL from HTTP Permanent Redirect"); } } catch (MalformedURLException mue) { // Ignore URL permanent redirect for now... } } } else { redirected = false; } // method.getResponseBody(); // method.releaseConnection(); count++; } while (count < 5 && redirected); } catch (HttpRecoverableException hre) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Temporary Http Problem - " + hre.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (ConnectException ce) { // @TODO Might also be a connection refused - not only a timeout... if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Connection Timeout, skipping - " + ce.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (UnknownHostException ue) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Unknown Host Exception, skipping"); } status = STATUS_UNKNOWN_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (NoRouteToHostException re) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - No Route To Host Exception, skipping"); } status = STATUS_NO_ROUTE_TO_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (SocketException se) { // e.g. Network is unreachable if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Socket Exception, skipping"); } status = STATUS_SOCKET_EXCEPTION; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } // Only process if ok - if not ok (e.g. not modified), don't do anything if (connected && statusCode == HttpStatus.SC_OK) { PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(result.getResponse()), PUSHBACK_BUFFER_SIZE); skipBOM(pbis); BufferedInputStream bis = new BufferedInputStream(pbis); DocumentBuilder db = AppConstants.newDocumentBuilder(); try { Document rssDoc = null; if (!parseAtAllCost) { try { rssDoc = db.parse(bis); } catch (InternalError ie) { // Crimson library throws InternalErrors if (log.isDebugEnabled()) { log.debug("InternalError thrown by Crimson", ie); } throw new SAXException("InternalError thrown by Crimson: " + ie.getMessage()); } } else { // Parse-at-all-costs selected // Read in document to local array - may need to parse twice ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int bytesRead = bis.read(buf); while (bytesRead > -1) { if (bytesRead > 0) { bos.write(buf, 0, bytesRead); } bytesRead = bis.read(buf); } bos.flush(); bos.close(); byte[] rssDocBytes = bos.toByteArray(); try { // Try the XML document parser first - just in case // the doc is well-formed rssDoc = db.parse(new ByteArrayInputStream(rssDocBytes)); } catch (SAXParseException spe) { if (log.isDebugEnabled()) { log.debug("XML parse failed, trying tidy"); } // Fallback to parse-at-all-costs parser rssDoc = LooseParser.parse(new ByteArrayInputStream(rssDocBytes)); } } processChannelDocument(expiration, rssDoc); // Update last modified / etag from headers // lastETag = httpCon.getHeaderField("ETag"); // lastModified = httpCon.getHeaderFieldDate("Last-Modified", 0); Header hdrETag = method.getResponseHeader("ETag"); lastETag = hdrETag != null ? hdrETag.getValue() : null; Header hdrLastModified = method.getResponseHeader("Last-Modified"); lastModified = hdrLastModified != null ? parseHttpDate(hdrLastModified.getValue()) : 0; log.debug("channel " + this.name + " parsed Last-Modifed " + hdrLastModified + " to " + (lastModified != 0 ? "" + (new Date(lastModified)) : "" + lastModified)); //ALEK status = STATUS_OK; } catch (SAXParseException spe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Error parsing RSS document - check feed"); } status = STATUS_INVALID_CONTENT; } bis.close(); // end if response code == HTTP_OK } else if (connected && statusCode == HttpStatus.SC_NOT_MODIFIED) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - HTTP_NOT_MODIFIED, skipping"); } status = STATUS_OK; } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Proxy authentication required"); } status = STATUS_PROXY_AUTHENTICATION_REQUIRED; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Authentication required"); } status = STATUS_USER_AUTHENTICATION_REQUIRED; } // Update channel in database... channelDAO.updateChannel(this); } catch (FileNotFoundException fnfe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - File not found returned by web server - check feed"); } status = STATUS_NOT_FOUND; } catch (Exception e) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Exception while polling channel", e); } } catch (NoClassDefFoundError ncdf) { // Throw if SSL / redirection to HTTPS if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - NoClassDefFound", ncdf); } } finally { connected = false; polling = false; } }
From source file:org.methodize.nntprss.feed.ChannelManager.java
private ChannelManager() { // Private constructor - singleton class channelDAO = ChannelManagerDAO.getChannelManagerDAO().getChannelDAO(); hostConfig = new HostConfiguration(); httpConMgr = new MultiThreadedHttpConnectionManager(); }
From source file:org.methodize.nntprss.feed.ChannelManager.java
private void updateProxyConfig() { // Set proxy configuration, if necessary. if (useProxy && (proxyServer != null) && (proxyServer.length() > 0)) { // Set HttpClient proxy configuration hostConfig.setProxy(proxyServer, proxyPort); } else {/*w w w .j av a 2 s. c o m*/ hostConfig = new HostConfiguration(); } }
From source file:org.mimacom.maven.plugins.liferay.prepare.MultithreadedDownloader.java
MultithreadedDownloader(int threads) { this.threads = threads; MultiThreadedHttpConnectionManager conMgr = new MultiThreadedHttpConnectionManager(); HostConfiguration hc = new HostConfiguration(); HttpConnectionManagerParams params = conMgr.getParams(); params.setMaxConnectionsPerHost(hc, 10); httpClient = new HttpClient(conMgr); httpClient.setHostConfiguration(hc); }
From source file:org.moxie.proxy.connection.ProxyDownload.java
/** * Do the download.//from ww w . j a va 2 s .co m * * @throws IOException * @throws DownloadFailed */ public void download() throws IOException, DownloadFailed { if (!config.isAllowed(url)) { throw new DownloadFailed( "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in Moxie Proxy config"); } HttpClient client = new HttpClient(); String userAgent = config.getUserAgent(); if (userAgent != null && userAgent.trim().length() > 0) { client.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); } String msg = ""; if (config.useProxy(url)) { Proxy proxy = config.getProxy(url); Credentials defaultcreds = new UsernamePasswordCredentials(proxy.username, proxy.password); AuthScope scope = new AuthScope(proxy.host, proxy.port, AuthScope.ANY_REALM); HostConfiguration hc = new HostConfiguration(); hc.setProxy(proxy.host, proxy.port); client.setHostConfiguration(hc); client.getState().setProxyCredentials(scope, defaultcreds); msg = "via proxy "; } log.info("Downloading " + msg + "to " + dest.getAbsolutePath()); GetMethod get = new GetMethod(url.toString()); get.setFollowRedirects(true); try { int status = client.executeMethod(get); log.info("Download status: " + status); if (status == 1 && log.isLoggable(Level.FINE)) { Header[] header = get.getResponseHeaders(); for (int i = 0; i < header.length; i++) log.fine(header[i].toString().trim()); } log.info("Content: " + valueOf(get.getResponseHeader("Content-Length")) + " bytes; " + valueOf(get.getResponseHeader("Content-Type"))); if (status != HttpStatus.SC_OK) { throw new DownloadFailed(get); } // Make sure the temporary file is created in // the destination folder, otherwise // dl.renameTo(dest) might not work // for example if you have a separate /tmp partition // on Linux File destinationFolder = dest.getParentFile(); dest.getParentFile().mkdirs(); File dl = File.createTempFile("moxie-", ".tmp", destinationFolder); OutputStream out = new BufferedOutputStream(new FileOutputStream(dl)); copy(get.getResponseBodyAsStream(), out); out.close(); // create folder structure after successful download // - no, we create it before the download! //dest.getParentFile().mkdirs(); if (dest.exists()) { dest.delete(); } dl.renameTo(dest); // preserve last-modified, if possible try { Header lastModified = get.getResponseHeader("Last-Modified"); if (lastModified != null) { Date date = DateUtil.parseDate(lastModified.getValue()); dest.setLastModified(date.getTime()); } } catch (Exception e) { log.log(Level.WARNING, "could not parse \"last-modified\" for " + url, e); } } finally { get.releaseConnection(); } }
From source file:org.moxie.proxy.connection.ProxyHead.java
/** * Do the download (of the headers).//from w w w. ja v a 2s . c o m * * @throws IOException * @throws DownloadFailed */ public void download() throws IOException, DownloadFailed { if (!config.isAllowed(url)) { throw new DownloadFailed( "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in Moxie Proxy config"); } HttpClient client = new HttpClient(); String userAgent = config.getUserAgent(); if (userAgent != null && userAgent.trim().length() > 0) { client.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); } String msg = ""; if (config.useProxy(url)) { Proxy proxy = config.getProxy(url); Credentials defaultcreds = new UsernamePasswordCredentials(proxy.username, proxy.password); AuthScope scope = new AuthScope(proxy.host, proxy.port, AuthScope.ANY_REALM); HostConfiguration hc = new HostConfiguration(); hc.setProxy(proxy.host, proxy.port); client.setHostConfiguration(hc); client.getState().setProxyCredentials(scope, defaultcreds); msg = "via proxy "; } log.info("Downloading " + msg + url); MyHeadMethod head = new MyHeadMethod(url.toString()); head.setFollowRedirects(true); try { int status = client.executeMethod(head); log.info("Download status: " + status); this.header = head.getResponseHeaders(); this.statusLine = head.getStatusLine().toString(); this.responseBody = head.getResponseBody(); if (status == 1 && log.isLoggable(Level.FINE)) { for (int i = 0; i < header.length; i++) log.fine(header[i].toString().trim()); } log.info("Content: " + valueOf(head.getResponseHeader("Content-Length")) + " bytes; " + valueOf(head.getResponseHeader("Content-Type"))); if (status != HttpStatus.SC_OK) { throw new DownloadFailed(head); } } finally { head.releaseConnection(); } }