use of io.vertx.core.http.HttpClientRequest in project raml-module-builder by folio-org.
the class DemoRamlRestTest method checkURLs.
public void checkURLs(TestContext context, String url, int codeExpected, String accept) {
try {
Async async = context.async();
HttpMethod method = HttpMethod.GET;
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = client.requestAbs(method, url, new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse httpClientResponse) {
log.info(httpClientResponse.statusCode() + ", " + codeExpected + " status expected: " + url);
context.assertEquals(codeExpected, httpClientResponse.statusCode(), url);
httpClientResponse.bodyHandler(body -> {
log.info(body.toString());
});
async.complete();
}
});
request.exceptionHandler(error -> {
context.fail(url + " - " + error.getMessage());
async.complete();
});
request.headers().add("x-okapi-tenant", TENANT);
request.headers().add("Accept", accept);
request.setChunked(true);
request.end();
} catch (Throwable e) {
log.error(e.getMessage(), e);
} finally {
}
}
use of io.vertx.core.http.HttpClientRequest in project georocket by georocket.
the class AbstractClient method update.
/**
* <p>Update a field in the chunk metadata (such as properties or tags).</p>
* <p>The chunks are either specified by a <code>query</code> or
* <code>layer</code> information or both. If none is given, all chunks in the
* data store will be updated.</p>
* @param method the http method to use for the update
* @param fieldEndpoint the path of the endpoint which should be used
* @param fieldName the name of the field to update
* @param query a search query specifying the chunks, whose
* fields should be updated (or <code>null</code> if the
* fields of all chunks in all sub-layers from the given
* <code>layer</code> should be updated)
* @param layer the absolute path to the layer from which to update
* fields (or <code>null</code> if the entire store should be
* queried to find the chunks, whose fields should be updated)
* @param updates a collection of values to update
* @param handler a handler that will be called when the operation
* has finished
* @since 1.1.0
*/
protected void update(HttpMethod method, String fieldEndpoint, String fieldName, String query, String layer, List<String> updates, Handler<AsyncResult<Void>> handler) {
if ((query == null || query.isEmpty()) && (layer == null || layer.isEmpty())) {
handler.handle(Future.failedFuture("No search query and no layer given. " + "Do you really wish to update all chunks in the GeoRocket " + "data store? If so, set the layer to '/'."));
return;
}
String queryPath = prepareQuery(query, layer);
if (query == null || query.isEmpty()) {
queryPath += "?";
} else {
queryPath += "&";
}
String values = updates.stream().map(this::urlencode).collect(Collectors.joining(","));
String path = fieldEndpoint + queryPath + fieldName + "=" + values;
HttpClientRequest request = client.request(method, path);
request.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
request.handler(response -> {
if (response.statusCode() != 204) {
fail(response, handler);
} else {
response.endHandler(v -> handler.handle(Future.succeededFuture()));
}
});
configureRequest(request).end();
}
use of io.vertx.core.http.HttpClientRequest in project georocket by georocket.
the class StoreClient method search.
/**
* <p>Search the GeoRocket data store and return a {@link ReadStream} of
* merged chunks matching the given criteria.</p>
* <p>If <code>query</code> is <code>null</code> or empty all chunks from
* the given <code>layer</code> (and all sub-layers) will be returned. If
* <code>layer</code> is also <code>null</code> or empty the contents of the
* whole data store will be returned.</p>
* <p>The caller is responsible for handling exceptions through
* {@link ReadStream#exceptionHandler(Handler)}.</p>
* @param query a search query specifying which chunks to return (may be
* <code>null</code>)
* @param layer the name of the layer where to search for chunks recursively
* (may be <code>null</code>)
* @param handler a handler that will receive the {@link ReadStream} from
* which the merged chunks matching the given criteria can be read
*/
public void search(String query, String layer, Handler<AsyncResult<ReadStream<Buffer>>> handler) {
if ((query == null || query.isEmpty()) && (layer == null || layer.isEmpty())) {
handler.handle(Future.failedFuture("No search query and no layer given. " + "Do you really wish to export/query the whole data store? If so, " + "set the layer to '/'."));
return;
}
String queryPath = prepareQuery(query, layer);
HttpClientRequest request = client.get(getEndpoint() + queryPath);
request.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
request.handler(response -> {
if (response.statusCode() == 404) {
fail(response, handler, message -> new NoSuchElementException(ClientAPIException.parse(message).getMessage()));
} else if (response.statusCode() != 200) {
fail(response, handler);
} else {
handler.handle(Future.succeededFuture(response));
}
});
configureRequest(request).end();
}
use of io.vertx.core.http.HttpClientRequest in project georocket by georocket.
the class S3Store method doAddChunk.
@Override
protected void doAddChunk(String chunk, String path, Handler<AsyncResult<String>> handler) {
if (path == null || path.isEmpty()) {
path = "/";
}
// generate new file name
String id = generateChunkId();
String filename = PathUtils.join(path, id);
String key = PathUtils.removeLeadingSlash(filename);
vertx.<URL>executeBlocking(f -> {
f.complete(generatePresignedUrl(key, HttpMethod.PUT));
}, ar -> {
if (ar.failed()) {
handler.handle(Future.failedFuture(ar.cause()));
return;
}
URL u = ar.result();
log.debug("PUT " + u);
Buffer chunkBuf = Buffer.buffer(chunk);
HttpClientRequest request = client.put(u.getFile());
request.putHeader("Host", u.getHost());
request.putHeader("Content-Length", String.valueOf(chunkBuf.length()));
request.exceptionHandler(t -> {
handler.handle(Future.failedFuture(t));
});
request.handler(response -> {
Buffer errorBody = Buffer.buffer();
if (response.statusCode() != 200) {
response.handler(buf -> {
errorBody.appendBuffer(buf);
});
}
response.endHandler(v -> {
if (response.statusCode() == 200) {
handler.handle(Future.succeededFuture(filename));
} else {
log.error(errorBody);
handler.handle(Future.failedFuture(response.statusMessage()));
}
});
});
request.end(chunkBuf);
});
}
use of io.vertx.core.http.HttpClientRequest in project georocket by georocket.
the class S3Store method doDeleteChunks.
@Override
protected void doDeleteChunks(Queue<String> paths, Handler<AsyncResult<Void>> handler) {
if (paths.isEmpty()) {
handler.handle(Future.succeededFuture());
return;
}
String key = PathUtils.removeLeadingSlash(PathUtils.normalize(paths.poll()));
vertx.<URL>executeBlocking(f -> {
f.complete(generatePresignedUrl(key, HttpMethod.DELETE));
}, ar -> {
if (ar.failed()) {
handler.handle(Future.failedFuture(ar.cause()));
return;
}
URL u = ar.result();
log.debug("DELETE " + u);
HttpClientRequest request = client.delete(ar.result().getFile());
request.putHeader("Host", u.getHost());
request.exceptionHandler(t -> {
handler.handle(Future.failedFuture(t));
});
request.handler(response -> {
Buffer errorBody = Buffer.buffer();
if (response.statusCode() != 204) {
response.handler(buf -> {
errorBody.appendBuffer(buf);
});
}
response.endHandler(v -> {
switch(response.statusCode()) {
case 204:
case 404:
doDeleteChunks(paths, handler);
break;
default:
log.error(errorBody);
handler.handle(Future.failedFuture(response.statusMessage()));
}
});
});
request.end();
});
}
Aggregations