Search in sources :

Example 1 with Buffer

use of io.vertx.rxjava.core.buffer.Buffer in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toObservable().flatMap(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable());
    }).reduce(Buffer::appendBuffer).map(buffer -> buffer.toString("UTF-8")).subscribe(data -> System.out.println("Server content " + data));
    // End request
    req.end();
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient)

Example 2 with Buffer

use of io.vertx.rxjava.core.buffer.Buffer in project rulesservice by genny-project.

the class RulesLoader method processFileRealms.

private static List<Tuple3<String, String, String>> processFileRealms(final String realm, String inputFileStr) {
    File file = new File(inputFileStr);
    String fileName = inputFileStr.replaceFirst(".*/(\\w+).*", "$1");
    String fileNameExt = inputFileStr.replaceFirst(".*/\\w+\\.(.*)", "$1");
    List<Tuple3<String, String, String>> rules = new ArrayList<Tuple3<String, String, String>>();
    if (!file.isFile()) {
        // DIRECTORY
        if (!fileName.startsWith("XX")) {
            String localRealm = realm;
            if (fileName.startsWith("prj_") || fileName.startsWith("PRJ_")) {
                // extract realm name
                localRealm = fileName.substring("prj_".length()).toLowerCase();
            }
            final List<String> filesList = Vertx.currentContext().owner().fileSystem().readDirBlocking(inputFileStr);
            for (final String dirFileStr : filesList) {
                // use
                List<Tuple3<String, String, String>> childRules = processFileRealms(localRealm, dirFileStr);
                // directory
                // name
                // as
                // rulegroup
                rules.addAll(childRules);
            }
        }
        return rules;
    } else {
        Buffer buf = Vertx.currentContext().owner().fileSystem().readFileBlocking(inputFileStr);
        try {
            if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("drl"))) {
                // ignore files that start
                // with XX
                final String ruleText = buf.toString();
                Tuple3<String, String, String> rule = (Tuple.of(realm, fileName + "." + fileNameExt, ruleText));
                System.out.println(realm + " Loading in Rule:" + rule._1 + " of " + inputFileStr);
                rules.add(rule);
            } else if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("bpmn"))) {
                // ignore files
                // that start
                // with XX
                final String bpmnText = buf.toString();
                Tuple3<String, String, String> bpmn = (Tuple.of(realm, fileName + "." + fileNameExt, bpmnText));
                System.out.println(realm + " Loading in BPMN:" + bpmn._1 + " of " + inputFileStr);
                rules.add(bpmn);
            } else if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("xls"))) {
                // ignore files that
                // start with XX
                final String xlsText = buf.toString();
                Tuple3<String, String, String> xls = (Tuple.of(realm, fileName + "." + fileNameExt, xlsText));
                System.out.println(realm + " Loading in XLS:" + xls._1 + " of " + inputFileStr);
                rules.add(xls);
            }
            return rules;
        } catch (final DecodeException dE) {
        }
    }
    return null;
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) Tuple3(io.vavr.Tuple3) ArrayList(java.util.ArrayList) File(java.io.File) DecodeException(io.vertx.core.json.DecodeException)

Example 3 with Buffer

use of io.vertx.rxjava.core.buffer.Buffer in project georocket by georocket.

the class ImporterVerticle method importJSON.

/**
 * Imports a JSON file from the given input stream into the store
 * @param f the JSON file to read
 * @param correlationId a unique identifier for this import process
 * @param filename the name of the file currently being imported
 * @param timestamp denotes when the import process has started
 * @param layer the layer where the file should be stored (may be null)
 * @param tags the list of tags to attach to the file (may be null)
 * @param properties the map of properties to attach to the file (may be null)
 * @return a single that will emit when the file has been imported
 */
protected Single<Integer> importJSON(ReadStream<Buffer> f, String correlationId, String filename, long timestamp, String layer, List<String> tags, Map<String, Object> properties) {
    UTF8BomFilter bomFilter = new UTF8BomFilter();
    StringWindow window = new StringWindow();
    GeoJsonSplitter splitter = new GeoJsonSplitter(window);
    AtomicInteger processing = new AtomicInteger(0);
    return f.toObservable().map(buf -> (io.vertx.core.buffer.Buffer) buf.getDelegate()).map(bomFilter::filter).doOnNext(window::append).lift(new JsonParserOperator()).flatMap(splitter::onEventObservable).flatMapSingle(result -> {
        IndexMeta indexMeta = new IndexMeta(correlationId, filename, timestamp, tags, properties, null);
        return addToStoreWithPause(result, layer, indexMeta, f, processing);
    }).count().toSingle();
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) IndexMeta(io.georocket.storage.IndexMeta) StringWindow(io.georocket.util.StringWindow) XMLParserOperator(io.georocket.util.XMLParserOperator) ChunkMeta(io.georocket.storage.ChunkMeta) Window(io.georocket.util.Window) LoggerFactory(io.vertx.core.logging.LoggerFactory) RxStore(io.georocket.storage.RxStore) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Single(rx.Single) JsonParserOperator(io.georocket.util.JsonParserOperator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Result(io.georocket.input.Splitter.Result) Map(java.util.Map) FileSystem(io.vertx.rxjava.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject) StoreFactory(io.georocket.storage.StoreFactory) UTF8BomFilter(io.georocket.util.UTF8BomFilter) Logger(io.vertx.core.logging.Logger) Message(io.vertx.rxjava.core.eventbus.Message) XMLSplitter(io.georocket.input.xml.XMLSplitter) OpenOptions(io.vertx.core.file.OpenOptions) GeoJsonSplitter(io.georocket.input.geojson.GeoJsonSplitter) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) FirstLevelSplitter(io.georocket.input.xml.FirstLevelSplitter) Collectors(java.util.stream.Collectors) XMLCRSIndexer(io.georocket.index.xml.XMLCRSIndexer) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) MimeTypeUtils.belongsTo(io.georocket.util.MimeTypeUtils.belongsTo) ReadStream(io.vertx.rxjava.core.streams.ReadStream) AddressConstants(io.georocket.constants.AddressConstants) RxUtils(io.georocket.util.RxUtils) ConfigConstants(io.georocket.constants.ConfigConstants) Buffer(io.vertx.rxjava.core.buffer.Buffer) StringWindow(io.georocket.util.StringWindow) GeoJsonSplitter(io.georocket.input.geojson.GeoJsonSplitter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UTF8BomFilter(io.georocket.util.UTF8BomFilter) IndexMeta(io.georocket.storage.IndexMeta) JsonParserOperator(io.georocket.util.JsonParserOperator)

Example 4 with Buffer

use of io.vertx.rxjava.core.buffer.Buffer in project georocket by georocket.

the class RemoteElasticsearchClient method performRequest.

/**
 * Perform an HTTP request
 * @param req the request to perform
 * @param body the body to send in the request (may be null)
 * @return an observable emitting the parsed response body (may be null if no
 * body was received)
 */
private Observable<JsonObject> performRequest(HttpClientRequest req, String body) {
    ObservableFuture<JsonObject> observable = RxHelper.observableFuture();
    Handler<AsyncResult<JsonObject>> handler = observable.toHandler();
    req.exceptionHandler(t -> {
        handler.handle(Future.failedFuture(t));
    });
    req.handler(res -> {
        int code = res.statusCode();
        if (code == 200) {
            Buffer buf = Buffer.buffer();
            res.handler(b -> {
                buf.appendBuffer(b);
            });
            res.endHandler(v -> {
                if (buf.length() > 0) {
                    handler.handle(Future.succeededFuture(buf.toJsonObject()));
                } else {
                    handler.handle(Future.succeededFuture());
                }
            });
        } else {
            handler.handle(Future.failedFuture(new HttpException(code)));
        }
    });
    if (body != null) {
        req.setChunked(false);
        Buffer buf = Buffer.buffer(body);
        req.putHeader("Content-Length", String.valueOf(buf.length()));
        req.end(buf);
    } else {
        req.end();
    }
    return observable;
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) JsonObject(io.vertx.core.json.JsonObject) HttpException(io.georocket.util.HttpException) AsyncResult(io.vertx.core.AsyncResult)

Example 5 with Buffer

use of io.vertx.rxjava.core.buffer.Buffer in project rulesservice by genny-project.

the class RuleTest method processFile.

private static List<Tuple2<String, String>> processFile(final Vertx vertx, String inputFileStr) {
    File file = new File(inputFileStr);
    String fileName = inputFileStr.replaceFirst(".*/(\\w+).*", "$1");
    String fileNameExt = inputFileStr.replaceFirst(".*/\\w+\\.(.*)", "$1");
    List<Tuple2<String, String>> rules = new ArrayList<Tuple2<String, String>>();
    if (!file.isFile()) {
        final List<String> filesList = vertx.fileSystem().readDirBlocking(inputFileStr);
        for (final String dirFileStr : filesList) {
            // use directory name as
            List<Tuple2<String, String>> childRules = processFile(vertx, dirFileStr);
            // rulegroup
            rules.addAll(childRules);
        }
        return rules;
    } else {
        Buffer buf = vertx.fileSystem().readFileBlocking(inputFileStr);
        try {
            if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("drl"))) {
                // ignore files that start
                // with XX
                final String ruleText = buf.toString();
                Tuple2<String, String> rule = (Tuple.of(fileName + "." + fileNameExt, ruleText));
                System.out.println("Loading in Rule:" + rule._1 + " of " + inputFileStr);
                rules.add(rule);
            } else if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("bpmn"))) {
                // ignore files
                // that start
                // with XX
                final String bpmnText = buf.toString();
                Tuple2<String, String> bpmn = (Tuple.of(fileName + "." + fileNameExt, bpmnText));
                System.out.println("Loading in BPMN:" + bpmn._1 + " of " + inputFileStr);
                rules.add(bpmn);
            }
            return rules;
        } catch (final DecodeException dE) {
        }
    }
    return null;
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) Tuple2(io.vavr.Tuple2) ArrayList(java.util.ArrayList) File(java.io.File) DecodeException(io.vertx.core.json.DecodeException)

Aggregations

Buffer (io.vertx.rxjava.core.buffer.Buffer)6 DecodeException (io.vertx.core.json.DecodeException)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Tuple2 (io.vavr.Tuple2)2 JsonObject (io.vertx.core.json.JsonObject)2 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)2 AddressConstants (io.georocket.constants.AddressConstants)1 ConfigConstants (io.georocket.constants.ConfigConstants)1 XMLCRSIndexer (io.georocket.index.xml.XMLCRSIndexer)1 Result (io.georocket.input.Splitter.Result)1 GeoJsonSplitter (io.georocket.input.geojson.GeoJsonSplitter)1 FirstLevelSplitter (io.georocket.input.xml.FirstLevelSplitter)1 XMLSplitter (io.georocket.input.xml.XMLSplitter)1 ChunkMeta (io.georocket.storage.ChunkMeta)1 IndexMeta (io.georocket.storage.IndexMeta)1 RxStore (io.georocket.storage.RxStore)1 StoreFactory (io.georocket.storage.StoreFactory)1 HttpException (io.georocket.util.HttpException)1 JsonParserOperator (io.georocket.util.JsonParserOperator)1