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