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);
}
}
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);
}
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;
}
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;
}
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();
}
}
Aggregations