Search in sources :

Example 11 with InvalidPathException

use of java.nio.file.InvalidPathException in project jdk8u_jdk by JetBrains.

the class NulFile method test.

@SuppressWarnings("deprecation")
private static void test(File testFile, boolean derived) {
    boolean exceptionThrown = false;
    if (testFile == null) {
        throw new RuntimeException("test file should not be null.");
    }
    // getPath()
    if (testFile.getPath().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File path should contain Nul character");
    }
    // getAbsolutePath()
    if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File absolute path should contain Nul character");
    }
    // getAbsoluteFile()
    File derivedAbsFile = testFile.getAbsoluteFile();
    if (derived) {
        if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) {
            throw new RuntimeException("Derived file path should also contain Nul character");
        }
    } else {
        test(derivedAbsFile, true);
    }
    // getCanonicalPath()
    try {
        exceptionThrown = false;
        testFile.getCanonicalPath();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("getCanonicalPath() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // getCanonicalFile()
    try {
        exceptionThrown = false;
        testFile.getCanonicalFile();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("getCanonicalFile() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // toURL()
    try {
        exceptionThrown = false;
        testFile.toURL();
    } catch (MalformedURLException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("toURL() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // canRead()
    if (testFile.canRead())
        throw new RuntimeException("File should not be readable");
    // canWrite()
    if (testFile.canWrite())
        throw new RuntimeException("File should not be writable");
    // exists()
    if (testFile.exists())
        throw new RuntimeException("File should not be existed");
    // isDirectory()
    if (testFile.isDirectory())
        throw new RuntimeException("File should not be a directory");
    // isFile()
    if (testFile.isFile())
        throw new RuntimeException("File should not be a file");
    // isHidden()
    if (testFile.isHidden())
        throw new RuntimeException("File should not be hidden");
    // lastModified()
    if (testFile.lastModified() != 0L)
        throw new RuntimeException("File last modified time should be 0L");
    // length()
    if (testFile.length() != 0L)
        throw new RuntimeException("File length should be 0L");
    // createNewFile()
    try {
        exceptionThrown = false;
        testFile.createNewFile();
    } catch (IOException ex) {
        if (ExceptionMsg.equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("createNewFile() should throw IOException with" + " message \"" + ExceptionMsg + "\"");
    }
    // delete()
    if (testFile.delete())
        throw new RuntimeException("Delete operation should fail");
    // list()
    if (testFile.list() != null)
        throw new RuntimeException("File list() should return null");
    // list(FilenameFilter)
    FilenameFilter fnFilter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return false;
        }
    };
    if (testFile.list(fnFilter) != null) {
        throw new RuntimeException("File list(FilenameFilter) should" + " return null");
    }
    // listFiles()
    if (testFile.listFiles() != null)
        throw new RuntimeException("File listFiles() should return null");
    // listFiles(FilenameFilter)
    if (testFile.listFiles(fnFilter) != null) {
        throw new RuntimeException("File listFiles(FilenameFilter)" + " should return null");
    }
    // listFiles(FileFilter)
    FileFilter fFilter = new FileFilter() {

        @Override
        public boolean accept(File file) {
            return false;
        }
    };
    if (testFile.listFiles(fFilter) != null) {
        throw new RuntimeException("File listFiles(FileFilter)" + " should return null");
    }
    // mkdir()
    if (testFile.mkdir()) {
        throw new RuntimeException("File should not be able to" + " create directory");
    }
    // mkdirs()
    if (testFile.mkdirs()) {
        throw new RuntimeException("File should not be able to" + " create directories");
    }
    // renameTo(File)
    if (testFile.renameTo(new File("dest")))
        throw new RuntimeException("File rename should fail");
    if (new File("dest").renameTo(testFile))
        throw new RuntimeException("File rename should fail");
    try {
        exceptionThrown = false;
        testFile.renameTo(null);
    } catch (NullPointerException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("File rename should thrown NPE");
    }
    // setLastModified(long)
    if (testFile.setLastModified(0L)) {
        throw new RuntimeException("File should fail to set" + " last modified time");
    }
    try {
        exceptionThrown = false;
        testFile.setLastModified(-1);
    } catch (IllegalArgumentException ex) {
        if ("Negative time".equals(ex.getMessage()))
            exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("File should fail to set" + " last modified time with message \"Negative time\"");
    }
    // setReadOnly()
    if (testFile.setReadOnly())
        throw new RuntimeException("File should fail to set read-only");
    // setWritable(boolean writable, boolean ownerOnly)
    if (testFile.setWritable(true, true))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(true, false))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(false, true))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(false, false))
        throw new RuntimeException("File should fail to set writable");
    // setWritable(boolean writable)
    if (testFile.setWritable(false))
        throw new RuntimeException("File should fail to set writable");
    if (testFile.setWritable(true))
        throw new RuntimeException("File should fail to set writable");
    // setReadable(boolean readable, boolean ownerOnly)
    if (testFile.setReadable(true, true))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(true, false))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(false, true))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(false, false))
        throw new RuntimeException("File should fail to set readable");
    // setReadable(boolean readable)
    if (testFile.setReadable(false))
        throw new RuntimeException("File should fail to set readable");
    if (testFile.setReadable(true))
        throw new RuntimeException("File should fail to set readable");
    // setExecutable(boolean executable, boolean ownerOnly)
    if (testFile.setExecutable(true, true))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(true, false))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(false, true))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(false, false))
        throw new RuntimeException("File should fail to set executable");
    // setExecutable(boolean executable)
    if (testFile.setExecutable(false))
        throw new RuntimeException("File should fail to set executable");
    if (testFile.setExecutable(true))
        throw new RuntimeException("File should fail to set executable");
    // canExecute()
    if (testFile.canExecute())
        throw new RuntimeException("File should not be executable");
    // getTotalSpace()
    if (testFile.getTotalSpace() != 0L)
        throw new RuntimeException("The total space should be 0L");
    // getFreeSpace()
    if (testFile.getFreeSpace() != 0L)
        throw new RuntimeException("The free space should be 0L");
    // getUsableSpace()
    if (testFile.getUsableSpace() != 0L)
        throw new RuntimeException("The usable space should be 0L");
    // compareTo(File null)
    try {
        exceptionThrown = false;
        testFile.compareTo(null);
    } catch (NullPointerException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("compareTo(null) should throw NPE");
    }
    // toString()
    if (testFile.toString().indexOf(CHAR_NUL) < 0) {
        throw new RuntimeException("File path should contain Nul character");
    }
    // toPath()
    try {
        exceptionThrown = false;
        testFile.toPath();
    } catch (InvalidPathException ex) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("toPath() should throw" + " InvalidPathException");
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileFilter(java.io.FileFilter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) InvalidPathException(java.nio.file.InvalidPathException)

Example 12 with InvalidPathException

use of java.nio.file.InvalidPathException in project jimfs by google.

the class WindowsPathType method parsePath.

@Override
public ParseResult parsePath(String path) {
    String original = path;
    path = path.replace('/', '\\');
    if (WORKING_DIR_WITH_DRIVE.matcher(path).matches()) {
        throw new InvalidPathException(original, "Jimfs does not currently support the Windows syntax for a relative path " + "on a specific drive (e.g. \"C:foo\\bar\"");
    }
    String root;
    if (path.startsWith("\\\\")) {
        root = parseUncRoot(path, original);
    } else if (path.startsWith("\\")) {
        throw new InvalidPathException(original, "Jimfs does not currently support the Windows syntax for an absolute path " + "on the current drive (e.g. \"\\foo\\bar\"");
    } else {
        root = parseDriveRoot(path);
    }
    // check for root.length() > 3 because only "C:\" type roots are allowed to have :
    int startIndex = root == null || root.length() > 3 ? 0 : root.length();
    for (int i = startIndex; i < path.length(); i++) {
        char c = path.charAt(i);
        if (isReserved(c)) {
            throw new InvalidPathException(original, "Illegal char <" + c + ">", i);
        }
    }
    Matcher trailingSpaceMatcher = TRAILING_SPACES.matcher(path);
    if (trailingSpaceMatcher.find()) {
        throw new InvalidPathException(original, "Trailing char < >", trailingSpaceMatcher.start());
    }
    if (root != null) {
        path = path.substring(root.length());
        if (!root.endsWith("\\")) {
            root = root + "\\";
        }
    }
    return new ParseResult(root, splitter().split(path));
}
Also used : Matcher(java.util.regex.Matcher) InvalidPathException(java.nio.file.InvalidPathException)

Example 13 with InvalidPathException

use of java.nio.file.InvalidPathException in project nifi by apache.

the class UnpackContent method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    PackageFormat packagingFormat = PackageFormat.getFormat(context.getProperty(PACKAGING_FORMAT).getValue().toLowerCase());
    if (packagingFormat == PackageFormat.AUTO_DETECT_FORMAT) {
        packagingFormat = null;
        final String mimeType = flowFile.getAttribute(CoreAttributes.MIME_TYPE.key());
        if (mimeType == null) {
            logger.error("No mime.type attribute set for {}; routing to failure", new Object[] { flowFile });
            session.transfer(flowFile, REL_FAILURE);
            return;
        }
        for (PackageFormat format : PackageFormat.values()) {
            if (mimeType.toLowerCase().equals(format.getMimeType())) {
                packagingFormat = format;
            }
        }
        if (packagingFormat == null) {
            logger.info("Cannot unpack {} because its mime.type attribute is set to '{}', which is not a format that can be unpacked; routing to 'success'", new Object[] { flowFile, mimeType });
            session.transfer(flowFile, REL_SUCCESS);
            return;
        }
    }
    // set the Unpacker to use for this FlowFile.  FlowFileUnpackager objects maintain state and are not reusable.
    final Unpacker unpacker;
    final boolean addFragmentAttrs;
    switch(packagingFormat) {
        case TAR_FORMAT:
        case X_TAR_FORMAT:
            unpacker = tarUnpacker;
            addFragmentAttrs = true;
            break;
        case ZIP_FORMAT:
            unpacker = zipUnpacker;
            addFragmentAttrs = true;
            break;
        case FLOWFILE_STREAM_FORMAT_V2:
            unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV2());
            addFragmentAttrs = false;
            break;
        case FLOWFILE_STREAM_FORMAT_V3:
            unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3());
            addFragmentAttrs = false;
            break;
        case FLOWFILE_TAR_FORMAT:
            unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV1());
            addFragmentAttrs = false;
            break;
        case AUTO_DETECT_FORMAT:
        default:
            // The format of the unpacker should be known before initialization
            throw new ProcessException(packagingFormat + " is not a valid packaging format");
    }
    final List<FlowFile> unpacked = new ArrayList<>();
    try {
        unpacker.unpack(session, flowFile, unpacked);
        if (unpacked.isEmpty()) {
            logger.error("Unable to unpack {} because it does not appear to have any entries; routing to failure", new Object[] { flowFile });
            session.transfer(flowFile, REL_FAILURE);
            return;
        }
        if (addFragmentAttrs) {
            finishFragmentAttributes(session, flowFile, unpacked);
        }
        session.transfer(unpacked, REL_SUCCESS);
        final String fragmentId = unpacked.size() > 0 ? unpacked.get(0).getAttribute(FRAGMENT_ID) : null;
        flowFile = FragmentAttributes.copyAttributesToOriginal(session, flowFile, fragmentId, unpacked.size());
        session.transfer(flowFile, REL_ORIGINAL);
        session.getProvenanceReporter().fork(flowFile, unpacked);
        logger.info("Unpacked {} into {} and transferred to success", new Object[] { flowFile, unpacked });
    } catch (final ProcessException | InvalidPathException e) {
        logger.error("Unable to unpack {} due to {}; routing to failure", new Object[] { flowFile, e });
        session.transfer(flowFile, REL_FAILURE);
        session.remove(unpacked);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) ArrayList(java.util.ArrayList) ComponentLog(org.apache.nifi.logging.ComponentLog) InvalidPathException(java.nio.file.InvalidPathException) FlowFileUnpackagerV3(org.apache.nifi.util.FlowFileUnpackagerV3) ProcessException(org.apache.nifi.processor.exception.ProcessException) FlowFileUnpackagerV2(org.apache.nifi.util.FlowFileUnpackagerV2) FlowFileUnpackagerV1(org.apache.nifi.util.FlowFileUnpackagerV1)

Example 14 with InvalidPathException

use of java.nio.file.InvalidPathException in project ddf by codice.

the class FileSystemStorageProvider method setBaseContentDirectory.

@SuppressFBWarnings
public void setBaseContentDirectory(final String baseDirectory) throws IOException {
    Path directory;
    if (!baseDirectory.isEmpty()) {
        String path = tryCanonicalizeDirectory(baseDirectory);
        try {
            directory = Paths.get(path, DEFAULT_CONTENT_REPOSITORY, DEFAULT_CONTENT_STORE);
        } catch (InvalidPathException e) {
            path = System.getProperty(KARAF_HOME);
            directory = Paths.get(path, DEFAULT_CONTENT_REPOSITORY, DEFAULT_CONTENT_STORE);
        }
    } else {
        String path = System.getProperty("karaf.home");
        directory = Paths.get(path, DEFAULT_CONTENT_REPOSITORY, DEFAULT_CONTENT_STORE);
    }
    Path directories;
    if (!Files.exists(directory)) {
        directories = Files.createDirectories(directory);
        LOGGER.debug("Setting base content directory to: {}", directories.toAbsolutePath().toString());
    } else {
        directories = directory;
    }
    Path tmpDirectories;
    Path tmpDirectory = Paths.get(directories.toAbsolutePath().toString(), DEFAULT_TMP);
    if (!Files.exists(tmpDirectory)) {
        tmpDirectories = Files.createDirectories(tmpDirectory);
        LOGGER.debug("Setting base content directory to: {}", tmpDirectory.toAbsolutePath().toString());
    } else {
        tmpDirectories = tmpDirectory;
    }
    this.baseContentDirectory = directories;
    this.baseContentTmpDirectory = tmpDirectories;
}
Also used : Path(java.nio.file.Path) InvalidPathException(java.nio.file.InvalidPathException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 15 with InvalidPathException

use of java.nio.file.InvalidPathException in project jdk8u_jdk by JetBrains.

the class ProxyClassesDumper method getInstance.

public static ProxyClassesDumper getInstance(String path) {
    if (null == path) {
        return null;
    }
    try {
        path = path.trim();
        final Path dir = Paths.get(path.length() == 0 ? "." : path);
        AccessController.doPrivileged(new PrivilegedAction<Void>() {

            @Override
            public Void run() {
                validateDumpDir(dir);
                return null;
            }
        }, null, new FilePermission("<<ALL FILES>>", "read, write"));
        return new ProxyClassesDumper(dir);
    } catch (InvalidPathException ex) {
        PlatformLogger.getLogger(ProxyClassesDumper.class.getName()).warning("Path " + path + " is not valid - dumping disabled", ex);
    } catch (IllegalArgumentException iae) {
        PlatformLogger.getLogger(ProxyClassesDumper.class.getName()).warning(iae.getMessage() + " - dumping disabled");
    }
    return null;
}
Also used : Path(java.nio.file.Path) FilePermission(java.io.FilePermission) InvalidPathException(java.nio.file.InvalidPathException)

Aggregations

InvalidPathException (java.nio.file.InvalidPathException)97 Path (java.nio.file.Path)53 IOException (java.io.IOException)26 URL (java.net.URL)14 File (java.io.File)13 MalformedURLException (java.net.MalformedURLException)12 Test (org.junit.Test)11 AlluxioURI (alluxio.AlluxioURI)7 Resource (org.springframework.core.io.Resource)7 Nullable (org.springframework.lang.Nullable)7 URI (java.net.URI)6 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 URIStatus (alluxio.client.file.URIStatus)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 InputStream (java.io.InputStream)4 FileOutStream (alluxio.client.file.FileOutStream)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 SetAttributePOptions (alluxio.grpc.SetAttributePOptions)2