Example usage for java.io File getCanonicalPath

List of usage examples for java.io File getCanonicalPath

Introduction

In this page you can find the example usage for java.io File getCanonicalPath.

Prototype

public String getCanonicalPath() throws IOException 

Source Link

Document

Returns the canonical pathname string of this abstract pathname.

Usage

From source file:com.norconex.jef4.suite.JobSuiteStatusIndexSerializerTest.java

@Test
public void testWriteJobSuiteIndex() throws IOException {

    File tempDirectory = new File(FileUtils.getTempDirectory(), "jef-test");

    JobSuiteConfig config = new JobSuiteConfig();

    config.setWorkdir(tempDirectory.getCanonicalPath());
    IJob job = new SleepyJob(5, 1);
    JobSuite suite = new JobSuite(job, config);
    Assert.assertTrue("Execution returned false.", suite.execute());

    JobSuiteStatusSnapshot tree = JobSuiteStatusSnapshot
            .newSnapshot(new File(tempDirectory, "latest/" + FileUtil.toSafeFileName(job.getId()) + ".index"));
    System.out.println("TREE: " + tree);
    Assert.assertEquals(1d, tree.getRoot().getProgress(), 0d);
}

From source file:com.adaptris.core.services.ScriptingServiceTest.java

public void testDoServiceWithFailingScript() throws Exception {
    ScriptingService service = createService();
    File script = writeScript(false);
    service.setScriptFilename(script.getCanonicalPath());
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage();
    try {/*  w  w w.  jav a 2s .  com*/
        execute(service, msg);
        fail("Service failure expected");
    } catch (ServiceException expected) {

    }
    delete(script);
}

From source file:foam.nanos.servlet.ImageServlet.java

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // get path/*from w w w . j  a  va2 s.com*/
    String cwd = System.getProperty("user.dir");
    String[] paths = getServletConfig().getInitParameter("paths").split(":");
    String reqPath = req.getRequestURI().replaceFirst("/?images/?", "/");

    // enumerate each file path
    for (int i = 0; i < paths.length; i++) {
        File src = new File(cwd + "/" + paths[i] + reqPath);
        if (src.isFile() && src.canRead()
                && src.getCanonicalPath().startsWith(new File(paths[i]).getCanonicalPath())) {
            String ext = EXTS.get(FilenameUtils.getExtension(src.getName()));
            try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(src))) {
                resp.setContentType(!SafetyUtil.isEmpty(ext) ? ext : DEFAULT_EXT);
                resp.setHeader("Content-Disposition",
                        "filename=\"" + StringEscapeUtils.escapeHtml4(src.getName()) + "\"");
                resp.setContentLengthLong(src.length());

                IOUtils.copy(is, resp.getOutputStream());
                return;
            }
        }
    }

    resp.sendError(resp.SC_NOT_FOUND);
}

From source file:com.blackducksoftware.integration.hub.detect.workflow.file.AirGapManager.java

private String getInspectorAirGapPath(File detectJar, final String inspectorLocationProperty,
        final String inspectorName) {
    if (StringUtils.isBlank(inspectorLocationProperty) && detectJar != null) {
        try {/*from   w  ww .ja v  a2 s  . c om*/
            final File inspectorsDirectory = new File(detectJar.getParentFile(), "packaged-inspectors");
            final File inspectorAirGapDirectory = new File(inspectorsDirectory, inspectorName);
            return inspectorAirGapDirectory.getCanonicalPath();
        } catch (final Exception e) {
            logger.debug(String.format(
                    "Exception encountered when guessing air gap path for %s, returning the detect property instead",
                    inspectorName));
            logger.debug(e.getMessage());
        }
    }
    return inspectorLocationProperty;
}

From source file:com.zimbra.cs.store.external.SimpleStoreManager.java

@Override
public String writeStreamToStore(InputStream in, long actualSize, Mailbox mbox) throws IOException {
    File destFile = getNewFile(mbox);
    FileUtil.copy(in, false, destFile);//  w ww.java  2 s  .  com
    return destFile.getCanonicalPath();
}

From source file:cleaner.ExternalHtmlCleaner.java

@Override
public String CleanText(String html, String encoding) {
    try {/*from w  w  w .  j  a v  a2 s  .  co m*/
        File tempIn = File.createTempFile("ExternalParserInput", ".tmp");
        String tempInName = tempIn.getCanonicalPath();
        File tempOut = File.createTempFile("ExternalParserOutput", ".tmp");
        String tempOutName = tempOut.getCanonicalPath();

        FileUtils.writeStringToFile(tempIn, html, encoding);

        String cmd = extScript + " " + tempInName + " " + tempOutName;
        System.out.println("Executing: " + cmd);
        Process proc = Runtime.getRuntime().exec(cmd);

        if (proc == null) {
            System.err.println("Cannot execute command: " + extScript);
            return null;
        }

        StringWriter err = new StringWriter();
        IOUtils.copy(proc.getErrorStream(), err, encoding);

        String ErrStr = err.toString();

        if (!ErrStr.isEmpty()) {
            System.err.println("External script " + extScript + " returned errors:");
            System.err.println(ErrStr);

            throw new Exception("External script " + extScript + " returned errors");
        }

        String out = FileUtils.readFileToString(tempOut);

        tempIn.delete();
        tempOut.delete();

        return LeoCleanerUtil.CollapseSpaces(out);
    } catch (Exception e) {
        System.err.println("Failed to run the script " + extScript + " Error: " + e);
        System.exit(1);
    }
    return null;
}

From source file:it.univaq.disim.connectorOTF.utils.SpringAlterationListener.java

@Override
public void onFileDelete(File file) {
    try {//  w w w  .  j av  a 2s . c  om
        // "file" is the reference to the removed file
        System.out.println("File removed: " + file.getCanonicalPath());
        // "file" does not exists anymore in the location
        System.out.println("File still exists in location: " + file.exists());
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:com.hs.mail.imap.schedule.MessageCompressor.java

private void compressFile(File srcFile) {
    File destFile = null;/*from w ww  .  j a  va  2 s  .c om*/
    try {
        destFile = new File(srcFile.getCanonicalPath() + DOT_GZIP_EXTENSION);
        FileUtils.compress(srcFile, destFile);
        srcFile.delete();
    } catch (IOException e) {
        if (srcFile.exists() && destFile != null && destFile.exists()) {
            destFile.delete();
        }
    }
}

From source file:co.cask.cdap.internal.app.runtime.webapp.ExplodeJarHttpHandler.java

@GET
@Path("**")
public void serve(HttpRequest request, HttpResponder responder) {
    try {/*ww  w. java2 s.  com*/

        String path = request.getUri();
        if (path == null) {
            responder.sendStatus(HttpResponseStatus.NOT_FOUND);
            return;
        }

        File file = new File(path);
        if (!file.getCanonicalPath().startsWith(cannonicalBaseDir)) {
            responder.sendStatus(HttpResponseStatus.NOT_FOUND);
            return;
        }

        if (!file.exists()) {
            responder.sendStatus(HttpResponseStatus.NOT_FOUND);
            return;
        }

        if (!file.isFile()) {
            responder.sendStatus(HttpResponseStatus.FORBIDDEN);
            return;
        }

        responder.sendFile(file, ImmutableMultimap.of(HttpHeaders.Names.CONTENT_TYPE,
                mimeTypesMap.getContentType(file.getAbsolutePath())));

    } catch (Throwable t) {
        LOG.error("Got exception: ", t);
        responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
}

From source file:com.adaptris.core.services.ScriptingServiceTest.java

public void testInit() throws Exception {
    ScriptingService service = new ScriptingService();
    try {// ww  w.j  ava2  s . c  o m
        service.init();
        fail("Service initialised w/o a language");
    } catch (Exception expected) {
        ;
    }
    service.setLanguage("jruby");
    service.setScriptFilename("/BLAHBLAHBLAHBLAHBLAH/BLAHBLAHBLAHBLAH");
    try {
        service.init();
        fail("Service initialised with no idiotic filename");
    } catch (Exception expected) {
        ;
    }
    File script = writeScript(false);
    service.setScriptFilename(script.getCanonicalPath());
    LifecycleHelper.init(service);
    LifecycleHelper.close(service);
    delete(script);
}