Search in sources :

Example 11 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ImportCommand method importFile.

/**
 * Upload a file to GeoRocket
 * @param path path to file to import
 * @param client the GeoRocket client
 * @param vertx the Vert.x instance
 * @return an observable that will emit when the file has been uploaded
 */
protected Observable<Void> importFile(String path, GeoRocketClient client, Vertx vertx) {
    // open file
    FileSystem fs = vertx.fileSystem();
    OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
    return fs.rxOpen(path, openOptions).flatMap(f -> fs.rxProps(path).map(props -> Pair.of(f, props.size()))).flatMapObservable(f -> {
        ObservableFuture<Void> o = RxHelper.observableFuture();
        Handler<AsyncResult<Void>> handler = o.toHandler();
        AsyncFile file = f.getLeft().getDelegate();
        WriteStream<Buffer> out = client.getStore().startImport(layer, tags, properties, Optional.of(f.getRight()), fallbackCRS, handler);
        AtomicBoolean fileClosed = new AtomicBoolean();
        Pump pump = Pump.pump(file, out);
        file.endHandler(v -> {
            file.close();
            out.end();
            fileClosed.set(true);
        });
        Handler<Throwable> exceptionHandler = t -> {
            if (!fileClosed.get()) {
                file.endHandler(null);
                file.close();
            }
            handler.handle(Future.failedFuture(t));
        };
        file.exceptionHandler(exceptionHandler);
        out.exceptionHandler(exceptionHandler);
        pump.start();
        return o;
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OptionParserException(de.undercouch.underline.OptionParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(org.apache.commons.lang3.tuple.Pair) FileSet(org.apache.tools.ant.types.FileSet) WriteStream(io.vertx.core.streams.WriteStream) FileSystem(io.vertx.rxjava.core.file.FileSystem) Project(org.apache.tools.ant.Project) UnknownAttributes(de.undercouch.underline.UnknownAttributes) Pump(io.vertx.core.streams.Pump) AsyncResult(io.vertx.core.AsyncResult) Splitter(com.google.common.base.Splitter) OptionDesc(de.undercouch.underline.OptionDesc) PrintWriter(java.io.PrintWriter) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) SystemUtils(org.apache.commons.lang3.SystemUtils) InputReader(de.undercouch.underline.InputReader) DurationFormat(io.georocket.util.DurationFormat) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) GeoRocketClient(io.georocket.client.GeoRocketClient) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) RxHelper(io.vertx.rx.java.RxHelper) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) ArgumentType(de.undercouch.underline.Option.ArgumentType) Handler(io.vertx.core.Handler) FilenameUtils(org.apache.commons.io.FilenameUtils) Vertx(io.vertx.rxjava.core.Vertx) Buffer(io.vertx.core.buffer.Buffer) Pump(io.vertx.core.streams.Pump) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileSystem(io.vertx.rxjava.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

Vertx (io.vertx.rxjava.core.Vertx)11 Async (io.vertx.ext.unit.Async)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)5 Test (org.junit.Test)5 TestContext (io.vertx.ext.unit.TestContext)4 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 Record (io.vertx.servicediscovery.Record)4 IOException (java.io.IOException)4 Observable (rx.Observable)4 JsonObject (io.vertx.core.json.JsonObject)3 Splitter (com.google.common.base.Splitter)2 InputReader (de.undercouch.underline.InputReader)2 ArgumentType (de.undercouch.underline.Option.ArgumentType)2 OptionDesc (de.undercouch.underline.OptionDesc)2 OptionParserException (de.undercouch.underline.OptionParserException)2 UnknownAttributes (de.undercouch.underline.UnknownAttributes)2 GeoRocketClient (io.georocket.client.GeoRocketClient)2