List of usage examples for java.net URLConnection getURL
public URL getURL()
From source file:org.abs_models.backend.erlang.ErlApp.java
private void copyRuntime() throws IOException { InputStream is = null;// w w w.j a va 2s. co m // TODO: this only works when the erlang compiler is invoked // from a jar file. See http://stackoverflow.com/a/2993908 on // how to handle the other case. URLConnection resource = getClass().getResource("").openConnection(); try { new File(destDir + "/absmodel/ebin").mkdirs(); if (resource instanceof JarURLConnection) { for (String f : RUNTIME_FILES) { if (f.endsWith("/*")) { String dirname = f.substring(0, f.length() - 2); String inname = JAR_PATH + dirname; String outname = destDir + "/" + dirname; new File(outname).mkdirs(); copyJarDirectory(((JarURLConnection) resource).getJarFile(), inname, outname); } else { is = ClassLoader.getSystemResourceAsStream(JAR_PATH + f); if (is == null) throw new RuntimeException("Could not locate Runtime file:" + f); String outputFile = f.replace('/', File.separatorChar); File file = new File(destDir, outputFile); file.getParentFile().mkdirs(); Files.asByteSink(file).writeFrom(is); } } } else if (resource.getURL().getProtocol().equals("file")) { /* stolz: This at least works for the unit tests from within Eclipse */ File file = new File("src/main/resources/erlang/"); assert file.exists(); FileUtils.copyDirectory(file, destDir); } else { throw new UnsupportedOperationException("File type: " + resource); } if (index_file != null) { File http_out_file = new File(destDir + "/absmodel/priv/index.html"); http_out_file.getParentFile().mkdirs(); FileUtils.copyFile(index_file, http_out_file); } if (static_dir != null) { File static_out_dir = new File(destDir + "/absmodel/priv/static"); static_out_dir.mkdirs(); FileUtils.copyDirectory(static_dir, static_out_dir); } } finally { if (is != null) is.close(); } for (String f : EXEC_FILES) { new File(destDir, f).setExecutable(true, false); } }
From source file:net.unit8.maven.plugins.handlebars.HandlebarsEngine.java
protected void fetchHandlebars(String handlebarsName) throws MojoExecutionException { String downloadUrl = null;/*from w w w . j a va 2s .c om*/ URLConnection conn = null; try { conn = handlebarsDownloadsUri.toURL().openConnection(); List<GitHubDownloadDto> githubDownloadDtoList = JSON.decode(conn.getInputStream(), (new ArrayList<GitHubDownloadDto>() { }).getClass().getGenericSuperclass()); for (GitHubDownloadDto githubDownloadDto : githubDownloadDtoList) { if (StringUtils.equals(githubDownloadDto.getName(), handlebarsName)) { downloadUrl = githubDownloadDto.getHtmlUrl(); } } } catch (Exception e) { throw new MojoExecutionException("Failure fetch handlebars.", e); } finally { if (conn != null) { ((HttpURLConnection) conn).disconnect(); } } conn = null; try { if (!cacheDir.exists()) { FileUtils.forceMkdir(cacheDir); } conn = new URL(downloadUrl).openConnection(); if (((HttpURLConnection) conn).getResponseCode() == 302) { String location = conn.getHeaderField("Location"); ((HttpURLConnection) conn).disconnect(); conn = new URL(location).openConnection(); } LOG.info("Fetch handlebars.js from GitHub (" + conn.getURL() + ")"); IOUtils.copy(conn.getInputStream(), new FileOutputStream(new File(cacheDir, handlebarsName))); } catch (Exception e) { throw new MojoExecutionException("Failure fetch handlebars.", e); } finally { if (conn != null) { ((HttpURLConnection) conn).disconnect(); } } }
From source file:com.moviejukebox.tools.WebBrowser.java
private void readHeader(URLConnection cnx) { // read new cookies and update our cookies for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) { if ("Set-Cookie".equals(header.getKey())) { for (String cookieHeader : header.getValue()) { String[] cookieElements = cookieHeader.split(" *; *"); if (cookieElements.length >= 1) { String[] firstElem = cookieElements[0].split(" *= *"); String cookieName = firstElem[0]; String cookieValue = firstElem.length > 1 ? firstElem[1] : null; String cookieDomain = null; // find cookie domain for (int i = 1; i < cookieElements.length; i++) { String[] cookieElement = cookieElements[i].split(" *= *"); if ("domain".equals(cookieElement[0])) { cookieDomain = cookieElement.length > 1 ? cookieElement[1] : null; break; }//from ww w . j a va 2 s .c o m } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } putCookie(cookieDomain, cookieName, cookieValue); } } } } }
From source file:org.dataconservancy.dcs.access.http.dataPackager.ZipPackageCreator.java
void downloadFileStream(SeadFile file, OutputStream destination) throws EntityNotFoundException, EntityTypeException { String filePath = null;/*ww w .java 2 s. c o m*/ if (file.getPrimaryLocation().getType() != null && file.getPrimaryLocation().getType().length() > 0 && file.getPrimaryLocation().getLocation() != null && file.getPrimaryLocation().getLocation().length() > 0 && file.getPrimaryLocation().getName() != null && file.getPrimaryLocation().getName().length() > 0) { if ((file.getPrimaryLocation().getName() .equalsIgnoreCase(ArchiveEnum.Archive.IU_SCHOLARWORKS.getArchive())) || (file.getPrimaryLocation().getName() .equalsIgnoreCase(ArchiveEnum.Archive.UIUC_IDEALS.getArchive()))) { URLConnection connection = null; try { String location = file.getPrimaryLocation().getLocation(); location = location.replace("http://maple.dlib.indiana.edu:8245/", "https://scholarworks.iu.edu/"); connection = new URL(location).openConnection(); connection.setDoOutput(true); final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) { } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }; if (connection.getURL().getProtocol().equalsIgnoreCase("https")) { final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory); } IOUtils.copy(connection.getInputStream(), destination); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (KeyManagementException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return; } else if (file.getPrimaryLocation().getType() .equalsIgnoreCase(ArchiveEnum.Archive.SDA.getType().getText()) && file.getPrimaryLocation().getName().equalsIgnoreCase(ArchiveEnum.Archive.SDA.getArchive())) { filePath = file.getPrimaryLocation().getLocation(); String[] pathArr = filePath.split("/"); try { Sftp sftp = new Sftp(config.getSdahost(), config.getSdauser(), config.getSdapwd(), config.getSdamount()); sftp.downloadFile(filePath.substring(0, filePath.lastIndexOf('/')), pathArr[pathArr.length - 1], destination); sftp.disConnectSession(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } } else { if (file.getSecondaryDataLocations() != null && file.getSecondaryDataLocations().size() > 0) { for (SeadDataLocation dataLocation : file.getSecondaryDataLocations()) { if (dataLocation.getType().equalsIgnoreCase(ArchiveEnum.Archive.SDA.getType().getText()) && dataLocation.getName().equalsIgnoreCase(ArchiveEnum.Archive.SDA.getArchive())) { filePath = dataLocation.getLocation(); String[] pathArr = filePath.split("/"); try { Sftp sftp = new Sftp(config.getSdahost(), config.getSdauser(), config.getSdapwd(), config.getSdamount()); sftp.downloadFile(filePath.substring(0, filePath.lastIndexOf('/')), pathArr[pathArr.length - 1], destination); sftp.disConnectSession(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } } } } return; }
From source file:eionet.cr.staging.FileDownloader.java
/** * Derives a name for the file to be downloaded from the given {@link URLConnection}. * * @param connection The given {@link URLConnection}. * @return The derived file name./*from w w w.j a v a2 s.c o m*/ */ private String getFileName(URLConnection connection) { // If file name already given, just return it. if (StringUtils.isNotBlank(newFileName)) { return newFileName; } // Attempt detection from the response's "Content-Disposition" header. String contentDisposition = connection.getHeaderField("Content-Disposition"); if (StringUtils.isNotBlank(contentDisposition)) { String s = StringUtils.substringAfter(contentDisposition, "filename"); if (StringUtils.isNotBlank(s)) { s = StringUtils.substringAfter(s, "="); if (StringUtils.isNotBlank(s)) { s = StringUtils.substringAfter(s, "\""); if (StringUtils.isNotBlank(s)) { s = StringUtils.substringBefore(s, "\""); if (StringUtils.isNotBlank(s)) { return s.trim(); } } } } } // // Attempt detection from the response's "Content-Location" header. // String contentLocation = connection.getHeaderField("Content-Location"); // if (StringUtils.isNotBlank(contentLocation)) { // String s = new File(contentLocation).getName(); // if (StringUtils.isNotBlank(s)) { // return s.trim(); // } // } // // Attempt detection from the URL itself. String s = StringUtils.substringAfterLast(connection.getURL().toString(), "#"); if (StringUtils.isBlank(s)) { s = StringUtils.substringAfterLast(connection.getURL().toString(), "/"); } if (StringUtils.isNotBlank(s)) { // Remove all characters that are not one of these: a latin letter, a digit, a minus, a dot, an underscore. s = s.replaceAll("[^a-zA-Z0-9-._]+", ""); } // If still no success, then just generate a hash from the URL. return StringUtils.isBlank(s) ? DigestUtils.md5Hex(connection.getURL().toString()) : s; }
From source file:org.qedeq.base.io.UrlUtility.java
/** * Make local copy of an URL./*www .j a va 2 s . c o m*/ * * @param url Save this URL. * @param f Save into this file. An existing file is overwritten. * @param proxyHost Use this proxy host. * @param proxyPort Use this port at proxy host. * @param nonProxyHosts This are hosts not to be proxied. * @param connectTimeout Connection timeout. * @param readTimeout Read timeout. * @param listener Here completion events are fired. * @throws IOException Saving failed. */ public static void saveUrlToFile(final String url, final File f, final String proxyHost, final String proxyPort, final String nonProxyHosts, final int connectTimeout, final int readTimeout, final LoadingListener listener) throws IOException { final String method = "saveUrlToFile()"; Trace.begin(CLASS, method); // if we are not web started and running under Java 1.4 we use apache commons // httpclient library (so we can set timeouts) if (!isSetConnectionTimeOutSupported() && !IoUtility.isWebStarted()) { saveQedeqFromWebToBufferApache(url, f, proxyHost, proxyPort, nonProxyHosts, connectTimeout, readTimeout, listener); Trace.end(CLASS, method); return; } // set proxy properties according to kernel configuration (if not webstarted) if (!IoUtility.isWebStarted()) { if (proxyHost != null) { System.setProperty("http.proxyHost", proxyHost); } if (proxyPort != null) { System.setProperty("http.proxyPort", proxyPort); } if (nonProxyHosts != null) { System.setProperty("http.nonProxyHosts", nonProxyHosts); } } FileOutputStream out = null; InputStream in = null; try { final URLConnection connection = new URL(url).openConnection(); if (connection instanceof HttpURLConnection) { final HttpURLConnection httpConnection = (HttpURLConnection) connection; // if we are running at least under Java 1.5 the following code should be executed if (isSetConnectionTimeOutSupported()) { try { YodaUtility.executeMethod(httpConnection, "setConnectTimeout", new Class[] { Integer.TYPE }, new Object[] { new Integer(connectTimeout) }); } catch (NoSuchMethodException e) { Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout was previously found", e); } catch (InvocationTargetException e) { Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout throwed an error", e); } } // if we are running at least under Java 1.5 the following code should be executed if (isSetReadTimeoutSupported()) { try { YodaUtility.executeMethod(httpConnection, "setReadTimeout", new Class[] { Integer.TYPE }, new Object[] { new Integer(readTimeout) }); } catch (NoSuchMethodException e) { Trace.fatal(CLASS, method, "URLConnection.setReadTimeout was previously found", e); } catch (InvocationTargetException e) { Trace.fatal(CLASS, method, "URLConnection.setReadTimeout throwed an error", e); } } int responseCode = httpConnection.getResponseCode(); if (responseCode == 200) { in = httpConnection.getInputStream(); } else { in = httpConnection.getErrorStream(); final String errorText = IoUtility.loadStreamWithoutException(in, 1000); throw new IOException("Response code from HTTP server was " + responseCode + (errorText.length() > 0 ? "\nResponse text from HTTP server was:\n" + errorText : "")); } } else { Trace.paramInfo(CLASS, method, "connection.getClass", connection.getClass().toString()); in = connection.getInputStream(); } if (!url.equals(connection.getURL().toString())) { throw new FileNotFoundException( "\"" + url + "\" was substituted by " + "\"" + connection.getURL() + "\" from server"); } final double maximum = connection.getContentLength(); IoUtility.createNecessaryDirectories(f); out = new FileOutputStream(f); final byte[] buffer = new byte[4096]; int bytesRead; // bytes read during one buffer read int position = 0; // current reading position within the whole document // continue writing while ((bytesRead = in.read(buffer)) != -1) { position += bytesRead; out.write(buffer, 0, bytesRead); if (maximum > 0) { double completeness = position / maximum; if (completeness < 0) { completeness = 0; } if (completeness > 100) { completeness = 1; } listener.loadingCompletenessChanged(completeness); } } listener.loadingCompletenessChanged(1); } finally { IoUtility.close(out); out = null; IoUtility.close(in); in = null; Trace.end(CLASS, method); } }
From source file:org.apache.axis2.wsdl.util.WSDLWrapperReloadImpl.java
private static String getExplicitURI(URL wsdlURL) throws WSDLException { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + ") "); }/*w w w. j a va 2 s .c o m*/ String explicitURI = null; ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return Thread.currentThread().getContextClassLoader(); } }); try { URL url = wsdlURL; String filePath = null; boolean isFileProtocol = (url != null && "file".equals(url.getProtocol())) ? true : false; if (isFileProtocol) { filePath = (url != null) ? url.getPath() : null; URI uri = null; if (url != null) { uri = new URI(url.toString()); } // Check if the uri has relative path // ie path is not absolute and is not starting with a "/" boolean isRelativePath = (filePath != null && !new File(filePath).isAbsolute()) ? true : false; if (isRelativePath) { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): WSDL URL has a relative path"); } // Lets read the complete WSDL URL for relative path from class loader // Use relative path of url to fetch complete URL. url = getAbsoluteURL(classLoader, filePath, wsdlURL); if (url == null) { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "WSDL URL for relative path not found in ClassLoader"); log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "Unable to read WSDL from relative path, check the relative path"); log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "Relative path example: file:/WEB-INF/wsdl/<wsdlfilename>"); log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "Using relative path as default wsdl URL to load wsdl Definition."); } url = wsdlURL; } else { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "WSDL URL found for relative path: " + filePath + " scheme: " + uri.getScheme()); } } } } URLConnection urlCon = url.openConnection(); InputStream is = null; try { is = getInputStream(urlCon); } catch (IOException e) { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "Could not open url connection. Trying to use " + "classloader to get another URL."); } if (filePath != null) { url = getAbsoluteURL(classLoader, filePath, wsdlURL); if (url == null) { if (log.isDebugEnabled()) { log.debug("Could not locate URL for wsdl. Reporting error"); } throw new WSDLException("WSDL4JWrapper : ", e.getMessage(), e); } else { urlCon = url.openConnection(); if (log.isDebugEnabled()) { log.debug("Found URL for WSDL from jar"); } } } else { if (isDebugEnabled) { log.debug(myClassName + ".getExplicitURI(" + wsdlURL + "): " + "Could not get URL from classloader. Reporting " + "error due to no file path."); } throw new WSDLException("WSDLWrapperReloadImpl : ", e.getMessage(), e); } } if (is != null) { is.close(); } explicitURI = urlCon.getURL().toString(); } catch (Exception ex) { throw new WSDLException("WSDLWrapperReloadImpl : ", ex.getMessage(), ex); } return explicitURI; }
From source file:org.appcelerator.titanium.util.TiResponseCache.java
@Override public CacheRequest put(URI uri, URLConnection conn) throws IOException { if (cacheDir == null) return null; // Make sure the cacheDir exists, in case user clears cache while app is running if (!cacheDir.exists()) { cacheDir.mkdirs();/*from w w w .j ava 2s . c om*/ } // Gingerbread 2.3 bug: getHeaderField tries re-opening the InputStream // getHeaderFields() just checks the response itself Map<String, List<String>> headers = makeLowerCaseHeaders(conn.getHeaderFields()); String cacheControl = getHeader(headers, "cache-control"); if (cacheControl != null && cacheControl.matches("^.*(no-cache|no-store|must-revalidate).*")) { return null; // See RFC-2616 } boolean skipTransferEncodingHeader = false; String tEncoding = getHeader(headers, "transfer-encoding"); if (tEncoding != null && tEncoding.toLowerCase().equals("chunked")) { skipTransferEncodingHeader = true; // don't put "chunked" transfer-encoding into our header file, else the http connection object that gets our header information will think the data starts with a chunk length specification } // Form the headers and generate the content length String newl = System.getProperty("line.separator"); long contentLength = getHeaderInt(headers, "content-length", 0); StringBuilder sb = new StringBuilder(); for (String hdr : headers.keySet()) { if (!skipTransferEncodingHeader || !hdr.equals("transfer-encoding")) { for (String val : headers.get(hdr)) { sb.append(hdr); sb.append("="); sb.append(val); sb.append(newl); } } } if (contentLength + sb.length() > maxCacheSize) { return null; } // Work around an android bug which gives us the wrong URI try { uri = conn.getURL().toURI(); } catch (URISyntaxException e) { } // Get our key, which is a hash of the URI String hash = DigestUtils.shaHex(uri.toString()); // Make our cache files File hFile = new File(cacheDir, hash + HEADER_SUFFIX); File bFile = new File(cacheDir, hash + BODY_SUFFIX); // Write headers synchronously FileWriter hWriter = new FileWriter(hFile); try { hWriter.write(sb.toString()); } finally { hWriter.close(); } synchronized (this) { // Don't add it to the cache if its already being written if (!bFile.createNewFile()) { return null; } return new TiCacheRequest(uri, bFile, hFile, contentLength); } }
From source file:org.apache.axis2.jaxws.util.WSDL4JWrapper.java
private void commonPartsURLConstructor(URL wsdlURL, ConfigurationContext configContext) throws FileNotFoundException, UnknownHostException, ConnectException, IOException, WSDLException { this.configContext = configContext; // debugMemoryParms(configContext); if (log.isDebugEnabled()) { log.debug("WSDL4JWrapper(URL,ConfigurationContext) - Looking for wsdl file on client: " + (wsdlURL != null ? wsdlURL.getPath() : null)); }/* ww w .j av a 2 s . co m*/ ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return Thread.currentThread().getContextClassLoader(); } }); this.wsdlURL = wsdlURL; URLConnection urlCon; try { urlCon = getPrivilegedURLConnection(this.wsdlURL); InputStream is = null; try { is = getInputStream(urlCon); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Could not open url connection. Trying to use " + "classloader to get another URL."); } String filePath = wsdlURL != null ? wsdlURL.getPath() : null; if (filePath != null) { URL url = getAbsoluteURL(classLoader, filePath); if (url == null) { if (log.isDebugEnabled()) { log.debug("Could not locate URL for wsdl. Reporting error"); } throw new WSDLException("WSDL4JWrapper : ", e.getMessage(), e); } else { urlCon = openConnection(url); if (log.isDebugEnabled()) { log.debug("Found URL for WSDL from jar"); } } } else { if (log.isDebugEnabled()) { log.debug("Could not get URL from classloader. Reporting " + "error due to no file path."); } throw new WSDLException("WSDL4JWrapper : ", e.getMessage(), e); } } if (is != null) { is.close(); } this.wsdlExplicitURL = urlCon.getURL().toString(); getDefinition(); } catch (FileNotFoundException ex) { throw ex; } catch (UnknownHostException ex) { throw ex; } catch (ConnectException ex) { throw ex; } catch (IOException ex) { throw ex; } catch (Exception ex) { throw new WSDLException("WSDL4JWrapper : ", ex.getMessage(), ex); } }
From source file:JNLPAppletLauncher.java
private void validateCache(URLConnection conn, File nativeFile, File indexFile) throws IOException { // Lock the cache directory final String lckFileName = "cache.lck"; File lckFile = new File(cacheDir, lckFileName); lckFile.createNewFile();/* ww w. j av a2 s . c o m*/ final FileOutputStream lckOut = new FileOutputStream(lckFile); final FileChannel lckChannel = lckOut.getChannel(); final FileLock lckLock = lckChannel.lock(); try { // Check to see whether the cached jar file exists and is valid boolean valid = false; long cachedTimeStamp = readTimeStamp(indexFile); long urlTimeStamp = conn.getLastModified(); if (nativeFile.exists() && urlTimeStamp > 0 && urlTimeStamp == readTimeStamp(indexFile)) { valid = true; } // Validate the cache, download the jar if needed if (!valid) { if (VERBOSE) { System.err.println("processNativeJar: downloading " + nativeFile.getAbsolutePath()); } indexFile.delete(); nativeFile.delete(); // Copy from URL to File int len = conn.getContentLength(); if (VERBOSE) { System.err.println("Content length = " + len + " bytes"); } int totalNumBytes = copyURLToFile(conn, nativeFile); if (DEBUG) { System.err.println("processNativeJar: " + conn.getURL().toString() + " --> " + nativeFile.getAbsolutePath() + " : " + totalNumBytes + " bytes written"); } // Write timestamp to index file. writeTimeStamp(indexFile, urlTimeStamp); } else { if (DEBUG) { System.err .println("processNativeJar: using previously cached: " + nativeFile.getAbsolutePath()); } } } finally { // Unlock the cache directory lckLock.release(); } }