Example usage for java.net URL getPath

List of usage examples for java.net URL getPath

Introduction

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

Prototype

public String getPath() 

Source Link

Document

Gets the path part of this URL .

Usage

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.util.JarURLStreamHandler.java

protected String toExternalForm(URL u) {
    return URL_PREFIX + u.getPath() + SEPARATOR + u.getRef();
}

From source file:fr.eolya.utils.http.HttpUtils.java

public static String urlRemoveParameters(String url, String paramsToRemove) {
    if (paramsToRemove == null || "".equals(paramsToRemove))
        return url;

    try {/*from  w ww. ja  va 2 s.  c  o m*/
        URL u = new URL(url);
        if (u.getQuery() == null && u.getPath().indexOf(";jsessionid=") == -1)
            return url;
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
        return null;
    }

    try {

        url = url.replace("?&", "?");

        if ("*".equals(paramsToRemove)) {
            int offset = url.lastIndexOf("?");
            if (offset != -1)
                return url.substring(0, offset);
        }

        paramsToRemove = paramsToRemove.replaceAll(" ", "").replaceAll(";", ",");
        String[] aToRemove = paramsToRemove.split(",");
        String tempUrl = url;
        for (int i = 0; i < aToRemove.length; i++) {
            boolean found = true;
            while (found) {
                found = false;
                String re = "[?&;]" + aToRemove[i].toLowerCase() + "[=&]";
                Pattern p = Pattern.compile(re);
                Matcher m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
                re = "[?&;]" + aToRemove[i].toLowerCase() + "$";
                p = Pattern.compile(re);
                m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
            }
        }
        return tempUrl;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.mustardgrain.solr.SolrClient.java

private static Runnable getStatsRunner(final WeakReference<SolrClient> lbRef) {
    return new Runnable() {

        @Override/* w  w w  .j a v a2  s. com*/
        public void run() {
            SolrClient lb = lbRef.get();
            if (lb != null && lb.serverStats != null && LOG.isInfoEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("server responses over past ");
                sb.append(TimeUnit.MILLISECONDS.toMinutes(lb.statsInterval));
                sb.append(" mins (good/timeout/zero found) and cache hit ratio: ");
                boolean appendComma = false;

                for (Map.Entry<String, SolrStats> entry : lb.serverStats.entrySet()) {
                    if (appendComma)
                        sb.append(", ");

                    String server = entry.getKey();
                    SolrStats stats = entry.getValue();
                    String hitRatio = getHitRatio(server);

                    sb.append(server);
                    sb.append(": ");
                    sb.append(stats.getSuccesses());
                    sb.append("/");
                    sb.append(stats.getReadTimeouts());
                    sb.append("/");
                    sb.append(stats.getEmptyResults());
                    sb.append(" ");
                    sb.append(hitRatio);

                    appendComma = true;

                    stats.reset(); // Reset the counters once printed
                }

                LOG.info(sb);
            }
        }

        /**
         * Determines the hit ratio for the query result cache by calling
         * the SolrServer via JMX and reading its MBean containing that
         * information.
         * @param server Server from which to pull information
         * @return String form of hit ratio (either "n/a" or "xx%")
         */

        private String getHitRatio(String server) {
            String hitRatio = "n/a";
            JMXConnector jmxc = null;

            try {
                URL url = new URL(server);
                String domain = url.getPath();

                if (domain.startsWith("/"))
                    domain = domain.substring(1);

                ObjectName name = new ObjectName(
                        domain + ":id=org.apache.solr.search.LRUCache,type=queryResultCache");
                JMXServiceURL jmxUrl = new JMXServiceURL(
                        "service:jmx:rmi:///jndi/rmi://" + url.getHost() + ":7199/jmxrmi");
                jmxc = JMXConnectorFactory.connect(jmxUrl, null);
                MBeanServerConnection con = jmxc.getMBeanServerConnection();

                Object result = con.getAttribute(name, "hitratio");
                hitRatio = (int) (Float.parseFloat(String.valueOf(result)) * 100.0f) + "%";
            } catch (Exception e) {
                LOG.error(getNestedErrorMessages(e));
            } finally {
                if (jmxc != null) {
                    try {
                        jmxc.close();
                    } catch (Exception e) {
                        LOG.error(getNestedErrorMessages(e));
                    }
                }
            }

            return hitRatio;
        }

    };
}

From source file:com.genericdemo.enginedata.Tester.java

@Test
public void blah() throws MalformedURLException, IOException {
    URL schemaURL = new URL(
            "file:/Users/ashwin/code/generic/genericdemo/target/genericdemo-1.0-SNAPSHOT.jar!/engineDataEventSchema.json");
    URL localSchemaURL = new URL("file:///tmp/blah.txt");
    FileUtils.deleteQuietly(new File(localSchemaURL.getPath()));
    FileUtils.copyFile(new File(schemaURL.getPath()), new File(localSchemaURL.getPath()));

    Path schemaPath = new Path(schemaURL.toString());
    Path localPath = new Path(localSchemaURL.toString());
    System.out.println("jar $$$ = " + schemaPath);
    System.out.println("local $$$ = " + localPath);
}

From source file:org.robobninjas.riemann.spring.server.RiemannProcessConfiguration.java

private Path getConfig() {
    final URL resource = Resources.getResource(riemannConfigResourcePath);
    return Paths.get(resource.getPath());
}

From source file:com.cognifide.aet.executor.xmlparser.xml.utils.EscapeUtilsTest.java

private String readContentFromFile(String path) throws IOException {
    URL resourceURL = getClass().getResource(path);
    return IOUtils.toString(new FileInputStream(resourceURL.getPath()), "UTF-8");
}

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

/**
 * Find library file/directory from an element class.
 * @param aClass element class in target library
 * @return target file/directory, or {@code null} if not found
 * @throws IllegalArgumentException if some parameters were {@code null}
 *//* ww  w. ja  v a2  s  .  c om*/
public static File findLibraryPathFromClass(Class<?> aClass) {
    if (aClass == null) {
        throw new IllegalArgumentException("aClass must not be null"); //$NON-NLS-1$
    }
    int start = aClass.getName().lastIndexOf('.') + 1;
    String name = aClass.getName().substring(start);
    URL resource = aClass.getResource(name + ".class");
    if (resource == null) {
        LOG.warn("Failed to locate the class file: {}", aClass.getName());
        return null;
    }
    String protocol = resource.getProtocol();
    if (protocol.equals("file")) {
        try {
            File file = new File(resource.toURI());
            return toClassPathRoot(aClass, file);
        } catch (URISyntaxException e) {
            LOG.warn(
                    MessageFormat.format(
                            "Failed to locate the library path (cannot convert to local file): {0}", resource),
                    e);
            return null;
        }
    }
    if (protocol.equals("jar")) {
        String path = resource.getPath();
        return toClassPathRoot(aClass, path);
    } else {
        LOG.warn("Failed to locate the library path (unsupported protocol {}): {}", resource, aClass.getName());
        return null;
    }
}

From source file:fm.last.peyote.cacti.JmxInputProcessor.java

private String convertToCactiUrl(String urlString) throws MalformedURLException {
    URL url = new URL(urlString);
    return url.getProtocol() + "://<host>:" + url.getPort() + url.getPath();
}

From source file:com.local.ask.controller.mail.MailManagerImpl.java

private String getTemplate() {
    try {//from   w  w w . j  a  v  a  2 s. com
        URL url = getClass().getResource("mail.html");
        File file = new File(url.getPath());
        byte[] encoded = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
        return new String(encoded, "UTF-8");
    } catch (IOException ex) {
        Logger.getLogger(MailManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:eu.databata.engine.osgi.PropagatorOsgiInstance.java

public void setChanges(URL changesDir) throws IOException {
    this.changesDir = new File(changesDir.getPath());
}