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:com.grendelscan.commons.http.CookieJar.java

public List<Cookie> getMatchingCookies(final URL url) {
    int port = url.getPort();
    if (port < 0) {
        port = url.getDefaultPort();/*from  w w w. ja  v a  2s.c om*/
    }
    CookieOrigin origin = new CookieOrigin(url.getHost(), port, url.getPath(),
            url.getProtocol().equalsIgnoreCase("https"));
    return getMatchingCookies(origin);
}

From source file:com.googlesource.gerrit.plugins.github.oauth.OAuthGitFilter.java

private URI getRequestUrlWithAlternatePath(HttpServletRequest httpRequest, String alternatePath)
        throws MalformedURLException {
    URL originalUrl = new URL(httpRequest.getRequestURL().toString());
    String contextPath = httpRequest.getContextPath();
    return URI.create(originalUrl.getProtocol() + "://" + originalUrl.getHost() + ":" + getPort(originalUrl)
            + contextPath + alternatePath);
}

From source file:com.jaspersoft.studio.server.protocol.restv2.RestV2Connection.java

@Override
public boolean connect(IProgressMonitor monitor, ServerProfile sp) throws Exception {
    this.sp = sp;

    URL url = sp.getURL();
    HttpHost host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());

    exec = Executor.newInstance().auth(host, sp.getUser(), Pass.getPass(sp.getPass()));
    exec.authPreemptive(host);//  www  .j a  v a2s .  co m
    net.sf.jasperreports.eclipse.util.HttpUtils.setupProxy(exec, url.toURI());
    getServerInfo(monitor);

    return true;
}

From source file:com.snowplowanalytics.refererparser.Parser.java

public Referer parse(URL refererUrl, String pageHost) {
    if (refererUrl == null) {
        return null;
    }/*from  www.  j  a  v a2  s .c om*/
    return parse(refererUrl.getProtocol(), refererUrl.getHost(), refererUrl.getPath(), refererUrl.getQuery(),
            pageHost);
}

From source file:appeng.recipes.loader.RecipeResourceCopier.java

/**
 * List directory contents for a resource folder. Not recursive. This is basically a brute-force implementation. Works for regular files and also JARs.
 *
 * @param clazz Any java class that lives in the same place as the resources you want.
 * @param path Should end with "/", but not start with one.
 *
 * @return Just the name of each member item, not the full paths.
 *
 * @throws URISyntaxException            if it is a file path and the URL can not be converted to URI
 * @throws IOException                   if jar path can not be decoded
 * @throws UnsupportedOperationException if it is neither in jar nor in file path
 *///www  . j  a  va 2 s  . com
@Nonnull
private String[] getResourceListing(@Nonnull final Class<?> clazz, @Nonnull final String path)
        throws URISyntaxException, IOException {
    assert clazz != null;
    assert path != null;

    final ClassLoader classLoader = clazz.getClassLoader();
    if (classLoader == null) {
        throw new IllegalStateException(
                "ClassLoader was not found. It was probably loaded at a inappropriate time");
    }

    URL dirURL = classLoader.getResource(path);
    if (dirURL != null) {
        final String protocol = dirURL.getProtocol();
        if (protocol.equals(FILE_PROTOCOL)) {
            // A file path: easy enough

            final URI uriOfURL = dirURL.toURI();
            final File fileOfURI = new File(uriOfURL);
            final String[] filesAndDirectoriesOfURI = fileOfURI.list();

            if (filesAndDirectoriesOfURI == null) {
                throw new IllegalStateException(
                        "Files and Directories were illegal. Either an abstract pathname does not denote a directory, or an I/O error occured.");
            } else {
                return filesAndDirectoriesOfURI;
            }
        }
    }

    if (dirURL == null) {
        /*
         * In case of a jar file, we can't actually find a directory.
         * Have to assume the same jar as clazz.
         */
        final String className = clazz.getName();
        final Matcher matcher = DOT_COMPILE_PATTERN.matcher(className);
        final String me = matcher.replaceAll("/") + CLASS_EXTENSION;
        dirURL = classLoader.getResource(me);
    }

    if (dirURL != null) {
        final String protocol = dirURL.getProtocol();
        if (protocol.equals(JAR_PROTOCOL)) {
            /* A JAR path */
            final String dirPath = dirURL.getPath();
            final String jarPath = dirPath.substring(5, dirPath.indexOf('!')); // strip out only
            // the JAR file
            final JarFile jar = new JarFile(URLDecoder.decode(jarPath, UTF_8_ENCODING));
            try {
                final Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries in jar
                final Collection<String> result = new HashSet<String>(INITIAL_RESOURCE_CAPACITY); // avoid duplicates

                // in case it is a
                // subdirectory
                while (entries.hasMoreElements()) {
                    final JarEntry entry = entries.nextElement();
                    final String entryFullName = entry.getName();
                    if (entryFullName.startsWith(path)) { // filter according to the path
                        String entryName = entryFullName.substring(path.length());
                        final int checkSubDir = entryName.indexOf('/');
                        if (checkSubDir >= 0) {
                            // if it is a subdirectory, we just return the directory name
                            entryName = entryName.substring(0, checkSubDir);
                        }
                        result.add(entryName);
                    }
                }

                return result.toArray(new String[result.size()]);
            } finally {
                jar.close();
            }
        }
    }

    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}

From source file:com.gargoylesoftware.htmlunit.CookieManager.java

/**
 * Helper that builds a CookieOrigin.//from   w ww  .j  a v  a 2 s.  c o m
 * @param url the url to be used
 * @return the new CookieOrigin
 */
public CookieOrigin buildCookieOrigin(final URL url) {
    final URL normalizedUrl = replaceForCookieIfNecessary(url);

    return new CookieOrigin(normalizedUrl.getHost(), getPort(normalizedUrl), normalizedUrl.getPath(),
            "https".equals(normalizedUrl.getProtocol()));
}

From source file:io.cloudslang.content.utilities.services.osdetector.NmapOsDetectorService.java

@NotNull
public String appendProxyArgument(String nmapArguments, String proxyHost, String proxyPort) {
    try {/*  w ww .ja  v a2s .c  om*/
        URL proxyUrl = new URL(proxyHost);
        if (!isValidIpPort(proxyPort)) {
            throw new IllegalArgumentException(
                    format("The '%s' input does not contain a valid port.", PROXY_PORT));
        }
        nmapArguments += " --proxies " + proxyUrl.getProtocol() + "://" + proxyUrl.getHost() + ":" + proxyPort;
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(
                format("The '%s' input does not contain a valid URL: %s.", PROXY_HOST, e.getMessage()));
    }
    return nmapArguments;
}

From source file:net.mojodna.sprout.SproutAutoLoaderPlugIn.java

public void autoloadClasses(final WebApplicationContext wac) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    if (loader instanceof URLClassLoader) {
        URL[] cp = ((URLClassLoader) loader).getURLs();

        for (int i = 0; i < cp.length; i++) {
            URL url = cp[i];

            if (url.getProtocol().equals("file")) {
                String pathname = url.getFile();
                File file = new File(pathname);

                if (file.isDirectory()) {
                    autoloadFromDirectory(loader, pathname.length(), file);
                }/*from   www.  j  a v a 2s.  com*/
            }
        }
    }
}

From source file:JMeter.plugins.functional.samplers.geoevent.HttpJsonToStreamServiceSampler.java

private void setupTarget() {
    try {//www.ja v  a 2  s. c  om
        // Send the Message to Rest Endpoint
        String urlString = getRestInputURL();
        //System.out.println(urlString);

        URL url = new URL(urlString);

        Client client;

        if (url.getProtocol().equalsIgnoreCase("https")) {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                }
            } };
            SSLContext sc = null;

            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            client = ClientBuilder.newBuilder().sslContext(sc).build();
        } else {
            client = ClientBuilder.newClient();
        }

        target = client.target(urlString);
    } catch (MalformedURLException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    } catch (NoSuchAlgorithmException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    } catch (KeyManagementException ex) {
        java.util.logging.Logger.getLogger(HttpJsonToStreamServiceSampler.class.getName()).log(Level.SEVERE,
                null, ex);
        ex.printStackTrace();
    }
}