Search in sources :

Example 86 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project zuul by Netflix.

the class FilterProcessor method addNewClasses.

static void addNewClasses(Filer filer, Collection<String> elements) throws IOException {
    String resourceName = "META-INF/zuul/allfilters";
    List<String> existing = Collections.emptyList();
    try {
        FileObject existingFilters = filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
        try (InputStream is = existingFilters.openInputStream();
            InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
            existing = readResourceFile(reader);
        }
    } catch (FileNotFoundException | NoSuchFileException e) {
    // Perhaps log this.
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    int sizeBefore = existing.size();
    Set<String> existingSet = new LinkedHashSet<>(existing);
    List<String> newElements = new ArrayList<>(existingSet);
    for (String element : elements) {
        if (existingSet.add(element)) {
            newElements.add(element);
        }
    }
    if (newElements.size() == sizeBefore) {
        // nothing to do.
        return;
    }
    newElements.sort(String::compareTo);
    FileObject dest = filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
    try (OutputStream os = dest.openOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
        writeResourceFile(osw, newElements);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) FileObject(javax.tools.FileObject)

Example 87 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project ghostdriver by detro.

the class GetFixtureHttpRequestCallback method call.

@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
    // Construct path to the file
    Path filePath = FileSystems.getDefault().getPath(FIXTURE_PATH, req.getPathInfo());
    // If the file exists
    if (filePath.toFile().exists()) {
        try {
            // Set Content Type
            res.setContentType(filePathToMimeType(filePath.toString()));
            // Read and write to response
            Files.copy(filePath, res.getOutputStream());
            return;
        } catch (NoSuchFileException nsfe) {
            LOG.warning(nsfe.getClass().getName());
        } catch (IOException ioe) {
            LOG.warning(ioe.getClass().getName());
        } catch (RuntimeException re) {
            LOG.warning(re.getClass().getName());
        }
    }
    LOG.warning("Fixture NOT FOUND: " + filePath);
    // < Not Found
    res.sendError(404);
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 88 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project guava by google.

the class MoreFiles method throwDeleteFailed.

/**
 * Throws an exception indicating that one or more files couldn't be deleted when deleting {@code
 * path} or its contents.
 *
 * <p>If there is only one exception in the collection, and it is a {@link NoSuchFileException}
 * thrown because {@code path} itself didn't exist, then throws that exception. Otherwise, the
 * thrown exception contains all the exceptions in the given collection as suppressed exceptions.
 */
private static void throwDeleteFailed(Path path, Collection<IOException> exceptions) throws FileSystemException {
    NoSuchFileException pathNotFound = pathNotFound(path, exceptions);
    if (pathNotFound != null) {
        throw pathNotFound;
    }
    // TODO(cgdecker): Should there be a custom exception type for this?
    // Also, should we try to include the Path of each file we may have failed to delete rather
    // than just the exceptions that occurred?
    FileSystemException deleteFailed = new FileSystemException(path.toString(), null, "failed to delete one or more files; see suppressed exceptions for details");
    for (IOException e : exceptions) {
        deleteFailed.addSuppressed(e);
    }
    throw deleteFailed;
}
Also used : FileSystemException(java.nio.file.FileSystemException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 89 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project es6draft by anba.

the class ModuleOperations method toScriptException.

private static ScriptException toScriptException(ExecutionContext cx, Throwable e, SourceIdentifier moduleId, SourceIdentifier referredId) {
    if (e instanceof CompletionException && e.getCause() != null) {
        e = e.getCause();
    }
    ScriptException exception;
    if (e instanceof NoSuchFileException) {
        exception = new ResolutionException(Messages.Key.ModulesUnresolvedModule, moduleId.toString(), referredId.toString()).toScriptException(cx);
    } else if (e instanceof IOException) {
        exception = newInternalError(cx, e, Messages.Key.ModulesIOException, Objects.toString(e.getMessage(), ""));
    } else if (e instanceof InternalThrowable) {
        exception = ((InternalThrowable) e).toScriptException(cx);
    } else {
        cx.getRuntimeContext().getErrorReporter().accept(cx, e);
        exception = newInternalError(cx, e, Messages.Key.InternalError, Objects.toString(e.getMessage(), ""));
    }
    return exception;
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) InternalThrowable(com.github.anba.es6draft.runtime.internal.InternalThrowable) CompletionException(java.util.concurrent.CompletionException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 90 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project undertow by undertow-io.

the class PathResource method serveImpl.

private void serveImpl(final Sender sender, final HttpServerExchange exchange, final long start, final long end, final IoCallback callback, final boolean range) {
    abstract class BaseFileTask implements Runnable {

        protected volatile FileChannel fileChannel;

        protected boolean openFile() {
            try {
                fileChannel = FileChannel.open(file, StandardOpenOption.READ);
                if (range) {
                    fileChannel.position(start);
                }
            } catch (NoSuchFileException e) {
                exchange.setStatusCode(StatusCodes.NOT_FOUND);
                callback.onException(exchange, sender, e);
                return false;
            } catch (IOException e) {
                exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                callback.onException(exchange, sender, e);
                return false;
            }
            return true;
        }
    }
    class ServerTask extends BaseFileTask implements IoCallback {

        private PooledByteBuffer pooled;

        long remaining = end - start + 1;

        @Override
        public void run() {
            if (range && remaining == 0) {
                // we are done
                if (pooled != null) {
                    pooled.close();
                    pooled = null;
                }
                IoUtils.safeClose(fileChannel);
                callback.onComplete(exchange, sender);
                return;
            }
            if (fileChannel == null) {
                if (!openFile()) {
                    return;
                }
                pooled = exchange.getConnection().getByteBufferPool().allocate();
            }
            if (pooled != null) {
                ByteBuffer buffer = pooled.getBuffer();
                try {
                    buffer.clear();
                    int res = fileChannel.read(buffer);
                    if (res == -1) {
                        // we are done
                        pooled.close();
                        IoUtils.safeClose(fileChannel);
                        callback.onComplete(exchange, sender);
                        return;
                    }
                    buffer.flip();
                    if (range) {
                        if (buffer.remaining() > remaining) {
                            buffer.limit((int) (buffer.position() + remaining));
                        }
                        remaining -= buffer.remaining();
                    }
                    sender.send(buffer, this);
                } catch (IOException e) {
                    onException(exchange, sender, e);
                }
            }
        }

        @Override
        public void onComplete(final HttpServerExchange exchange, final Sender sender) {
            if (exchange.isInIoThread()) {
                exchange.dispatch(this);
            } else {
                run();
            }
        }

        @Override
        public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
            UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
            if (pooled != null) {
                pooled.close();
                pooled = null;
            }
            IoUtils.safeClose(fileChannel);
            if (!exchange.isResponseStarted()) {
                exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
            }
            callback.onException(exchange, sender, exception);
        }
    }
    class TransferTask extends BaseFileTask {

        @Override
        public void run() {
            if (!openFile()) {
                return;
            }
            sender.transferFrom(fileChannel, new IoCallback() {

                @Override
                public void onComplete(HttpServerExchange exchange, Sender sender) {
                    try {
                        IoUtils.safeClose(fileChannel);
                    } finally {
                        callback.onComplete(exchange, sender);
                    }
                }

                @Override
                public void onException(HttpServerExchange exchange, Sender sender, IOException exception) {
                    try {
                        IoUtils.safeClose(fileChannel);
                    } finally {
                        callback.onException(exchange, sender, exception);
                    }
                }
            });
        }
    }
    BaseFileTask task;
    try {
        task = manager.getTransferMinSize() > Files.size(file) || range ? new ServerTask() : new TransferTask();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (exchange.isInIoThread()) {
        exchange.dispatch(task);
    } else {
        task.run();
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) IoCallback(io.undertow.io.IoCallback)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11