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.apache.flink.table.runtime.functions.SqlFunctionUtils.java

/**
 * Parse url and return various components of the URL.
 * If accept any null arguments, return null.
 *
 * @param urlStr        URL string./*  ww w  . j  a  v a2 s .  c  o  m*/
 * @param partToExtract determines which components would return.
 *                      accept values:
 *                      HOST,PATH,QUERY,REF,
 *                      PROTOCOL,FILE,AUTHORITY,USERINFO
 * @return target value.
 */
public static String parseUrl(String urlStr, String partToExtract) {
    URL url;
    try {
        url = URL_CACHE.get(urlStr);
    } catch (Exception e) {
        LOG.error("Parse URL error: " + urlStr, e);
        return null;
    }
    if ("HOST".equals(partToExtract)) {
        return url.getHost();
    }
    if ("PATH".equals(partToExtract)) {
        return url.getPath();
    }
    if ("QUERY".equals(partToExtract)) {
        return url.getQuery();
    }
    if ("REF".equals(partToExtract)) {
        return url.getRef();
    }
    if ("PROTOCOL".equals(partToExtract)) {
        return url.getProtocol();
    }
    if ("FILE".equals(partToExtract)) {
        return url.getFile();
    }
    if ("AUTHORITY".equals(partToExtract)) {
        return url.getAuthority();
    }
    if ("USERINFO".equals(partToExtract)) {
        return url.getUserInfo();
    }

    return null;
}

From source file:com.cladonia.xngreditor.URLUtilities.java

/**
 * Encrypts the password in this URL.//from  ww  w  . jav a  2  s . co  m
 * 
 * @param url the url.
 * 
 * @return the encrypted string.
 */
public static String encrypt(URL url) {
    String password = getPassword(url);
    String username = getUsername(url);

    if (password != null) {
        password = StringUtilities.encrypt(password);
    }

    //      System.out.println( "URLUtilities.encrypt( "+url+") ["+password+"]");

    return createURL(url.getProtocol(), username, password, url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef()).toString();
}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDTFParseUrlTuple.java

private String evaluate(URL url, int index) {
    if (url == null || index < 0 || index >= partnames.length) {
        return null;
    }/*w ww .j  a  va  2  s .c o  m*/

    switch (partnames[index]) {
    case HOST:
        return url.getHost();
    case PATH:
        return url.getPath();
    case QUERY:
        return url.getQuery();
    case REF:
        return url.getRef();
    case PROTOCOL:
        return url.getProtocol();
    case FILE:
        return url.getFile();
    case AUTHORITY:
        return url.getAuthority();
    case USERINFO:
        return url.getUserInfo();
    case QUERY_WITH_KEY:
        return evaluateQuery(url.getQuery(), paths[index]);
    case NULLNAME:
    default:
        return null;
    }
}

From source file:org.sakaiproject.shortenedurl.impl.RandomisedUrlService.java

/**
 * Encodes a full URL.//w  w  w  .j av  a 2s .  c o m
 * 
 * @param rawUrl the URL to encode.
 */
private String encodeUrl(String rawUrl) {
    if (StringUtils.isBlank(rawUrl)) {
        return null;
    }
    String encodedUrl = null;

    try {
        URL url = new URL(rawUrl);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        encodedUrl = uri.toASCIIString();
    } catch (Exception e) {
        log.warn("encoding url: " + rawUrl + ", " + e.getMessage(), e);
    }

    return encodedUrl;
}

From source file:eionet.cr.web.action.HarvestSourceActionBean.java

/**
 *
 * @return boolean//from  www.j  a v a 2s .  com
 */
public boolean validateAddEdit() {

    if (isPostRequest()) {

        String urlString = harvestSource.getUrl();
        if (StringUtils.isBlank(urlString)) {
            addGlobalValidationError(new SimpleError("URL missing!"));
        } else {
            try {
                URL url = new URL(urlString);
                if (url.getRef() != null) {
                    addGlobalValidationError(new SimpleError("URL with a fragment part not allowed!"));
                }

                if (!StringUtils.equals(urlBefore, urlString)
                        && URLUtil.isNotExisting(urlString, harvestSource.isSparqlEndpoint())) {
                    addGlobalValidationError(new SimpleError("There is no resource existing behind this URL!"));
                }

                Integer intervalMinutes = harvestSource.getIntervalMinutes();
                if (intervalMinutes != null) {
                    if (intervalMinutes.intValue() < 0 || intervalMultiplier < 0) {
                        addGlobalValidationError(new SimpleError("Harvest interval must be >= 0"));
                    } else {
                        harvestSource.setIntervalMinutes(
                                Integer.valueOf(intervalMinutes.intValue() * intervalMultiplier));
                    }
                } else {
                    harvestSource.setIntervalMinutes(Integer.valueOf(0));
                }
            } catch (MalformedURLException e) {
                addGlobalValidationError(new SimpleError("Invalid URL!"));
            }
        }
    }

    return getContext().getValidationErrors() == null || getContext().getValidationErrors().isEmpty();
}

From source file:com.cladonia.xngreditor.URLUtilities.java

/**
 * Decrypts the string and creates a URL.
 * //w ww. ja  v a2s.  co  m
 * @param url the url.
 * 
 * @return the decrypted url or null if the url could not be constructed.
 */
public static URL decrypt(String string) {
    try {
        URL url = new URL(string);

        String password = getPassword(url);
        String username = getUsername(url);

        if (password != null) {
            password = StringUtilities.decrypt(password);
        }

        //         System.out.println( "URLUtilities.decrypt( "+string+") ["+password+"]");

        return createURL(url.getProtocol(), username, password, url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    } catch (MalformedURLException e) {
        // ignore
    }

    return null;
}

From source file:org.springframework.richclient.image.Handler.java

protected URLConnection openConnection(URL url) throws IOException {
    if (!StringUtils.hasText(url.getPath())) {
        throw new MalformedURLException("must provide an image key.");
    } else if (StringUtils.hasText(url.getHost())) {
        throw new MalformedURLException("host part should be empty.");
    } else if (url.getPort() != -1) {
        throw new MalformedURLException("port part should be empty.");
    } else if (StringUtils.hasText(url.getQuery())) {
        throw new MalformedURLException("query part should be empty.");
    } else if (StringUtils.hasText(url.getRef())) {
        throw new MalformedURLException("ref part should be empty.");
    } else if (StringUtils.hasText(url.getUserInfo())) {
        throw new MalformedURLException("user info part should be empty.");
    }/* w w  w  . j  ava 2  s  . c o m*/
    urlHandlerImageSource.getImage(url.getPath());
    Resource image = urlHandlerImageSource.getImageResource(url.getPath());
    if (image != null)
        return image.getURL().openConnection();

    throw new IOException("null image returned for key [" + url.getFile() + "].");
}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * <p>Encodes illegal characters in the specified URL's path, query string and anchor according to the URL
 * encoding rules observed in real browsers.</p>
 *
 * <p>For example, this method changes <tt>"http://first/?a=b c"</tt> to <tt>"http://first/?a=b%20c"</tt>.</p>
 *
 * @param url the URL to encode//from   w w  w.  ja v a 2 s .  c o m
 * @param minimalQueryEncoding whether or not to perform minimal query encoding, like IE does
 * @param charset the charset
 * @return the encoded URL
 */
public static URL encodeUrl(final URL url, final boolean minimalQueryEncoding, final String charset) {
    if (!isNormalUrlProtocol(URL_CREATOR.getProtocol(url))) {
        return url; // javascript:, about:, data: and anything not supported like foo:
    }

    try {
        String path = url.getPath();
        if (path != null) {
            path = encode(path, PATH_ALLOWED_CHARS, "UTF-8");
        }
        String query = url.getQuery();
        if (query != null) {
            if (minimalQueryEncoding) {
                query = org.apache.commons.lang3.StringUtils.replace(query, " ", "%20");
            } else {
                query = encode(query, QUERY_ALLOWED_CHARS, charset);
            }
        }
        String anchor = url.getRef();
        if (anchor != null) {
            anchor = encode(anchor, ANCHOR_ALLOWED_CHARS, "UTF-8");
        }
        return createNewUrl(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), path, anchor,
                query);
    } catch (final MalformedURLException e) {
        // Impossible... I think.
        throw new RuntimeException(e);
    }
}

From source file:de.innovationgate.utils.URLBuilder.java

/**
  * Creates a URLBuilder that parses an existing URL
 * @param url The URL to parse/*  w  ww  .j ava  2 s.  c o m*/
 */
public URLBuilder(URL url) {
    this(url.getProtocol(), url.getPort(), url.getHost(), url.getPath(), url.getQuery(), url.getRef(), "UTF-8");
}