Example usage for java.net URL getProtocol

List of usage examples for java.net URL getProtocol

Introduction

In this page you can find the example usage for java.net URL getProtocol.

Prototype

public String getProtocol() 

Source Link

Document

Gets the protocol name of this URL .

Usage

From source file:io.cloudine.rhq.plugins.worker.ResourceUtils.java

/**
 *  URL? Jar ? ?   ? ?. , "jar", "zip"?   ? ?. "zip"? WebLogic?  Jar ? .
 *
 * @param url  URL//from  w  w  w  .j av a2  s  .co  m
 * @return JAR ?? 
 */
public static boolean isJarURL(URL url) {
    String protocol = url.getProtocol();
    return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol));
}

From source file:net.dataforte.commons.resources.ClassUtils.java

/**
 * Returns all resources beneath a folder. Supports filesystem, JARs and JBoss VFS
 * //from   www. ja  v a 2s .c o  m
 * @param folder
 * @return
 * @throws IOException
 */
public static URL[] getResources(String folder) throws IOException {
    List<URL> urls = new ArrayList<URL>();
    ArrayList<File> directories = new ArrayList<File>();
    try {
        ClassLoader cld = Thread.currentThread().getContextClassLoader();
        if (cld == null) {
            throw new IOException("Can't get class loader.");
        }
        // Ask for all resources for the path
        Enumeration<URL> resources = cld.getResources(folder);
        while (resources.hasMoreElements()) {
            URL res = resources.nextElement();
            String resProtocol = res.getProtocol();
            if (resProtocol.equalsIgnoreCase("jar")) {
                JarURLConnection conn = (JarURLConnection) res.openConnection();
                JarFile jar = conn.getJarFile();
                for (JarEntry e : Collections.list(jar.entries())) {
                    if (e.getName().startsWith(folder) && !e.getName().endsWith("/")) {
                        urls.add(new URL(
                                joinUrl(res.toString(), "/" + e.getName().substring(folder.length() + 1)))); // FIXME: fully qualified name
                    }
                }
            } else if (resProtocol.equalsIgnoreCase("vfszip") || resProtocol.equalsIgnoreCase("vfs")) { // JBoss 5+
                try {
                    Object content = res.getContent();
                    Method getChildren = content.getClass().getMethod("getChildren");
                    List<?> files = (List<?>) getChildren.invoke(res.getContent());
                    Method toUrl = null;
                    for (Object o : files) {
                        if (toUrl == null) {
                            toUrl = o.getClass().getMethod("toURL");
                        }
                        urls.add((URL) toUrl.invoke(o));
                    }
                } catch (Exception e) {
                    throw new IOException("Error while scanning " + res.toString(), e);
                }
            } else if (resProtocol.equalsIgnoreCase("file")) {
                directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
            } else {
                throw new IOException("Unknown protocol for resource: " + res.toString());
            }
        }
    } catch (NullPointerException x) {
        throw new IOException(folder + " does not appear to be a valid folder (Null pointer exception)");
    } catch (UnsupportedEncodingException encex) {
        throw new IOException(folder + " does not appear to be a valid folder (Unsupported encoding)");
    }

    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (String file : files) {
                urls.add(new URL("file:///" + joinPath(directory.getAbsolutePath(), file)));
            }
        } else {
            throw new IOException(
                    folder + " (" + directory.getPath() + ") does not appear to be a valid folder");
        }
    }
    URL[] urlsA = new URL[urls.size()];
    urls.toArray(urlsA);
    return urlsA;
}

From source file:cn.org.rapid_framework.generator.util.ResourceHelper.java

/**
 * Determine whether the given URL points to a resource in the file system,
 * that is, has protocol "file" or "vfs".
 * @param url the URL to check//from w w  w. j  av a 2s  . c  o  m
 * @return whether the URL has been identified as a file system URL
 */
public static boolean isFileURL(URL url) {
    String protocol = url.getProtocol();
    return (URL_PROTOCOL_FILE.equals(protocol) || protocol.startsWith(URL_PROTOCOL_VFS));
}

From source file:cn.org.rapid_framework.generator.util.ResourceHelper.java

/**
 * Determine whether the given URL points to a resource in a jar file,
 * that is, has protocol "jar", "zip", "wsjar" or "code-source".
 * <p>"zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively,
 * but can be treated like jar files. The same applies to "code-source" URLs on Oracle
 * OC4J, provided that the path contains a jar separator.
 * @param url the URL to check/*from  w w  w  . ja  v  a2  s.  c  o  m*/
 * @return whether the URL has been identified as a JAR URL
 */
public static boolean isJarURL(URL url) {
    String protocol = url.getProtocol();
    return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol)
            || URL_PROTOCOL_WSJAR.equals(protocol)
            || (URL_PROTOCOL_CODE_SOURCE.equals(protocol) && url.getPath().contains(JAR_URL_SEPARATOR)));
}

From source file:com.dianping.resource.io.util.ResourceUtils.java

/**
 * Determine whether the given URL points to a resource in the file system,
 * that is, has protocol "file" or "vfs".
 * @param url the URL to check/*from   w  w  w.  j av  a 2 s  .  co  m*/
 * @return whether the URL has been identified as a file system URL
 */
public static boolean isFileURL(URL url) {
    String protocol = url.getProtocol();
    return (URL_PROTOCOL_FILE.equals(protocol) || URL_PROTOCOL_VFS.equals(protocol));
}

From source file:com.dianping.resource.io.util.ResourceUtils.java

/**
 * Determine whether the given URL points to a resource in a jar file,
 * that is, has protocol "jar", "zip", "wsjar" or "code-source".
 * <p>"zip" and "wsjar" are used by WebLogic Server and WebSphere, respectively,
 * but can be treated like jar files./*from  w w  w  . ja va2 s .  c om*/
 * @param url the URL to check
 * @return whether the URL has been identified as a JAR URL
 */
public static boolean isJarURL(URL url) {
    String protocol = url.getProtocol();
    return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol)
            || URL_PROTOCOL_WSJAR.equals(protocol) || URL_PROTOCOL_VFSZIP.equals(protocol));
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

public static File getFile(URL url) throws IOException {
    if (url == null) {
        return null;
    }/*w ww.  j  av  a 2s.  c  om*/
    if (PROTOCOL_FILE.equals(url.getProtocol()) || PROTOCOL_PLATFORM.equalsIgnoreCase(url.getProtocol())) {
        File file;
        try {
            file = new File(new URI(url.toExternalForm()));
        } catch (Exception e) {
            file = new File(url.getFile());
        }
        if (!file.exists()) {
            return null;
        }
        return file;
    }
    File file = getCachedFile(url);
    long urlLastModified = getLastModified(url);
    if (file.exists()) {
        long lastModified = file.lastModified();
        if (urlLastModified > lastModified) {
            file = download(file, url);
            if (file != null) {
                file.setLastModified(urlLastModified);
            }
        }
    } else {
        file = download(file, url);
        if (file != null && urlLastModified > -1) {
            file.setLastModified(urlLastModified);
        }
    }
    return file;
}

From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java

public static InputStream postConnection(Context context, URL url, String method, String urlParameters)
        throws IOException {
    if (!url.getProtocol().startsWith("http")) {
        return url.openStream();
    }/*from   w w w  .  j  av  a 2  s  .  co  m*/

    URLConnection c = url.openConnection();

    HttpURLConnection connection = (HttpURLConnection) c;

    connection.setRequestMethod(method.toUpperCase());
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");

    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    OutputStream output = null;
    try {
        output = connection.getOutputStream();
        output.write(urlParameters.getBytes("UTF-8"));
        output.flush();
    } finally {
        IOUtils.close(output);
    }
    return connection.getInputStream();
}

From source file:com.asakusafw.testdriver.DirectIoUtil.java

private static <T> DataModelSourceFactory load(Configuration configuration, DataModelDefinition<T> definition,
        DataFormat<? super T> format, URL source) throws IOException, InterruptedException {
    checkDataType(definition, format);//from w  w  w .  ja va  2 s.  c  o m
    if (source.getProtocol().equals("file")) { //$NON-NLS-1$
        File file = null;
        try {
            file = new File(source.toURI());
        } catch (URISyntaxException e) {
            LOG.debug("failed to convert URL into local file path: {}", source, e); //$NON-NLS-1$
        }
        if (file != null) {
            return load(configuration, definition, format, file);
        }
    }
    if (format instanceof BinaryStreamFormat<?>) {
        return load0(definition, (BinaryStreamFormat<? super T>) format, source);
    }
    HadoopFileFormat<? super T> hFormat = HadoopDataSourceUtil.toHadoopFileFormat(configuration, format);
    return load0(definition, hFormat, source);
}

From source file:org.duracloud.duradmin.spaces.controller.ContentItemController.java

public static String getBaseURL(HttpServletRequest request) throws MalformedURLException {
    URL url = new URL(request.getRequestURL().toString());
    int port = url.getPort();
    String baseURL = url.getProtocol() + "://" + url.getHost() + ":"
            + (port > 0 && port != 80 ? url.getPort() : "") + request.getContextPath();
    return baseURL;
}