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.apache.synapse.commons.evaluators.source.URLTextRetriever.java

public String getSourceText(EvaluatorContext context) throws EvaluatorException {
    if (fragment == null) {
        return context.getUrl();
    }//from w  ww  .  j av  a2 s. co m

    try {
        URI uri = new URI(context.getUrl());
        switch (fragment) {
        case protocol:
            return uri.getScheme();
        case user:
            return uri.getUserInfo();
        case host:
            return uri.getHost();
        case port:
            return String.valueOf(uri.getPort());
        case path:
            return uri.getPath();
        case query:
            return uri.getQuery();
        case ref:
            return uri.getFragment();
        default:
            return context.getUrl();
        }
    } catch (URISyntaxException e) {
        String message = "Error parsing URL: " + context.getUrl();
        log.error(message);
        throw new EvaluatorException(message);
    }
}

From source file:org.codice.ddf.catalog.content.impl.FileSystemStorageProviderTest.java

private void assertUpdateRequest(UpdateStorageRequest updateRequest) throws Exception {
    UpdateStorageResponse updateResponse = provider.update(updateRequest);

    List<ContentItem> items = updateResponse.getUpdatedContentItems();
    ContentItem item = items.get(0);/*  ww  w .  j  a v  a 2  s .  c om*/
    String id = item.getId();

    LOGGER.debug("Item retrieved: {}", item);
    assertEquals(id, item.getId());
    assertEquals(NITF_MIME_TYPE, item.getMimeTypeRawData());
    URI uri = new URI(item.getUri());

    List<String> parts = provider.getContentFilePathParts(uri.getSchemeSpecificPart(), uri.getFragment());

    String expectedFilePath = baseDir + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_REPOSITORY
            + File.separator + FileSystemStorageProvider.DEFAULT_CONTENT_STORE + File.separator
            + FileSystemStorageProvider.DEFAULT_TMP + File.separator + updateRequest.getId() + File.separator
            + parts.get(0) + File.separator + parts.get(1) + File.separator + parts.get(2)
            + (StringUtils.isNotBlank(item.getQualifier()) ? File.separator + item.getQualifier() : "")
            + File.separator + item.getFilename();

    assertThat(Files.exists(Paths.get(expectedFilePath)), is(true));
    assertTrue(item.getSize() > 0);

    String updatedFileContents = IOUtils.toString(item.getInputStream());
    assertEquals("Updated NITF", updatedFileContents);
    provider.commit(updateRequest);
    assertReadRequest(new URI(updateRequest.getContentItems().get(0).getUri()), NITF_MIME_TYPE);
}

From source file:org.apache.camel.component.comet.CometComponent.java

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
        throws Exception {

    if (endpointCache.containsKey(uri)) {
        LOG.debug("Using cached endpoint for URI " + uri);
        return endpointCache.get(uri);
    }// w w w. jav  a 2s  .c  om

    LOG.debug("Creating new endpoint for URI " + uri);
    CometEndpoint endpoint = new CometEndpoint(uri, this);
    URI u = new URI(uri);
    endpoint.setHost(u.getHost());
    endpoint.setScheme(u.getScheme());
    if (u.getPort() > 0)
        endpoint.setPort(u.getPort());
    else {
        endpoint.setPort(endpoint.isSecure() ? 443 : 80);
    }
    if (u.getUserInfo() != null) {
        endpoint.setUser(u.getUserInfo());
    }
    String remainingPath = u.getPath();
    endpoint.setPath(remainingPath != null ? remainingPath : "/");
    endpoint.setChannel(u.getFragment());
    endpoint.initialize();

    endpointCache.put(uri, endpoint);

    return endpoint;
}

From source file:org.apache.cxf.fediz.service.oidc.clients.ClientRegistrationService.java

private boolean isValidURI(String uri, boolean requireHttps) {

    UrlValidator urlValidator = null;//from w w w.ja  v  a2 s .  co m

    if (requireHttps) {
        String[] schemes = { "https" };
        urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS);
    } else {
        urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS + UrlValidator.ALLOW_ALL_SCHEMES);
    }

    if (!urlValidator.isValid(uri)) {
        return false;
    }

    // Do additional checks on the URI
    try {
        URI parsedURI = new URI(uri);
        // The URI can't have a fragment according to the OAuth 2.0 spec (+ audience spec)
        if (parsedURI.getFragment() != null) {
            return false;
        }
    } catch (URISyntaxException ex) {
        return false;
    }

    return true;
}

From source file:com.mellanox.jxio.ServerPortal.java

private URI replacePortInURI(URI uri, int newPort) {
    URI newUri = null;//from  www.ja  va  2 s.  c  o m
    try {
        newUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), newPort, uri.getPath(),
                uri.getQuery(), uri.getFragment());
        if (LOG.isDebugEnabled()) {
            LOG.debug(this.toLogString() + "uri with port " + newPort + " is " + newUri.toString());
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
        LOG.error(this.toLogString() + "URISyntaxException occured while trying to create a new URI");
    }

    return newUri;
}

From source file:com.buaa.cfs.fs.Path.java

/** Returns a qualified path object. */
public Path makeQualified(URI defaultUri, Path workingDir) {
    Path path = this;
    if (!isAbsolute()) {
        path = new Path(workingDir, this);
    }/*from ww  w. j  ava 2  s  . com*/

    URI pathUri = path.toUri();

    String scheme = pathUri.getScheme();
    String authority = pathUri.getAuthority();
    String fragment = pathUri.getFragment();

    if (scheme != null && (authority != null || defaultUri.getAuthority() == null))
        return path;

    if (scheme == null) {
        scheme = defaultUri.getScheme();
    }

    if (authority == null) {
        authority = defaultUri.getAuthority();
        if (authority == null) {
            authority = "";
        }
    }

    URI newUri = null;
    try {
        newUri = new URI(scheme, authority, normalizePath(scheme, pathUri.getPath()), null, fragment);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    return new Path(newUri);
}

From source file:org.dita.dost.module.ChunkModule.java

/**
 * Update Job configuration to include new generated files
 *///from  ww  w  . j ava 2  s  .c  om
private void updateList(final Map<String, String> changeTable, final Map<String, String> conflictTable) {
    final URI xmlDitalist = new File(job.tempDir, "dummy.xml").toURI();

    final Set<URI> hrefTopics = new HashSet<>();
    for (final FileInfo f : job.getFileInfo()) {
        if (f.isNonConrefTarget) {
            hrefTopics.add(f.uri);
        }
    }
    for (final FileInfo f : job.getFileInfo()) {
        if (f.isSkipChunk) {
            final URI s = f.uri;
            if (s.getFragment() == null) {
                // This entry does not have an anchor, we assume that this
                // topic will
                // be fully chunked. Thus it should not produce any output.
                final Iterator<URI> hrefit = hrefTopics.iterator();
                while (hrefit.hasNext()) {
                    final URI ent = hrefit.next();
                    if (resolve(job.tempDir, ent).equals(resolve(job.tempDir, s))) {
                        // The entry in hrefTopics points to the same target
                        // as entry in chunkTopics, it should be removed.
                        hrefit.remove();
                    }
                }
            } else if (hrefTopics.contains(s)) {
                hrefTopics.remove(s);
            }
        }
    }

    final Set<URI> topicList = new LinkedHashSet<>(128);
    final Set<URI> oldTopicList = new HashSet<>();
    for (final FileInfo f : job.getFileInfo()) {
        if (ATTR_FORMAT_VALUE_DITA.equals(f.format)) {
            oldTopicList.add(f.uri);
        }
    }
    for (final URI hrefTopic : hrefTopics) {
        final URI t = getRelativePath(xmlDitalist,
                job.tempDir.toURI().resolve(stripFragment(hrefTopic.toString())));
        topicList.add(t);
        if (oldTopicList.contains(t)) {
            oldTopicList.remove(t);
        }
    }

    final Set<URI> chunkedTopicSet = new LinkedHashSet<>(128);
    final Set<URI> chunkedDitamapSet = new LinkedHashSet<>(128);
    final Set<URI> ditamapList = new HashSet<>();
    for (final FileInfo f : job.getFileInfo()) {
        if (ATTR_FORMAT_VALUE_DITAMAP.equals(f.format)) {
            ditamapList.add(f.uri);
        }
    }
    // XXX: Change to <File, File>
    for (final Map.Entry<String, String> entry : changeTable.entrySet()) {
        final String oldFile = entry.getKey();
        if (entry.getValue().equals(oldFile)) {
            // newly chunked file
            URI newChunkedFile = new File(entry.getValue()).toURI();
            newChunkedFile = getRelativePath(xmlDitalist, newChunkedFile);
            final String extName = getExtension(newChunkedFile.getPath());
            if (extName != null && !extName.equalsIgnoreCase(ATTR_FORMAT_VALUE_DITAMAP)) {
                chunkedTopicSet.add(newChunkedFile);
                if (!topicList.contains(newChunkedFile)) {
                    topicList.add(newChunkedFile);
                    if (oldTopicList.contains(newChunkedFile)) {
                        // newly chunked file shouldn't be deleted
                        oldTopicList.remove(newChunkedFile);
                    }
                }
            } else {
                if (!ditamapList.contains(newChunkedFile)) {
                    ditamapList.add(newChunkedFile);
                    if (oldTopicList.contains(newChunkedFile)) {
                        oldTopicList.remove(newChunkedFile);
                    }
                }
                chunkedDitamapSet.add(newChunkedFile);
            }

        }
    }
    // removed extra topic files
    for (final URI s : oldTopicList) {
        //            if (!StringUtils.isEmptyString(s)) {
        final File f = new File(job.tempDir.toURI().resolve(s));
        if (f.exists() && !f.delete()) {
            logger.error("Failed to delete " + f.getAbsolutePath());
        }
        //            }
    }

    // TODO we have refined topic list and removed extra topic files, next
    // we need to clean up
    // conflictTable and try to resolve file name conflicts.
    for (final Map.Entry<String, String> entry : changeTable.entrySet()) {
        final String oldFile = entry.getKey();
        if (entry.getValue().equals(oldFile)) {
            // original topic file
            final String targetPath = conflictTable.get(entry.getKey());
            if (targetPath != null) {
                final URI target = new File(targetPath).toURI();
                if (!new File(target).exists()) {
                    // newly chunked file
                    final URI from = new File(entry.getValue()).toURI();
                    URI relativePath = getRelativePath(xmlDitalist, from);
                    final URI relativeTargetPath = getRelativePath(xmlDitalist, target);
                    if (relativeTargetPath.getPath().lastIndexOf(File.separator) != -1) {
                        relativePath2fix.put(relativeTargetPath.getPath(), relativeTargetPath.getPath()
                                .substring(0, relativeTargetPath.getPath().lastIndexOf(File.separator) + 1));
                    }
                    // ensure the newly chunked file to the old one
                    try {
                        deleteQuietly(new File(target));
                        moveFile(new File(from), new File(target));
                    } catch (final IOException e) {
                        logger.error("Failed to replace chunk topic: " + e.getMessage(), e);

                    }
                    if (topicList.contains(relativePath)) {
                        topicList.remove(relativePath);
                    }
                    if (chunkedTopicSet.contains(relativePath)) {
                        chunkedTopicSet.remove(relativePath);
                    }
                    relativePath = getRelativePath(xmlDitalist, target);
                    topicList.add(relativePath);
                    chunkedTopicSet.add(relativePath);
                } else {
                    conflictTable.remove(entry.getKey());
                }
            }
        }
    }

    final Set<URI> all = new HashSet<>();
    all.addAll(topicList);
    all.addAll(ditamapList);
    all.addAll(chunkedDitamapSet);
    all.addAll(chunkedTopicSet);

    // remove redundant topic information
    for (final URI file : oldTopicList) {
        if (!all.contains(file)) {
            job.remove(job.getOrCreateFileInfo(file));
        }
    }

    for (final URI file : topicList) {
        final FileInfo ff = job.getOrCreateFileInfo(file);
        ff.format = ATTR_FORMAT_VALUE_DITA;
    }
    for (final URI file : ditamapList) {
        final FileInfo ff = job.getOrCreateFileInfo(file);
        ff.format = ATTR_FORMAT_VALUE_DITAMAP;
    }

    for (final URI file : chunkedDitamapSet) {
        final FileInfo f = job.getOrCreateFileInfo(file);
        f.format = ATTR_FORMAT_VALUE_DITAMAP;
        f.isResourceOnly = false;
    }
    for (final URI file : chunkedTopicSet) {
        final FileInfo f = job.getOrCreateFileInfo(file);
        f.format = ATTR_FORMAT_VALUE_DITA;
        f.isResourceOnly = false;
    }

    try {
        job.write();
    } catch (final IOException ex) {
        logger.error(ex.getMessage(), ex);
    }
}

From source file:org.apache.hadoop.fs.TestFileSystem.java

static void runTestCache(int port) throws Exception {
    Configuration conf = new Configuration();
    MiniDFSCluster cluster = null;//from   ww w . j  a va 2 s.co  m
    try {
        cluster = new MiniDFSCluster(port, conf, 2, true, true, null, null);
        URI uri = cluster.getFileSystem().getUri();
        LOG.info("uri=" + uri);

        {
            FileSystem fs = FileSystem.get(uri, new Configuration());
            checkPath(cluster, fs);
            for (int i = 0; i < 100; i++) {
                assertTrue(fs == FileSystem.get(uri, new Configuration()));
            }
        }

        if (port == NameNode.DEFAULT_PORT) {
            //test explicit default port
            URI uri2 = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), NameNode.DEFAULT_PORT,
                    uri.getPath(), uri.getQuery(), uri.getFragment());
            LOG.info("uri2=" + uri2);
            FileSystem fs = FileSystem.get(uri2, conf);
            checkPath(cluster, fs);
            for (int i = 0; i < 100; i++) {
                assertTrue(fs == FileSystem.get(uri2, new Configuration()));
            }
        }
    } finally {
        if (cluster != null)
            cluster.shutdown();
    }
}

From source file:com.sap.core.odata.fit.basic.issues.TestIssue105.java

@Test
public void checkContextForDifferentHostNamesRequests()
        throws ClientProtocolException, IOException, ODataException, URISyntaxException {
    URI uri1 = URI.create(getEndpoint().toString() + "$metadata");

    HttpGet get1 = new HttpGet(uri1);
    HttpResponse response1 = getHttpClient().execute(get1);
    assertNotNull(response1);/* w  ww .jav  a  2  s. c  om*/

    URI serviceRoot1 = getService().getProcessor().getContext().getPathInfo().getServiceRoot();
    assertEquals(uri1.getHost(), serviceRoot1.getHost());

    get1.reset();

    URI uri2 = new URI(uri1.getScheme(), uri1.getUserInfo(), "127.0.0.1", uri1.getPort(), uri1.getPath(),
            uri1.getQuery(), uri1.getFragment());

    HttpGet get2 = new HttpGet(uri2);
    HttpResponse response2 = getHttpClient().execute(get2);
    assertNotNull(response2);

    URI serviceRoot2 = getService().getProcessor().getContext().getPathInfo().getServiceRoot();
    assertEquals(uri2.getHost(), serviceRoot2.getHost());
}

From source file:com.esri.geoportal.commons.agp.client.AgpClient.java

private URI generateTokenUri() throws URISyntaxException {
    URI uri = rootUrl.toURI().resolve("sharing/rest/generateToken");
    return new URI("https", uri.getSchemeSpecificPart(), uri.getFragment());
}