Example usage for java.net URI getFragment

List of usage examples for java.net URI getFragment

Introduction

In this page you can find the example usage for java.net URI getFragment.

Prototype

public String getFragment() 

Source Link

Document

Returns the decoded fragment component of this URI.

Usage

From source file:org.opencms.site.xmlsitemap.CmsXmlSitemapGenerator.java

/**
 * Replaces the protocol/host/port of a link with the ones from the given server URI, if it's not empty.<p>
 *
 * @param link the link to change//w  w w. jav a 2s .co  m
 * @param server the server URI string
        
 * @return the changed link
 */
public static String replaceServerUri(String link, String server) {

    String serverUriStr = server;

    if (CmsStringUtil.isEmptyOrWhitespaceOnly(serverUriStr)) {
        return link;
    }
    try {
        URI serverUri = new URI(serverUriStr);
        URI linkUri = new URI(link);
        URI result = new URI(serverUri.getScheme(), serverUri.getAuthority(), linkUri.getPath(),
                linkUri.getQuery(), linkUri.getFragment());
        return result.toString();
    } catch (URISyntaxException e) {
        LOG.error(e.getLocalizedMessage(), e);
        return link;
    }

}

From source file:org.apache.distributedlog.util.DLUtils.java

/**
 * Normalize the uri.//w  ww  .j a  va2s  . c om
 *
 * @param uri the distributedlog uri.
 * @return the normalized uri
 */
public static URI normalizeURI(URI uri) {
    checkNotNull(uri, "DistributedLog uri is null");
    String scheme = uri.getScheme();
    checkNotNull(scheme, "Invalid distributedlog uri : " + uri);
    scheme = scheme.toLowerCase();
    String[] schemeParts = StringUtils.split(scheme, '-');
    checkArgument(Objects.equal(DistributedLogConstants.SCHEME_PREFIX, schemeParts[0].toLowerCase()),
            "Unknown distributedlog scheme found : " + uri);
    URI normalizedUri;
    try {
        normalizedUri = new URI(schemeParts[0], // remove backend info
                uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Invalid distributedlog uri found : " + uri, e);
    }
    return normalizedUri;
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * suitable for namespacing//from   w w  w  .ja  va 2  s .co m
 * @return the uri with relative schema ( ex. //facebook.com/path/to/stuff?query )
 */
public static URI createNamespaceUri(Object uriStr) {
    URI temp = createUriWithOptions(uriStr, true, true);
    try {
        URI namesUri = new URI(null, temp.getUserInfo(), temp.getHost(), -1, temp.getPath(), temp.getQuery(),
                temp.getFragment());
        return namesUri;
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:org.apache.hadoop.mapreduce.v2.util.MRApps.java

/**
 * Add the paths to the classpath if they are not jars
 * @param paths the paths to add to the classpath
 * @param withLinks the corresponding paths that may have a link name in them
 * @param conf used to resolve the paths
 * @param environment the environment to update CLASSPATH in
 * @throws IOException if there is an error resolving any of the paths.
 *//* w  ww.  jav a  2  s .c o  m*/
private static void addToClasspathIfNotJar(Path[] paths, URI[] withLinks, Configuration conf,
        Map<String, String> environment, String classpathEnvVar) throws IOException {
    if (paths != null) {
        HashMap<Path, String> linkLookup = new HashMap<Path, String>();
        if (withLinks != null) {
            for (URI u : withLinks) {
                Path p = new Path(u);
                FileSystem remoteFS = p.getFileSystem(conf);
                p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(), remoteFS.getWorkingDirectory()));
                String name = (null == u.getFragment()) ? p.getName() : u.getFragment();
                if (!StringUtils.toLowerCase(name).endsWith(".jar")) {
                    linkLookup.put(p, name);
                }
            }
        }

        for (Path p : paths) {
            FileSystem remoteFS = p.getFileSystem(conf);
            p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(), remoteFS.getWorkingDirectory()));
            String name = linkLookup.get(p);
            if (name == null) {
                name = p.getName();
            }
            if (!StringUtils.toLowerCase(name).endsWith(".jar")) {
                MRApps.addToEnvironment(environment, classpathEnvVar,
                        crossPlatformifyMREnv(conf, Environment.PWD) + Path.SEPARATOR + name, conf);
            }
        }
    }
}

From source file:org.hl7.fhir.client.ResourceAddress.java

public static URI appendHttpParameters(URI basePath, Map<String, String> parameters) {
    try {// w  w  w . j av  a  2  s .  c  o m
        Set<String> httpParameterNames = parameters.keySet();
        String query = basePath.getQuery();

        for (String httpParameterName : httpParameterNames) {
            if (query != null) {
                query += "&";
            } else {
                query = "";
            }
            query += httpParameterName + "=" + parameters.get(httpParameterName);
        }

        return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(),
                basePath.getPath(), query, basePath.getFragment());
    } catch (Exception e) {
        throw new EFhirClientException("Error appending http parameter", e);
    }
}

From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java

/**
 * Append the URL encoded OpenID message parameters to the query string of the provided URI.
 * /*w w w.j  av a2 s.co  m*/
 * @param uri URI to append OpenID message parameter to
 * @param message URL encoded OpenID message parameters
 * @return URI with message parameters appended
 */
public static URI appendMessageParameters(URI uri, String message) {
    if (uri == null || message == null) {
        return uri;
    }

    // build new query string
    StringBuffer queryBuffer = new StringBuffer();
    if (uri.getRawQuery() != null) {
        queryBuffer.append(uri.getRawQuery());
    }
    if (queryBuffer.length() > 0 && queryBuffer.charAt(queryBuffer.length() - 1) != '&') {
        queryBuffer.append('&');
    }
    queryBuffer.append(message);

    try {
        return URIUtils.createURI(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(),
                queryBuffer.toString(), uri.getFragment());
    } catch (URISyntaxException e) {
        log.error("Unable to append message parameters to URI: {}", e);
    }

    return null;
}

From source file:org.apache.hadoop.mapreduce.v2.util.MRApps.java

@SuppressWarnings("deprecation")
private static void parseDistributedCacheArtifacts(Configuration conf,
        Map<String, LocalResource> localResources, LocalResourceType type, URI[] uris, long[] timestamps,
        long[] sizes, boolean visibilities[]) throws IOException {

    if (uris != null) {
        // Sanity check
        if ((uris.length != timestamps.length) || (uris.length != sizes.length)
                || (uris.length != visibilities.length)) {
            throw new IllegalArgumentException("Invalid specification for "
                    + "distributed-cache artifacts of type " + type + " :" + " #uris=" + uris.length
                    + " #timestamps=" + timestamps.length + " #visibilities=" + visibilities.length);
        }// ww w . ja  v a2  s. com

        for (int i = 0; i < uris.length; ++i) {
            URI u = uris[i];
            Path p = new Path(u);
            FileSystem remoteFS = p.getFileSystem(conf);
            p = remoteFS.resolvePath(p.makeQualified(remoteFS.getUri(), remoteFS.getWorkingDirectory()));
            // Add URI fragment or just the filename
            Path name = new Path((null == u.getFragment()) ? p.getName() : u.getFragment());
            if (name.isAbsolute()) {
                throw new IllegalArgumentException("Resource name must be relative");
            }
            String linkName = name.toUri().getPath();
            LocalResource orig = localResources.get(linkName);
            URL url = URL.fromURI(p.toUri());
            if (orig != null && !orig.getResource().equals(url)) {
                LOG.warn(getResourceDescription(orig.getType()) + toString(orig.getResource())
                        + " conflicts with " + getResourceDescription(type) + toString(url)
                        + " This will be an error in Hadoop 2.0");
                continue;
            }
            localResources.put(linkName,
                    LocalResource.newInstance(URL.fromURI(p.toUri()), type,
                            visibilities[i] ? LocalResourceVisibility.PUBLIC : LocalResourceVisibility.PRIVATE,
                            sizes[i], timestamps[i]));
        }
    }
}

From source file:org.apache.hadoop.mapreduce.v2.util.MRApps.java

private static String getMRFrameworkName(Configuration conf) {
    String frameworkName = null;/*  w w  w. j a v a 2  s  .co  m*/
    String framework = conf.get(MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH, "");
    if (!framework.isEmpty()) {
        URI uri;
        try {
            uri = new URI(framework);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Unable to parse '" + framework
                    + "' as a URI, check the setting for " + MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH,
                    e);
        }

        frameworkName = uri.getFragment();
        if (frameworkName == null) {
            frameworkName = new Path(uri).getName();
        }
    }
    return frameworkName;
}

From source file:org.apache.hadoop.mapreduce.JobSubmitter.java

@SuppressWarnings("deprecation")
private static void addMRFrameworkToDistributedCache(Configuration conf) throws IOException {
    String framework = conf.get(MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH, "");
    if (!framework.isEmpty()) {
        URI uri;
        try {//from ww  w  .j  a v  a 2  s  .c o  m
            uri = new URI(framework);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Unable to parse '" + framework
                    + "' as a URI, check the setting for " + MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH,
                    e);
        }

        String linkedName = uri.getFragment();

        // resolve any symlinks in the URI path so using a "current" symlink
        // to point to a specific version shows the specific version
        // in the distributed cache configuration
        FileSystem fs = FileSystem.get(conf);
        Path frameworkPath = fs.makeQualified(new Path(uri.getScheme(), uri.getAuthority(), uri.getPath()));
        FileContext fc = FileContext.getFileContext(frameworkPath.toUri(), conf);
        frameworkPath = fc.resolvePath(frameworkPath);
        uri = frameworkPath.toUri();
        try {
            uri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, linkedName);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }

        DistributedCache.addCacheArchive(uri, conf);
    }
}

From source file:org.jenkinsci.plugins.github_branch_source.GitHubConfiguration.java

/**
 * Fix an apiUri./*from  ww w.  jav  a 2 s  . co m*/
 *
 * @param apiUri the api URI.
 * @return the normalized api URI.
 */
@CheckForNull
public static String normalizeApiUri(@CheckForNull String apiUri) {
    if (apiUri == null) {
        return null;
    }
    try {
        URI uri = new URI(apiUri).normalize();
        String scheme = uri.getScheme();
        if ("http".equals(scheme) || "https".equals(scheme)) {
            // we only expect http / https, but also these are the only ones where we know the authority
            // is server based, i.e. [userinfo@]server[:port]
            // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase

            String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH);
            int port = uri.getPort();
            if ("http".equals(scheme) && port == 80) {
                port = -1;
            } else if ("https".equals(scheme) && port == 443) {
                port = -1;
            }
            apiUri = new URI(scheme, uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(),
                    uri.getFragment()).toASCIIString();
        }
    } catch (URISyntaxException e) {
        // ignore, this was a best effort tidy-up
    }
    return apiUri.replaceAll("/$", "");
}