List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:com.amalto.core.query.SystemStorageTest.java
private static Collection<String> getConfigFiles() throws Exception { URL data = InitDBUtil.class.getResource("data"); //$NON-NLS-1$ List<String> result = new ArrayList<String>(); if ("jar".equals(data.getProtocol())) { //$NON-NLS-1$ JarURLConnection connection = (JarURLConnection) data.openConnection(); JarEntry entry = connection.getJarEntry(); JarFile file = connection.getJarFile(); Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry e = entries.nextElement(); if (e.getName().startsWith(entry.getName()) && !e.isDirectory()) { result.add(IOUtils.toString(file.getInputStream(e))); }/*from w w w. jav a 2s .c om*/ } } else { Collection<File> files = FileUtils.listFiles(new File(data.toURI()), new IOFileFilter() { @Override public boolean accept(File file) { return true; } @Override public boolean accept(File file, String s) { return true; } }, new IOFileFilter() { @Override public boolean accept(File file) { return !".svn".equals(file.getName()); //$NON-NLS-1$ } @Override public boolean accept(File file, String s) { return !".svn".equals(file.getName()); //$NON-NLS-1$ } }); for (File f : files) { result.add(IOUtils.toString(new FileInputStream(f))); } } return result; }
From source file:com.odoko.solrcli.actions.CrawlPostAction.java
/** * Appends to the path of the URL// w w w. j ava 2s . c o m * @param url the URL * @param append the path to append * @return the final URL version */ protected static URL appendUrlPath(URL url, String append) throws MalformedURLException { return new URL(url.getProtocol() + "://" + url.getAuthority() + url.getPath() + append + (url.getQuery() != null ? "?"+url.getQuery() : "")); }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Finds all classes defined in a given package. * * @author paouelle/*from w ww . ja va 2 s . c o m*/ * * @param pkg the package from which to find all defined classes * @param cl the classloader to find the classes with * @return the non-<code>null</code> collection of all classes defined in the * given package * @throws NullPointerException if <code>pkg</code> or <code>cl</code> is * <code>null</code> */ public static Collection<Class<?>> findClasses(String pkg, ClassLoader cl) { org.apache.commons.lang3.Validate.notNull(pkg, "invalid null pkg"); final String scannedPath = pkg.replace('.', File.separatorChar); final Enumeration<URL> resources; try { resources = cl.getResources(scannedPath); } catch (IOException e) { throw new IllegalArgumentException("Unable to get resources from path '" + scannedPath + "'. Are you sure the given '" + pkg + "' package exists?", e); } final List<Class<?>> classes = new LinkedList<>(); while (resources.hasMoreElements()) { final URL url = resources.nextElement(); if ("jar".equals(url.getProtocol())) { ReflectionUtils.findClassesFromJar(classes, url, scannedPath, cl); } else if ("file".equals(url.getProtocol())) { final File file = new File(url.getFile()); ReflectionUtils.findClassesFromFile(classes, file, pkg, cl); } else { throw new IllegalArgumentException("package is provided by an unknown url: " + url); } } return classes; }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Finds all resources defined in a given package. * * @author paouelle//w w w . j a v a 2s .c om * * @param pkg the package from which to find all defined resources * @param cl the classloader to find the resources with * @return the non-<code>null</code> collection of all resources defined in the * given package * @throws NullPointerException if <code>pkg</code> or <code>cl</code> is * <code>null</code> */ public static Collection<URL> findResources(String pkg, ClassLoader cl) { org.apache.commons.lang3.Validate.notNull(pkg, "invalid null pkg"); final String scannedPath = pkg.replace('.', File.separatorChar); final Enumeration<URL> resources; try { resources = cl.getResources(scannedPath); } catch (IOException e) { throw new IllegalArgumentException("Unable to get resources from path '" + scannedPath + "'. Are you sure the given '" + pkg + "' package exists?", e); } final List<URL> urls = new LinkedList<>(); while (resources.hasMoreElements()) { final URL url = resources.nextElement(); if ("jar".equals(url.getProtocol())) { ReflectionUtils.findResourcesFromJar(urls, url, scannedPath, cl); } else if ("file".equals(url.getProtocol())) { final File file = new File(url.getFile()); ReflectionUtils.findResourcesFromFile(urls, file, scannedPath, cl); } else { throw new IllegalArgumentException("package is provided by an unknown url: " + url); } } return urls; }
From source file:net.sf.firemox.tools.Picture.java
/** * Download a file from the specified URL to the specified local file. * //ww w . j ava 2s. c o m * @param localFile * is the new card's picture to try first * @param remoteFile * is the URL where this picture will be downloaded in case of the * specified card name has not been found locally. * @param listener * the component waiting for this picture. * @since 0.83 Empty file are deleted to force file to be downloaded. */ public static synchronized void download(String localFile, URL remoteFile, MonitoredCheckContent listener) { BufferedOutputStream out = null; BufferedInputStream in = null; File toDownload = new File(localFile); if (toDownload.exists() && toDownload.length() == 0 && toDownload.canWrite()) { toDownload.delete(); } if (!toDownload.exists() || (toDownload.length() == 0 && toDownload.canWrite())) { // the file has to be downloaded try { if ("file".equals(remoteFile.getProtocol())) { File localRemoteFile = MToolKit .getFile(remoteFile.toString().substring(7).replaceAll("%20", " "), false); int contentLength = (int) localRemoteFile.length(); Log.info("Copying from " + localRemoteFile.getAbsolutePath()); LoaderConsole.beginTask( LanguageManager.getString("downloading") + " " + localRemoteFile.getAbsolutePath() + "(" + FileUtils.byteCountToDisplaySize(contentLength) + ")"); // Copy file in = new BufferedInputStream(new FileInputStream(localRemoteFile)); byte[] buf = new byte[2048]; int currentLength = 0; boolean succeed = false; for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) { if (!succeed) { toDownload.getParentFile().mkdirs(); out = new BufferedOutputStream(new FileOutputStream(localFile)); succeed = true; } currentLength += bufferLen; if (out != null) { out.write(buf, 0, bufferLen); } if (listener != null) { listener.updateProgress(contentLength, currentLength); } } // Step 3: close streams IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); in = null; out = null; return; } // Testing mode? if (!MagicUIComponents.isUILoaded()) { return; } // Step 1: open streams final URLConnection connection = MToolKit.getHttpConnection(remoteFile); int contentLength = connection.getContentLength(); in = new BufferedInputStream(connection.getInputStream()); Log.info("Download from " + remoteFile + "(" + FileUtils.byteCountToDisplaySize(contentLength) + ")"); LoaderConsole.beginTask(LanguageManager.getString("downloading") + " " + remoteFile + "(" + FileUtils.byteCountToDisplaySize(contentLength) + ")"); // Step 2: read and write until done byte[] buf = new byte[2048]; int currentLength = 0; boolean succeed = false; for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) { if (!succeed) { toDownload.getParentFile().mkdirs(); out = new BufferedOutputStream(new FileOutputStream(localFile)); succeed = true; } currentLength += bufferLen; if (out != null) { out.write(buf, 0, bufferLen); } if (listener != null) { listener.updateProgress(contentLength, currentLength); } } // Step 3: close streams IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); in = null; out = null; return; } catch (IOException e1) { if (MToolKit.getFile(localFile) != null) { MToolKit.getFile(localFile).delete(); } if (remoteFile.getFile().equals(remoteFile.getFile().toLowerCase())) { Log.fatal("could not load picture " + localFile + " from URL " + remoteFile + ", " + e1.getMessage()); } String tmpRemote = remoteFile.toString().toLowerCase(); try { download(localFile, new URL(tmpRemote), listener); } catch (MalformedURLException e) { Log.fatal("could not load picture " + localFile + " from URL " + tmpRemote + ", " + e.getMessage()); } } } }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified port. * @param u the URL on which to base the returned URL * @param newPort the new port to use in the returned URL * @return a new URL identical to the specified URL, except using the specified port * @throws MalformedURLException if there is a problem creating the new URL *//*from w w w.j a va 2s . c om*/ public static URL getUrlWithNewPort(final URL u, final int newPort) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getUserInfo(), u.getHost(), newPort, u.getPath(), u.getRef(), u.getQuery()); }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified reference. * @param u the URL on which to base the returned URL * @param newRef the new reference to use in the returned URL * @return a new URL identical to the specified URL, except using the specified reference * @throws MalformedURLException if there is a problem creating the new URL *//*from w ww.jav a 2 s . c o m*/ public static URL getUrlWithNewRef(final URL u, final String newRef) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getAuthority(), u.getPath(), newRef, u.getQuery()); }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified path. * @param u the URL on which to base the returned URL * @param newPath the new path to use in the returned URL * @return a new URL identical to the specified URL, except using the specified path * @throws MalformedURLException if there is a problem creating the new URL *//*from w w w.jav a 2 s . c om*/ public static URL getUrlWithNewPath(final URL u, final String newPath) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getAuthority(), newPath, u.getRef(), u.getQuery()); }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified host. * @param u the URL on which to base the returned URL * @param newHost the new host to use in the returned URL * @return a new URL identical to the specified URL, except using the specified host * @throws MalformedURLException if there is a problem creating the new URL */// w w w . j a v a 2 s .com public static URL getUrlWithNewHost(final URL u, final String newHost) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getUserInfo(), newHost, u.getPort(), u.getPath(), u.getRef(), u.getQuery()); }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified query string. * @param u the URL on which to base the returned URL * @param newQuery the new query string to use in the returned URL * @return a new URL identical to the specified URL, except using the specified query string * @throws MalformedURLException if there is a problem creating the new URL *//*from w ww. j a v a 2 s. co m*/ public static URL getUrlWithNewQuery(final URL u, final String newQuery) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getAuthority(), u.getPath(), u.getRef(), newQuery); }