Example usage for java.net URI resolve

List of usage examples for java.net URI resolve

Introduction

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

Prototype

public URI resolve(String str) 

Source Link

Document

Constructs a new URI by parsing the given string and then resolving it against this URI.

Usage

From source file:org.apache.taverna.scufl2.wfdesc.WfdescSerialiser.java

protected OntModel save(final WorkflowBundle bundle) {
    final OntModel model = ModelFactory.createOntologyModel();
    bundle.accept(new VisitorWithPath() {
        Scufl2Tools scufl2Tools = new Scufl2Tools();

        public boolean visit() {
            WorkflowBean node = getCurrentNode();
            // System.out.println(node);
            if (node instanceof WorkflowBundle) {
                return true;
            }/*ww w.  j a  v a  2  s . c o  m*/
            // @SuppressWarnings("rawtypes")

            if (node instanceof org.apache.taverna.scufl2.api.core.Workflow) {
                entityForBean(node, Wfdesc.Workflow);
            } else if (node instanceof Processor) {
                Processor processor = (Processor) node;
                Individual process = entityForBean(processor, Wfdesc.Process);
                Individual wf = entityForBean(processor.getParent(), Wfdesc.Workflow);
                wf.addProperty(Wfdesc.hasSubProcess, process);
            } else if (node instanceof InputPort) {
                WorkflowBean parent = ((Child) node).getParent();
                Individual input = entityForBean(node, Wfdesc.Input);
                Individual process = entityForBean(parent, Wfdesc.Process);
                process.addProperty(Wfdesc.hasInput, input);

            } else if (node instanceof OutputPort) {
                WorkflowBean parent = ((Child) node).getParent();
                Individual output = entityForBean(node, Wfdesc.Output);
                Individual process = entityForBean(parent, Wfdesc.Process);
                process.addProperty(Wfdesc.hasOutput, output);
            } else if (node instanceof DataLink) {
                WorkflowBean parent = ((Child) node).getParent();
                DataLink link = (DataLink) node;
                Individual dl = entityForBean(link, Wfdesc.DataLink);

                Individual source = entityForBean(link.getReceivesFrom(), Wfdesc.Output);
                dl.addProperty(Wfdesc.hasSource, source);

                Individual sink = entityForBean(link.getSendsTo(), Wfdesc.Input);
                dl.addProperty(Wfdesc.hasSink, sink);
                Individual wf = entityForBean(parent, Wfdesc.Workflow);
                wf.addProperty(Wfdesc.hasDataLink, dl);
            } else if (node instanceof Profile) {
                // So that we can get at the ProcessorBinding - buy only if
                // it is the main Profile
                return node == bundle.getMainProfile();
            } else if (node instanceof ProcessorBinding) {
                ProcessorBinding b = (ProcessorBinding) node;
                Activity a = b.getBoundActivity();
                Processor boundProcessor = b.getBoundProcessor();
                Individual process = entityForBean(boundProcessor, Wfdesc.Process);

                // Note: We don't describe the activity and processor
                // binding in wfdesc. Instead we
                // assign additional types and attributes to the parent
                // processor

                try {
                    URI type = a.getType();
                    Configuration c = scufl2Tools.configurationFor(a, b.getParent());
                    JsonNode json = c.getJson();
                    if (type.equals(BEANSHELL)) {
                        process.addRDFType(Wf4ever.BeanshellScript);
                        String s = json.get("script").asText();
                        process.addProperty(Wf4ever.script, s);
                        JsonNode localDep = json.get("localDependency");
                        if (localDep != null && localDep.isArray()) {
                            for (int i = 0; i < localDep.size(); i++) {
                                String depStr = localDep.get(i).asText();
                                // FIXME: Better class for dependency?
                                Individual dep = model.createIndividual(OWL.Thing);
                                dep.addLabel(depStr, null);
                                dep.addComment("JAR dependency", "en");
                                process.addProperty(Roterms.requiresSoftware, dep);
                                // Somehow this gets the whole thing to fall
                                // out of the graph!
                                // QName depQ = new
                                // QName("http://google.com/", ""+
                                // UUID.randomUUID());
                                // sesameManager.rename(dep, depQ);

                            }
                        }
                    }
                    if (type.equals(RSHELL)) {
                        process.addRDFType(Wf4ever.RScript);
                        String s = json.get("script").asText();
                        process.addProperty(Wf4ever.script, s);
                    }
                    if (type.equals(WSDL)) {
                        process.addRDFType(Wf4ever.SOAPService);
                        JsonNode operation = json.get("operation");
                        URI wsdl = URI.create(operation.get("wsdl").asText());
                        process.addProperty(Wf4ever.wsdlURI, wsdl.toASCIIString());
                        process.addProperty(Wf4ever.wsdlOperationName, operation.get("name").asText());
                        process.addProperty(Wf4ever.rootURI, wsdl.resolve("/").toASCIIString());
                    }
                    if (type.equals(REST)) {
                        process.addRDFType(Wf4ever.RESTService);
                        // System.out.println(json);
                        JsonNode request = json.get("request");
                        String absoluteURITemplate = request.get("absoluteURITemplate").asText();
                        String uriTemplate = absoluteURITemplate.replace("{", "");
                        uriTemplate = uriTemplate.replace("}", "");
                        // TODO: Detect {}
                        try {
                            URI root = new URI(uriTemplate).resolve("/");
                            process.addProperty(Wf4ever.rootURI, root.toASCIIString());
                        } catch (URISyntaxException e) {
                            logger.warning("Potentially invalid URI template: " + absoluteURITemplate);
                            // Uncomment to temporarily break
                            // TestInvalidURITemplate:
                            // rest.getWfRootURI().add(URI.create("http://example.com/FRED"));
                        }
                    }
                    if (type.equals(TOOL)) {
                        process.addRDFType(Wf4ever.CommandLineTool);
                        JsonNode desc = json.get("toolDescription");
                        // System.out.println(json);
                        JsonNode command = desc.get("command");
                        if (command != null) {
                            process.addProperty(Wf4ever.command, command.asText());
                        }
                    }
                    if (type.equals(NESTED_WORKFLOW)) {
                        Workflow nestedWf = scufl2Tools.nestedWorkflowForProcessor(boundProcessor,
                                b.getParent());
                        // The parent process is a specialization of the
                        // nested workflow
                        // (because the nested workflow could exist as
                        // several processors)
                        specializationOf(boundProcessor, nestedWf);
                        process.addRDFType(Wfdesc.Workflow);

                        // Just like the Processor specializes the nested
                        // workflow, the
                        // ProcessorPorts specialize the WorkflowPort
                        for (ProcessorPortBinding portBinding : b.getInputPortBindings()) {
                            // Map from activity port (not in wfdesc) to
                            // WorkflowPort
                            WorkflowPort wfPort = nestedWf.getInputPorts()
                                    .getByName(portBinding.getBoundActivityPort().getName());
                            if (wfPort == null) {
                                continue;
                            }
                            specializationOf(portBinding.getBoundProcessorPort(), wfPort);
                        }
                        for (ProcessorPortBinding portBinding : b.getOutputPortBindings()) {
                            WorkflowPort wfPort = nestedWf.getOutputPorts()
                                    .getByName(portBinding.getBoundActivityPort().getName());
                            if (wfPort == null) {
                                continue;
                            }
                            specializationOf(portBinding.getBoundProcessorPort(), wfPort);
                        }
                    }
                } catch (IndexOutOfBoundsException ex) {
                }
                return false;
            } else {
                // System.out.println("--NO!");
                return false;
            }
            for (Annotation ann : scufl2Tools.annotationsFor(node, bundle)) {
                String annotationBody = ann.getBody().toASCIIString();
                String baseURI = bundle.getGlobalBaseURI().resolve(ann.getBody()).toASCIIString();
                InputStream annotationStream;
                try {
                    annotationStream = bundle.getResources().getResourceAsInputStream(annotationBody);

                    try {
                        // FIXME: Don't just assume Lang.TURTLE
                        RDFDataMgr.read(model, annotationStream, baseURI, Lang.TURTLE);
                    } catch (RiotException e) {
                        logger.log(Level.WARNING, "Can't parse RDF Turtle from " + annotationBody, e);
                    } finally {
                        annotationStream.close();
                    }
                } catch (IOException e) {
                    logger.log(Level.WARNING, "Can't read " + annotationBody, e);
                }
            }
            if (node instanceof Named) {
                Named named = (Named) node;
                entityForBean(node, OWL.Thing).addLabel(named.getName(), null);
            }
            return true;
        }

        private void specializationOf(WorkflowBean special, WorkflowBean general) {
            Individual specialEnt = entityForBean(special, Prov_o.Entity);
            Individual generalEnt = entityForBean(general, Prov_o.Entity);
            specialEnt.addProperty(Prov_o.specializationOf, generalEnt);
        }

        private Individual entityForBean(WorkflowBean bean, Resource thing) {
            return model.createIndividual(uriForBean(bean), thing);
        }

        // @Override
        // public boolean visitEnter(WorkflowBean node) {
        // if (node instanceof Processor
        // || node instanceof org.apache.taverna.scufl2.api.core.Workflow
        // || node instanceof Port || node instanceof DataLink) {
        // visit(node);
        // return true;
        // }
        // // The other node types (e.g. dispatch stack, configuration) are
        // // not (directly) represented in wfdesc
        //// System.out.println("Skipping " + node);
        // return false;
        // };
    });
    return model;
}

From source file:org.kitodo.production.services.file.FileServiceTest.java

@Test
public void testCopyDirectory() throws IOException {
    URI fromDirectory = fileService.createDirectory(URI.create("fileServiceTest"), "copyDirectory");
    fileService.createResource(fromDirectory, "test.pdf");
    URI toDirectory = URI.create("fileServiceTest/copyDirectoryTo/");

    assertFalse(fileService.fileExist(toDirectory));

    fileService.copyDirectory(fromDirectory, toDirectory);

    assertTrue(fileService.fileExist(toDirectory));
    assertTrue(fileService.fileExist(toDirectory.resolve("test.pdf")));
    assertTrue(fileService.fileExist(fromDirectory));

}

From source file:org.kitodo.production.services.file.FileServiceTest.java

@Test
public void testCopyFileToDirectory() throws IOException {
    URI originFile = fileService.createResource(URI.create("fileServiceTest"), "copyFileToDirectory.txt");
    URI targetDirectory = fileService.createDirectory(URI.create("fileServiceTest"),
            "copyFileToDirectoryTarget");

    assertTrue(fileService.fileExist(originFile));
    assertTrue(fileService.fileExist(targetDirectory));
    assertFalse(fileService.fileExist(targetDirectory.resolve("copyFileToDirectory.txt")));

    fileService.copyFileToDirectory(originFile, targetDirectory);

    assertTrue(fileService.fileExist(originFile));
    assertTrue(fileService.fileExist(targetDirectory.resolve("copyFileToDirectory.txt")));

}

From source file:org.kitodo.production.services.file.FileServiceTest.java

@Test(expected = FileNotFoundException.class)
public void testCopyFileToDirectoryWithMissingSource() throws IOException {
    URI originFile = URI.create("fileServiceTest/copyFileToDirectoryMissingSource");
    URI targetDirectory = fileService.createDirectory(URI.create("fileServiceTest"),
            "copyFileToDirectoryMissingSourceTarget");

    assertFalse(fileService.fileExist(originFile));
    assertTrue(fileService.fileExist(targetDirectory));
    assertFalse(fileService.fileExist(targetDirectory.resolve("copyFileToDirectoryMissingSource")));

    fileService.copyFileToDirectory(originFile, targetDirectory);

}

From source file:tachyon.TachyonURI.java

/**
 * Resolve a child TachyonURI against a parent TachyonURI.
 *
 * @param parent the parent/*from   w  w  w . j  ava  2 s  .c o  m*/
 * @param child the child
 */
public TachyonURI(TachyonURI parent, TachyonURI child) {
    // Add a slash to parent's path so resolution is compatible with URI's
    URI parentUri = parent.mUri;
    String parentPath = parentUri.getPath();
    if (!parentPath.endsWith(SEPARATOR) && parentPath.length() > 0) {
        parentPath += SEPARATOR;
    }
    try {
        parentUri = new URI(parentUri.getScheme(), parentUri.getAuthority(), parentPath, null, null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    URI resolved = parentUri.resolve(child.mUri);
    mUri = createURI(resolved.getScheme(), resolved.getAuthority(), resolved.getPath());
}

From source file:org.kitodo.services.file.FileServiceTest.java

@Test
public void testCopyDirectory() throws IOException {
    URI fromDirectory = fileService.createDirectory(URI.create("fileServiceTest"), "copyDirectory");
    fileService.createResource(fromDirectory, "test.pdf");
    URI toDirectory = URI.create("fileServiceTest/copyDirectoryTo/");

    Assert.assertFalse(fileService.fileExist(toDirectory));

    fileService.copyDirectory(fromDirectory, toDirectory);

    Assert.assertTrue(fileService.fileExist(toDirectory));
    Assert.assertTrue(fileService.fileExist(toDirectory.resolve("test.pdf")));
    Assert.assertTrue(fileService.fileExist(fromDirectory));

}

From source file:org.kitodo.services.file.FileServiceTest.java

@Test
public void testCopyFileToDirectory() throws IOException {
    URI originFile = fileService.createResource(URI.create("fileServiceTest"), "copyFileToDirectory.txt");
    URI targetDirectory = fileService.createDirectory(URI.create("fileServiceTest"),
            "copyFileToDirectoryTarget");

    Assert.assertTrue(fileService.fileExist(originFile));
    Assert.assertTrue(fileService.fileExist(targetDirectory));
    Assert.assertFalse(fileService.fileExist(targetDirectory.resolve("copyFileToDirectory.txt")));

    fileService.copyFileToDirectory(originFile, targetDirectory);

    Assert.assertTrue(fileService.fileExist(originFile));
    Assert.assertTrue(fileService.fileExist(targetDirectory.resolve("copyFileToDirectory.txt")));

}

From source file:com.netflix.genie.web.services.impl.JobDirectoryServerServiceImpl.java

private DefaultDirectoryWriter.Entry createEntry(final JobDirectoryManifest.ManifestEntry manifestEntry,
        final URI baseUri) {
    final DefaultDirectoryWriter.Entry entry = new DefaultDirectoryWriter.Entry();
    // For backwards compatibility the V3 names ended in "/" for directories
    if (manifestEntry.isDirectory()) {
        entry.setName(manifestEntry.getName().endsWith("/") ? manifestEntry.getName()
                : manifestEntry.getName() + "/");
    } else {//from  ww w .j a va2s .  c  o m
        entry.setName(manifestEntry.getName());
    }
    entry.setUrl(baseUri.resolve(manifestEntry.getPath()).toString());
    entry.setSize(manifestEntry.getSize());
    entry.setLastModified(manifestEntry.getLastModifiedTime());
    return entry;
}

From source file:org.kitodo.services.file.FileServiceTest.java

@Test(expected = FileNotFoundException.class)
public void testCopyFileToDirectoryWithMissingSource() throws IOException {
    URI originFile = URI.create("fileServiceTest/copyFileToDirectoryMissingSource");
    URI targetDirectory = fileService.createDirectory(URI.create("fileServiceTest"),
            "copyFileToDirectoryMissingSourceTarget");

    Assert.assertFalse(fileService.fileExist(originFile));
    Assert.assertTrue(fileService.fileExist(targetDirectory));
    Assert.assertFalse(fileService.fileExist(targetDirectory.resolve("copyFileToDirectoryMissingSource")));

    fileService.copyFileToDirectory(originFile, targetDirectory);

}

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

/** Resolve a child path against a parent path. */
public Path(Path parent, Path child) {
    // Add a slash to parent's path so resolution is compatible with URI's
    URI parentUri = parent.uri;
    String parentPath = parentUri.getPath();
    if (!(parentPath.equals("/") || parentPath.isEmpty())) {
        try {/*w w  w .j a  v a  2 s .co m*/
            parentUri = new URI(parentUri.getScheme(), parentUri.getAuthority(), parentUri.getPath() + "/",
                    null, parentUri.getFragment());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }
    URI resolved = parentUri.resolve(child.uri);
    initialize(resolved.getScheme(), resolved.getAuthority(), resolved.getPath(), resolved.getFragment());
}