use of io.vertx.core.AsyncResult in project georocket by georocket.
the class StoreEndpoint method onPost.
/**
* Handles the HTTP POST request
* @param context the routing context
*/
private void onPost(RoutingContext context) {
HttpServerRequest request = context.request();
request.pause();
String layer = getEndpointPath(context);
String tagsStr = request.getParam("tags");
String propertiesStr = request.getParam("props");
String fallbackCRSString = request.getParam("fallbackCRS");
List<String> tags = StringUtils.isNotEmpty(tagsStr) ? Splitter.on(',').trimResults().splitToList(tagsStr) : null;
Map<String, Object> properties = new HashMap<>();
if (StringUtils.isNotEmpty(propertiesStr)) {
String regex = "(?<!" + Pattern.quote("\\") + ")" + Pattern.quote(":");
String[] parts = propertiesStr.split(",");
for (String part : parts) {
part = part.trim();
String[] property = part.split(regex);
if (property.length != 2) {
request.response().setStatusCode(400).end("Invalid property syntax: " + part);
return;
}
String key = StringEscapeUtils.unescapeJava(property[0].trim());
String value = StringEscapeUtils.unescapeJava(property[1].trim());
properties.put(key, value);
}
}
// get temporary filename
String incoming = storagePath + "/incoming";
String filename = new ObjectId().toString();
String filepath = incoming + "/" + filename;
String correlationId = UUID.randomUUID().toString();
long startTime = System.currentTimeMillis();
this.onReceivingFileStarted(correlationId, layer, startTime);
// create directory for incoming files
FileSystem fs = vertx.fileSystem();
ObservableFuture<Void> observable = RxHelper.observableFuture();
fs.mkdirs(incoming, observable.toHandler());
observable.flatMap(v -> {
// create temporary file
ObservableFuture<AsyncFile> openObservable = RxHelper.observableFuture();
fs.open(filepath, new OpenOptions(), openObservable.toHandler());
return openObservable;
}).flatMap(f -> {
// write request body into temporary file
ObservableFuture<Void> pumpObservable = RxHelper.observableFuture();
Handler<AsyncResult<Void>> pumpHandler = pumpObservable.toHandler();
Pump.pump(request, f).start();
Handler<Throwable> errHandler = (Throwable t) -> {
request.endHandler(null);
f.close();
pumpHandler.handle(Future.failedFuture(t));
};
f.exceptionHandler(errHandler);
request.exceptionHandler(errHandler);
request.endHandler(v -> {
f.close();
pumpHandler.handle(Future.succeededFuture());
});
request.resume();
return pumpObservable;
}).flatMap(v -> {
String contentTypeHeader = request.getHeader("Content-Type");
String mimeType = null;
try {
ContentType contentType = ContentType.parse(contentTypeHeader);
mimeType = contentType.getMimeType();
} catch (ParseException | IllegalArgumentException ex) {
// mimeType already null
}
// detect content type of file to import
if (mimeType == null || mimeType.trim().isEmpty() || mimeType.equals("application/octet-stream") || mimeType.equals("application/x-www-form-urlencoded")) {
// fallback: if the client has not sent a Content-Type or if it's
// a generic one, then try to guess it
log.debug("Mime type '" + mimeType + "' is invalid or generic. " + "Trying to guess the right type.");
return detectContentType(filepath).doOnNext(guessedType -> {
log.debug("Guessed mime type '" + guessedType + "'.");
});
}
return Observable.just(mimeType);
}).subscribe(detectedContentType -> {
long duration = System.currentTimeMillis() - startTime;
this.onReceivingFileFinished(correlationId, duration, layer, null);
// run importer
JsonObject msg = new JsonObject().put("filename", filename).put("layer", layer).put("contentType", detectedContentType).put("correlationId", correlationId);
if (tags != null) {
msg.put("tags", new JsonArray(tags));
}
if (!properties.isEmpty()) {
msg.put("properties", new JsonObject(properties));
}
if (fallbackCRSString != null) {
msg.put("fallbackCRSString", fallbackCRSString);
}
request.response().setStatusCode(// Accepted
202).putHeader("X-Correlation-Id", correlationId).setStatusMessage("Accepted file - importing in progress").end();
// run importer
vertx.eventBus().send(AddressConstants.IMPORTER_IMPORT, msg);
}, err -> {
long duration = System.currentTimeMillis() - startTime;
this.onReceivingFileFinished(correlationId, duration, layer, err);
fail(request.response(), err);
err.printStackTrace();
fs.delete(filepath, ar -> {
});
});
}
use of io.vertx.core.AsyncResult 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.core.AsyncResult in project vert.x by eclipse.
the class Http2ConnectionBase method updateSettings.
protected void updateSettings(Http2Settings settingsUpdate, Handler<AsyncResult<Void>> completionHandler) {
Http2Settings current = handler.decoder().localSettings();
for (Map.Entry<Character, Long> entry : current.entrySet()) {
Character key = entry.getKey();
if (Objects.equals(settingsUpdate.get(key), entry.getValue())) {
settingsUpdate.remove(key);
}
}
Handler<Void> pending = v -> {
synchronized (Http2ConnectionBase.this) {
localSettings.putAll(settingsUpdate);
}
if (completionHandler != null) {
completionHandler.handle(Future.succeededFuture());
}
};
updateSettingsHandlers.add(pending);
handler.writeSettings(settingsUpdate).addListener(fut -> {
if (!fut.isSuccess()) {
synchronized (Http2ConnectionBase.this) {
updateSettingsHandlers.remove(pending);
}
if (completionHandler != null) {
completionHandler.handle(Future.failedFuture(fut.cause()));
}
}
});
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class WebSocketEndpoint method tryConnect.
private void tryConnect(ContextInternal ctx, Handler<AsyncResult<HttpClientConnection>> handler) {
class Listener implements Handler<AsyncResult<HttpClientConnection>> {
private void onEvict() {
decRefCount();
Waiter h;
synchronized (WebSocketEndpoint.this) {
if (--inflightConnections > maxPoolSize || waiters.isEmpty()) {
return;
}
h = waiters.poll();
}
tryConnect(h.context, h.handler);
}
@Override
public void handle(AsyncResult<HttpClientConnection> ar) {
if (ar.succeeded()) {
HttpClientConnection c = ar.result();
if (incRefCount()) {
c.evictionHandler(v -> onEvict());
handler.handle(Future.succeededFuture(c));
} else {
c.close();
handler.handle(Future.failedFuture("Connection closed"));
}
} else {
handler.handle(Future.failedFuture(ar.cause()));
}
}
}
EventLoopContext eventLoopContext;
if (ctx instanceof EventLoopContext) {
eventLoopContext = (EventLoopContext) ctx;
} else {
eventLoopContext = ctx.owner().createEventLoopContext(ctx.nettyEventLoop(), ctx.workerPool(), ctx.classLoader());
}
connector.httpConnect(eventLoopContext, new Listener());
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class FakeStreamTest method testAsyncEnd.
@Test
public void testAsyncEnd() {
Promise<Void> end = Promise.promise();
AtomicInteger ended = new AtomicInteger();
AtomicReference<AsyncResult> endRes = new AtomicReference<>();
stream.setEnd(end.future());
stream.endHandler(v -> ended.incrementAndGet());
stream.end(endRes::set);
assertEquals(0, ended.get());
assertNull(endRes.get());
end.complete();
assertEquals(1, ended.get());
assertTrue(endRes.get().succeeded());
}
Aggregations