public boolean isProxyNeeded() { try { // Try opening the URL and see if the proxy works fine proxyConfig.openUrl(new URL("http://www.hudson-ci.org/")); } catch (IOException ex) { logger.debug(ex.getLocalizedMessage()); return true; } return false; }
/** * This method should be used wherever {@link URL#openConnection()} to * internet URLs is invoked directly. */ public static URLConnection open(URL url) throws IOException { Hudson hudson = Hudson.getInstance(); // this code might run on slaves ProxyConfiguration proxyConfig = hudson != null ? hudson.proxy : null; if (proxyConfig == null) { return url.openConnection(); } else { return proxyConfig.openUrl(url); } } private static final XStream XSTREAM = new XStream2();
public void verifyUpdateSite(String remoteUrl) throws IOException { InputStream inputStream; try { URL updateCenterRemoteUrl = new URL(remoteUrl); inputStream = proxyConfig.openUrl(updateCenterRemoteUrl).getInputStream(); } catch (Exception exc) { throw new IOException("Could not connect to " + remoteUrl + ". " + "If you are behind a firewall set HTTP proxy and try again."); } String jsonStr = IOUtils.toString(inputStream); jsonStr = jsonStr.trim(); if (jsonStr.startsWith("updateCenter.post(")) { jsonStr = jsonStr.substring("updateCenter.post(".length()); } else { throw new IOException(remoteUrl + " does not seem to be a valid update site"); } if (jsonStr.endsWith(");")) { jsonStr = jsonStr.substring(0, jsonStr.lastIndexOf(");")); } parseJson(jsonStr); }
public void refreshFromUpdateSite() throws IOException { InputStream inputStream; try { URL updateCenterRemoteUrl = new URL(getUpdateSiteUrl()); inputStream = proxyConfig.openUrl(updateCenterRemoteUrl).getInputStream(); } catch (Exception exc) { throw new IOException("Could not connect to " + getUpdateSiteUrl() + ". " + "If you are behind a firewall set HTTP proxy and try again."); } String jsonStr = IOUtils.toString(inputStream); jsonStr = jsonStr.trim(); if (jsonStr.startsWith("updateCenter.post(")) { jsonStr = jsonStr.substring("updateCenter.post(".length()); } if (jsonStr.endsWith(");")) { jsonStr = jsonStr.substring(0, jsonStr.lastIndexOf(");")); } availablePluginInfos = parseJson(jsonStr); getLocalCacheFile().write(jsonStr); }
private File download(URL src) throws IOException { URLConnection con; if ((proxyConfig != null) && (proxyConfig.name != null)) { con = proxyConfig.openUrl(src); } else { con = src.openConnection(); } int total = con.getContentLength(); CountingInputStream in = new CountingInputStream(con.getInputStream()); byte[] buf = new byte[8192]; int len; File dst = getDestination(); File tmp = new File(dst.getPath() + ".tmp"); OutputStream out = new FileOutputStream(tmp); try { while ((len = in.read(buf)) >= 0) { out.write(buf, 0, len); //job.status = job.new Installing(total == -1 ? -1 : in.getCount() * 100 / total); } } catch (IOException e) { throw new IOException("Failed to load " + src + " to " + tmp, e); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } if (total != -1 && total != tmp.length()) { throw new IOException("Inconsistent file length: expected " + total + " but only got " + tmp.length()); } return tmp; }
public HttpResponse doProxyConfigure( @QueryParameter("proxy.server") String server, @QueryParameter("proxy.port") String port, @QueryParameter("proxy.noProxyFor") String noProxyFor, @QueryParameter("proxy.userName") String userName, @QueryParameter("proxy.password") String password, @QueryParameter("proxy.authNeeded") String authNeeded) throws IOException { if (!hudsonSecurityManager.hasPermission(Permission.HUDSON_ADMINISTER)) { return HttpResponses.forbidden(); } try { boolean proxySet = setProxy(server, port, noProxyFor, userName, password, authNeeded); if (proxySet) { proxyConfig.save(); } // Try opening a URL and see if the proxy works fine proxyConfig.openUrl(new URL("http://www.google.com")); } catch (IOException ex) { return new ErrorHttpResponse(ex.getLocalizedMessage()); } return HttpResponses.ok(); }
public HttpResponse doProxyConfigure( @QueryParameter("proxy.server") String server, @QueryParameter("proxy.port") String port, @QueryParameter("proxy.noProxyFor") String noProxyFor, @QueryParameter("proxy.userName") String userName, @QueryParameter("proxy.password") String password, @QueryParameter("proxy.authNeeded") String authNeeded) throws IOException { if (!hudsonSecurityManager.hasPermission(Permission.HUDSON_ADMINISTER)) { return HttpResponses.forbidden(); } try { boolean proxySet = setProxy(server, port, noProxyFor, userName, password, authNeeded); if (proxySet) { proxyConfig.save(); } // Try opening a URL and see if the proxy works fine proxyConfig.openUrl(new URL("http://www.hudson-ci.org/")); } catch (IOException ex) { return HttpResponses.error(HttpServletResponse.SC_BAD_REQUEST, ex); } return HttpResponses.ok(); }
public HttpResponse doRefreshUpdateCenter() throws IOException { if (!hudsonSecurityManager.hasPermission(Permission.HUDSON_ADMINISTER)) { return HttpResponses.forbidden(); } // Check if you are able to connect try { URL updateCenterRemoteUrl = new URL(updateSiteManager.getUpdateSiteUrl()); proxyConfig.openUrl(updateCenterRemoteUrl); } catch (Exception exc) { return new ErrorHttpResponse("Could not connect to " + updateSiteManager.getUpdateSiteUrl() + ". " + "If you are behind a firewall set HTTP proxy and try again."); } try { updateSiteManager.refreshFromUpdateSite(); } catch (IOException ex) { return new ErrorHttpResponse("Updates could not be refreshed. " + ex.getLocalizedMessage()); } return HttpResponses.ok(); }