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.eclipse.orion.server.tests.servlets.xfer.TransferTest.java

@Test
public void testImportFile() throws CoreException, IOException, SAXException, URISyntaxException {
    //create a directory to upload to
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);/*w w w.j  a v  a 2s  .c o  m*/

    //start the import
    URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
    File source = new File(FileLocator.toFileURL(entry).getPath());
    long length = source.length();
    String importPath = getImportRequestPath(directoryPath);
    PostMethodWebRequest request = new PostMethodWebRequest(importPath);
    request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
    request.setHeaderField("X-Xfer-Options", "raw");

    request.setHeaderField("Slug", "client.zip");
    setAuthentication(request);
    WebResponse postResponse = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
    String location = postResponse.getHeaderField("Location");
    assertNotNull(location);
    URI importURI = URIUtil.fromString(importPath);
    location = importURI.resolve(location).toString();

    doImport(source, length, location);

    //assert the file is present in the workspace
    assertTrue(checkFileExists(directoryPath + "/client.zip"));
    //assert that imported file has same content as original client.zip
    assertTrue(checkContentEquals(source, directoryPath + "/client.zip"));
}

From source file:org.eclipse.orion.server.tests.servlets.xfer.TransferTest.java

@Test
public void testImportWithPostZeroByteFile()
        throws CoreException, IOException, SAXException, URISyntaxException {
    //create a directory to upload to
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);/*from w ww.  j  a v  a  2 s  .co m*/

    //start the import
    URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/zeroByteFile.txt");
    File source = new File(FileLocator.toFileURL(entry).getPath());
    long length = source.length();
    assertEquals(length, 0);
    String importPath = getImportRequestPath(directoryPath);
    PostMethodWebRequest request = new PostMethodWebRequest(importPath);
    request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
    request.setHeaderField("X-Xfer-Options", "raw");

    request.setHeaderField("Slug", "zeroByteFile.txt");
    setAuthentication(request);
    WebResponse postResponse = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
    String location = postResponse.getHeaderField("Location");
    assertNotNull(location);
    URI importURI = URIUtil.fromString(importPath);
    location = importURI.resolve(location).toString();

    doImport(source, length, location, "text/plain");

    //assert the file is present in the workspace
    assertTrue(checkFileExists(directoryPath + "/zeroByteFile.txt"));
}

From source file:pcgen.gui2.dialog.ExportDialog.java

private File getSelectedTemplate() {
    File osDir;//from ww  w. ja v a  2 s .co m
    String outputSheetDirectory = SettingsHandler.getGame().getOutputSheetDirectory();
    if (outputSheetDirectory == null) {
        osDir = new File(ConfigurationSettings.getOutputSheetsDir());
        outputSheetDirectory = "";
    } else {
        osDir = new File(ConfigurationSettings.getOutputSheetsDir(), outputSheetDirectory);
    }
    URI osPath = new File(osDir, ((SheetFilter) exportBox.getSelectedItem()).getPath()).toURI();
    URI uri = fileList.getSelectedValue();
    return new File(osPath.resolve(uri));
}

From source file:org.eclipse.orion.server.tests.servlets.xfer.TransferTest.java

@Test
public void testImportFileMultiPart() throws CoreException, IOException, SAXException, URISyntaxException {
    //create a directory to upload to
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);//from   www  .  j  a va 2s  .  c o m

    URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
    File source = new File(FileLocator.toFileURL(entry).getPath());

    // current server implementation cannot handle binary data, thus base64-encode client.zip before import
    File expected = EFS.getStore(makeLocalPathAbsolute(directoryPath + "/expected.txt")).toLocalFile(EFS.NONE,
            null);
    byte[] expectedContent = Base64.encode(FileUtils.readFileToByteArray(source));
    FileUtils.writeByteArrayToFile(expected, expectedContent);

    //start the import
    long length = expectedContent.length;
    String importPath = getImportRequestPath(directoryPath);
    PostMethodWebRequest request = new PostMethodWebRequest(importPath);
    request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
    request.setHeaderField("X-Xfer-Options", "raw");
    request.setHeaderField("Slug", "actual.txt");
    setAuthentication(request);
    WebResponse postResponse = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
    String location = postResponse.getHeaderField("Location");
    assertNotNull(location);
    URI importURI = URIUtil.fromString(importPath);
    location = importURI.resolve(location).toString();

    // perform the upload
    doImport(expected, length, location, "multipart/mixed;boundary=foobar");

    //assert the file is present in the workspace
    assertTrue(checkFileExists(directoryPath + "/actual.txt"));
    //assert that actual.txt has same content as expected.txt
    assertTrue(checkContentEquals(expected, directoryPath + "/actual.txt"));
}

From source file:org.eclipse.orion.server.tests.servlets.xfer.TransferTest.java

@Test
public void testImportDBCSFilename() throws CoreException, IOException, SAXException, URISyntaxException {
    //create a directory to upload to
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);/*from   w  w w .j a  v  a  2  s  . com*/

    //start the import
    // file with DBCS character in the filename.
    String filename = "\u3042.txt";
    File source = null;
    try {
        source = createTempFile(filename, "No content");
        long length = source.length();
        String importPath = getImportRequestPath(directoryPath);
        PostMethodWebRequest request = new PostMethodWebRequest(importPath);
        request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
        request.setHeaderField("X-Xfer-Options", "raw");

        request.setHeaderField("Slug", Slug.encode(filename));
        setAuthentication(request);
        WebResponse postResponse = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
        String location = postResponse.getHeaderField("Location");
        assertNotNull(location);
        URI importURI = URIUtil.fromString(importPath);
        location = importURI.resolve(location).toString();

        doImport(source, length, location, "text/plain");

        //assert the file is present in the workspace
        assertTrue(checkFileExists(directoryPath + File.separator + filename));
        //assert that imported file has same content as original client.zip
        assertTrue(checkContentEquals(source, directoryPath + File.separator + filename));
    } finally {
        // Delete the temp file
        FileUtils.deleteQuietly(source);
    }
}

From source file:org.eclipse.orion.server.tests.servlets.xfer.TransferTest.java

@Test
public void testImportEmojiFilename() throws CoreException, IOException, SAXException, URISyntaxException {
    //create a directory to upload to
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);/*from   w  w w  .java  2 s  .  co m*/

    //start the import
    // file with Emoji characters in the filename.
    // U+1F60A: SMILING FACE WITH SMILING EYES ("\ud83d\ude0a")
    // U+1F431: CAT FACE ("\ud83d\udc31")
    // U+1F435: MONKEY FACE ("\ud83d\udc35")
    String filename = "\ud83d\ude0a\ud83d\udc31\ud83d\udc35.txt";
    String contents = "Emoji characters: \ud83d\ude0a\ud83d\udc31\ud83d\udc35";
    File source = null;
    try {
        source = createTempFile(filename, contents);
        long length = source.length();

        String importPath = getImportRequestPath(directoryPath);
        PostMethodWebRequest request = new PostMethodWebRequest(importPath);
        request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
        request.setHeaderField("X-Xfer-Options", "raw");

        request.setHeaderField("Slug", Slug.encode(filename));
        setAuthentication(request);
        WebResponse postResponse = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
        String location = postResponse.getHeaderField("Location");
        assertNotNull(location);
        URI importURI = URIUtil.fromString(importPath);
        location = importURI.resolve(location).toString();

        doImport(source, length, location, "text/plain");

        //assert the file is present in the workspace
        assertTrue(checkFileExists(directoryPath + File.separator + filename));
        //assert that imported file has same content as original client.zip
        assertTrue(checkContentEquals(source, directoryPath + File.separator + filename));
    } finally {
        // Delete the temp file
        FileUtils.deleteQuietly(source);
    }
}

From source file:de.sub.goobi.metadaten.FileManipulation.java

/**
 * move files on server folder.// w  w  w .ja v a2 s.co  m
 */
public void exportFiles() throws IOException {
    if (selectedFiles == null || selectedFiles.isEmpty()) {
        Helper.setFehlerMeldung("noFileSelected");
        return;
    }
    List<DocStruct> allPages = metadataBean.getDigitalDocument().getPhysicalDocStruct().getAllChildren();
    List<String> filenamesToMove = new ArrayList<>();

    for (String fileIndex : selectedFiles) {
        try {
            int index = Integer.parseInt(fileIndex);
            filenamesToMove.add(allPages.get(index).getImageName());
        } catch (NumberFormatException e) {
            logger.error(e);
        }
    }
    URI tempDirectory = fileService.getTemporalDirectory();
    URI fileuploadFolder = fileService.createDirectory(tempDirectory, "fileupload");

    URI destination = fileuploadFolder.resolve(File.separator + metadataBean.getProcess().getTitle());
    if (!fileService.fileExist(destination)) {
        fileService.createDirectory(fileuploadFolder, metadataBean.getProcess().getTitle());
    }

    for (String filename : filenamesToMove) {
        String prefix = filename.replace(Metadaten.getFileExtension(filename), "");
        String processTitle = metadataBean.getProcess().getTitle();
        for (URI folder : metadataBean.getAllTifFolders()) {
            ArrayList<URI> filesInFolder = fileService.getSubUris(serviceManager.getFileService()
                    .getProcessSubTypeURI(metadataBean.getProcess(), ProcessSubType.IMAGE, folder.toString()));
            for (URI currentFile : filesInFolder) {

                String filenameInFolder = fileService.getFileName(currentFile);
                String filenamePrefix = filenameInFolder.replace(Metadaten.getFileExtension(filenameInFolder),
                        "");
                if (filenamePrefix.equals(prefix)) {
                    URI tempFolder = destination.resolve(File.separator + folder);
                    if (!fileService.fileExist(tempFolder)) {
                        fileService.createDirectory(destination, folder.toString());
                    }

                    URI destinationFile = tempFolder
                            .resolve(processTitle + "_" + fileService.getFileName(currentFile));

                    // if (deleteFilesAfterMove) {
                    // currentFile.renameTo(destinationFile);
                    // } else {
                    fileService.copyFile(currentFile, destinationFile);
                    // }
                    break;

                }

            }

        }
    }
    if (deleteFilesAfterMove) {
        String[] pagesArray = new String[selectedFiles.size()];
        selectedFiles.toArray(pagesArray);
        metadataBean.setAlleSeitenAuswahl(pagesArray);
        metadataBean.deleteSelectedPages();
        selectedFiles = new ArrayList<>();
        deleteFilesAfterMove = false;
    }

    metadataBean.retrieveAllImages();
    metadataBean.identifyImage(0);
}

From source file:org.eclipse.orion.server.cf.commands.GetLogCommand.java

public ServerStatus _doIt() {
    try {//from   w  w  w . ja va2 s .co m
        URI targetURI = URIUtil.toURI(target.getUrl());

        // Find the app
        GetAppCommand getAppCommand = new GetAppCommand(target, this.appName);
        IStatus getAppStatus = getAppCommand.doIt();
        if (!getAppStatus.isOK())
            return (ServerStatus) getAppStatus;

        String appUrl = getAppCommand.getApp().getAppJSON().getString("url");

        // check if crash log
        String computedInstanceNo = instanceNo;
        if ("Last Crash".equals(instanceNo)) {
            String crashedInstancesUrl = appUrl + "/crashes";
            URI crashedInstancesURI = targetURI.resolve(crashedInstancesUrl);

            GetMethod getCrashedInstancesMethod = new GetMethod(crashedInstancesURI.toString());
            HttpUtil.configureHttpMethod(getCrashedInstancesMethod, target);

            ServerStatus getStatus = HttpUtil.executeMethod(getCrashedInstancesMethod);
            if (!getStatus.isOK()) {
                return getStatus;
            }

            String response = getStatus.getJsonData().getString("response");
            JSONArray crashedInstances = new JSONArray(response);
            if (crashedInstances.length() > 0) {
                JSONObject crashedInstance = crashedInstances.getJSONObject(crashedInstances.length() - 1);
                computedInstanceNo = crashedInstance.getString("instance");
            }
        }

        String instanceLogsAppUrl = appUrl + "/instances/" + computedInstanceNo + "/files/logs";
        if (logName != null) {
            instanceLogsAppUrl += ("/" + logName);
        }

        URI instanceLogsAppURI = targetURI.resolve(instanceLogsAppUrl);

        GetMethod getInstanceLogMethod = new GetMethod(instanceLogsAppURI.toString());
        HttpUtil.configureHttpMethod(getInstanceLogMethod, target);

        ServerStatus getInstanceLogStatus = HttpUtil.executeMethod(getInstanceLogMethod);
        if (!getInstanceLogStatus.isOK()) {
            return getInstanceLogStatus;
        }

        String response = getInstanceLogStatus.getJsonData().optString("response");

        JSONObject jsonResp = new JSONObject();
        Log log = new Log(appName, logName);
        log.setContents(response);
        log.setLocation(new URI(baseRequestLocation));
        jsonResp.put(instanceNo, log.toJSON());

        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, jsonResp);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
        logger.error(msg, e);
        return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
    }
}

From source file:org.apache.archiva.admin.mock.ArchivaIndexManagerMock.java

private String calculateIndexRemoteUrl(URI baseUri, RemoteIndexFeature rif) {
    if (rif.getIndexUri() == null) {
        return baseUri.resolve(".index").toString();
    } else {//from ww w .  j a v  a  2 s.  c  o  m
        return baseUri.resolve(rif.getIndexUri()).toString();
    }
}

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

/**
 * Creates a MetaDirectory./*w  w w. ja  v  a2s.co m*/
 *
 * @param parentFolderUri
 *            The URI, where the
 * @param directoryName
 *            the name of the directory
 * @throws IOException
 *             an IOException
 */
public void createMetaDirectory(URI parentFolderUri, String directoryName) throws IOException {
    if (!fileExist(parentFolderUri.resolve(directoryName))) {
        CommandService commandService = serviceManager.getCommandService();
        List<String> commandParameter = Collections.singletonList(parentFolderUri + directoryName);
        commandService.runCommand(new File(ConfigCore.getParameter("script_createDirMeta")), commandParameter);
    }
}