Example usage for java.net URL getRef

List of usage examples for java.net URL getRef

Introduction

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

Prototype

public String getRef() 

Source Link

Document

Gets the anchor (also known as the "reference") of this URL .

Usage

From source file:org.talend.core.runtime.maven.MavenUrlHelper.java

public static String generateMvnUrl(String username, String password, String repositoryId, String groupId,
        String artifactId, String version, String packaging, String classifier, boolean encryptPassword) {
    Assert.isNotNull(groupId);/* w w  w. ja  v  a 2s .  com*/
    Assert.isNotNull(artifactId);

    StringBuffer mvnUrl = new StringBuffer(100);
    mvnUrl.append(MVN_PROTOCOL);

    if (StringUtils.isNotEmpty(repositoryId)) {
        String repositoryUrl = repositoryId;
        if (StringUtils.isNotEmpty(username)) {
            if (password == null) {
                password = "";
            }
            if (encryptPassword) {
                password = encryptPassword(password);
            }
            String usernamePassword = username + USER_PASSWORD_SPLITER + password;
            try {
                URL repoWithoutUserPasswordUrl = new URL(repositoryId);
                if (repoWithoutUserPasswordUrl != null) {
                    if (StringUtils.isEmpty(repoWithoutUserPasswordUrl.getHost())) {
                        throw new Exception("Bad url, can't resolve it: " + repositoryId);
                    } else {
                        URI repoWithUserPasswordURI = new URI(repoWithoutUserPasswordUrl.getProtocol(),
                                usernamePassword, repoWithoutUserPasswordUrl.getHost(),
                                repoWithoutUserPasswordUrl.getPort(), repoWithoutUserPasswordUrl.getPath(),
                                repoWithoutUserPasswordUrl.getQuery(), repoWithoutUserPasswordUrl.getRef());
                        repositoryUrl = repoWithUserPasswordURI.toString();
                    }
                }
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
        mvnUrl.append(repositoryUrl).append(REPO_SEPERATOR);
    }

    mvnUrl.append(groupId);
    mvnUrl.append(SEPERATOR);
    mvnUrl.append(artifactId);

    if (version != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(version);
    } else {
        if (packaging != null || classifier != null) { // if has packaging or classifier
            // add one empty seperator
            mvnUrl.append(SEPERATOR);
        }
    }
    if (packaging != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(packaging);
    } else {
        if (classifier != null) { // if has classifier
            // add one empty seperator
            mvnUrl.append(SEPERATOR);
        }
    }
    if (classifier != null) {
        mvnUrl.append(SEPERATOR);
        mvnUrl.append(classifier);
    }

    return mvnUrl.toString();
}

From source file:com.mashape.unirest.request.HttpRequest.java

private URL parseUrl(String s) throws Exception {
    URL u = new URL(s);
    return new URI(u.getProtocol(), u.getAuthority(), u.getPath(), u.getQuery(), u.getRef()).toURL();
}

From source file:com.digitalpebble.storm.crawler.filtering.basic.BasicURLNormalizer.java

@Override
public String filter(URL sourceUrl, Metadata sourceMetadata, String urlToFilter) {
    if (removeAnchorPart) {
        try {//from   w w w. j  a v a 2  s .  com
            URL theURL = new URL(urlToFilter);
            String anchor = theURL.getRef();
            if (anchor != null)
                urlToFilter = urlToFilter.replace("#" + anchor, "");
        } catch (MalformedURLException e) {
            return null;
        }
    }

    if (unmangleQueryString) {
        urlToFilter = unmangleQueryString(urlToFilter);
    }

    if (!queryElementsToRemove.isEmpty()) {
        urlToFilter = filterQueryElements(urlToFilter);
    }

    return urlToFilter;
}

From source file:com.esri.gpt.framework.http.crawl.HttpCrawlRequest.java

private String getRelativePath() throws MalformedURLException {
    URL u = new URL(getUrl());
    return String.format("%s%s%s", u.getPath() != null ? u.getPath() : "/",
            u.getQuery() != null ? "?" + u.getQuery() : "", u.getRef() != null ? "#" + u.getRef() : "");
}

From source file:org.apache.struts.util.RequestUtils.java

/**
 * <p>Compute the printable representation of a URL, leaving off the
 * scheme/host/port part if no host is specified. This will typically be
 * the case for URLs that were originally created from relative or
 * context-relative URIs.</p>/*from ww  w .  ja  v a  2  s. c om*/
 *
 * @param url URL to render in a printable representation
 * @return printable representation of a URL
 */
public static String printableURL(URL url) {
    if (url.getHost() != null) {
        return (url.toString());
    }

    String file = url.getFile();
    String ref = url.getRef();

    if (ref == null) {
        return (file);
    } else {
        StringBuffer sb = new StringBuffer(file);

        sb.append('#');
        sb.append(ref);

        return (sb.toString());
    }
}

From source file:net.hillsdon.reviki.wiki.renderer.creole.CreoleLinkContentsSplitter.java

/**
 * Splits links of the form target or text|target where target is
 * //from   w  w w  . jav a  2s.c  om
 * PageName wiki:PageName PageName#fragment wiki:PageName#fragment
 * A String representing an absolute URI scheme://valid/absolute/uri
 * Any character not in the `unreserved`, `punct`, `escaped`, or `other` categories (RFC 2396),
 * and not equal '/' or '@', is %-encoded. 
 * 
 * @param in The String to split
 * @return The split LinkParts
 */
LinkParts split(final String in) {
    String target = StringUtils.trimToEmpty(StringUtils.substringBefore(in, "|"));
    String text = StringUtils.trimToNull(StringUtils.substringAfter(in, "|"));
    if (target == null) {
        target = "";
    }
    if (text == null) {
        text = target;
    }
    // Link target can be PageName, wiki:PageName or a URL.
    URI uri = null;
    try {
        URL url = new URL(target);

        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        if (uri.getPath() == null || !uri.isAbsolute()) {
            uri = null;
        }
    } catch (URISyntaxException e) {
        // uri remains null
    } catch (MalformedURLException e) {
        // uri remains null
    }

    if (uri != null) {
        return new LinkParts(text, uri);
    } else {
        // Split into wiki:pageName
        String[] parts = target.split(":", 2);
        String wiki = null;
        String pageName = target;
        if (parts.length == 2) {
            wiki = parts[0];
            pageName = parts[1];
        }

        // Split into pageName#fragment
        parts = pageName.split("#", 2);
        String fragment = null;
        if (parts.length == 2) {
            pageName = parts[0];
            fragment = parts[1];
        }

        // Split into pageName/attachment
        parts = pageName.split("/", 2);
        String attachment = null;
        if (parts.length == 2) {
            pageName = parts[0];
            attachment = parts[1];
        }

        return new LinkParts(text, wiki, pageName, fragment, attachment);
    }
}

From source file:org.squidy.manager.scanner.PackageScanner.java

/**
 * TODO: out-source me to a FileUtility or FileUtils helper class.
 * /*from   w ww .ja  v a  2 s  . c o m*/
 * @param url
 * @return
 */
private static File urlToFile(URL url) {
    URI uri;
    try {
        // this is the step that can fail, and so
        // it should be this step that should be fixed
        uri = url.toURI();
    } catch (URISyntaxException e) {
        // OK if we are here, then obviously the URL did
        // not comply with RFC 2396. This can only
        // happen if we have illegal unescaped characters.
        // If we have one unescaped character, then
        // the only automated fix we can apply, is to assume
        // all characters are unescaped.
        // If we want to construct a URI from unescaped
        // characters, then we have to use the component
        // constructors:
        try {
            uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
        } catch (URISyntaxException e1) {
            // The URL is broken beyond automatic repair
            throw new IllegalArgumentException("broken URL: " + url);
        }

    }
    return new File(uri);
}

From source file:org.dasein.cloud.azure.platform.AzureSQLDatabaseSupportRequests.java

private String getEncodedUri(String urlString) throws InternalException {
    try {/*  www . j a  v a  2 s.c  om*/
        URL url = new URL(urlString);
        return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef()).toString();
    } catch (Exception e) {
        throw new InternalException(e.getMessage());
    }
}

From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java

public static XSModel getXSModel(String fileName) throws URISyntaxException, MalformedURLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*  ww w . j a v a2  s.c om*/
    URI uri = null;
    File f = new File(fileName);
    if (f.exists()) {
        uri = f.toURI();
    } else {
        URL url = new URL(fileName);
        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    }

    // Then try to parse the input string as a url in web.
    if (uri == null) {
        uri = new URI(fileName);
    }

    // fixed a bug when parse one file contians Franch ,maybe need modification
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(uri.toString());
    if (xsModel == null) {
        try {
            Grammar loadGrammar = xsLoader.loadGrammar(
                    new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1"));
            xsModel = ((XSGrammar) loadGrammar).toXSModel();
        } catch (XNIException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return xsModel;
}

From source file:com.adito.replacementproxy.ProxyReplacer.java

public String getProxiedPath(URL context, String path, String base) {
    // TODO leave alone?
    if (path.startsWith("#")) {
        return path;
    }//from   w  ww .  j  a va 2  s.co  m

    // Some types we do not want to proxy at all
    String lc = path.toLowerCase();
    if (lc.startsWith("mailto:") || lc.startsWith("javascript:") || lc.startsWith("ftp:")
            || lc.startsWith("news")) {
        return path;
    }

    // Strip the reference of the path (either as a URL or a relative path)
    // and
    // store it for
    // later appending to the proxied path
    URL pathURL = null;
    String ref = "";
    try {
        pathURL = new URL(path);
        if (pathURL.getRef() != null) {
            ref = "#" + pathURL.getRef();
            int idx = path.lastIndexOf('#');
            path = path.substring(0, idx);
            pathURL = new URL(path);
        }
    } catch (MalformedURLException murle) {

        int idx = path.lastIndexOf('#');
        if (idx != -1) {
            ref = "#" + path.substring(idx + 1);
            path = path.substring(0, idx);
        }
    }

    // The web forward may restrict access to the target URL
    //        if (requestProcessor.getWebForward().getRestrictToURL()) {
    //            try {
    //                URL webForwardURL = new URL(requestProcessor.getWebForward().getDestinationURL());
    //                if (!(InetAddress.getByName(pathURL.getHost()).equals(InetAddress.getByName(webForwardURL.getHost())))) {
    //                    throw new Exception("Restricted.");
    //                }
    //            } catch (Exception e) {
    //                return "javascript: void();";
    //            }
    //
    //        }

    /**
     * LDP - We have to unescape any HTML entities because our client will not process them
     * otherwise when a request is received.
     */
    String newPath = null;
    try {
        if (base != null) {
            if (pathURL != null) {
                //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(pathURL.toExternalForm())) + ref;
                newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/"
                        + Util.urlEncode(Utils.htmlunescape(pathURL.toExternalForm())) + ref;
            } else {
                // Relative so we need to prepend the base
                if (path.startsWith("./")) {
                    path = path.substring(2);
                }
                try {
                    URL baseURL = new URL(base);
                    URL actual = new URL(baseURL, path);
                    //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
                    newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/"
                            + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
                } catch (MalformedURLException murle) {
                    log.error("Invalidate base URL.", murle);
                }
            }
        } else {
            URL actual = new URL(context, path);
            //newPath = "/replacementProxyEngine?" + LaunchSession.LONG_LAUNCH_ID + "=" + requestProcessor.getLaunchId() + "&sslex_url=" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
            newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/"
                    + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref;
        }
    } catch (MalformedURLException ex) {
        log.error("Could not convert path from '" + path + "' using " + context.toExternalForm(), ex);
        newPath = path;
    }
    if (newPath == null) {
        log.warn("Failed to proxy path " + path + ", using original.");
        newPath = path;
    }
    if (log.isDebugEnabled())
        log.debug("Created proxy path " + newPath);

    // Unescape the last encoded path
    int idx = newPath.lastIndexOf("%2F");
    newPath = newPath.substring(0, idx) + "/" + newPath.substring(idx + 3);

    return newPath;
}