Search in sources :

Example 66 with HttpClientRequest

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 {
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RestVerticle(org.folio.rest.RestVerticle) BeforeClass(org.junit.BeforeClass) AES(org.folio.rest.security.AES) Date(java.util.Date) MimeBodyPart(javax.mail.internet.MimeBodyPart) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) VertxUtils(org.folio.rest.tools.utils.VertxUtils) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) BodyPart(javax.mail.BodyPart) BigDecimal(java.math.BigDecimal) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Locale(java.util.Locale) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JsonObject(io.vertx.core.json.JsonObject) Datetime(org.folio.rest.jaxrs.model.Datetime) Metadata(org.folio.rest.jaxrs.model.Metadata) Logger(io.vertx.core.logging.Logger) MimeMultipart(javax.mail.internet.MimeMultipart) AfterClass(org.junit.AfterClass) RmbtestsClient(org.folio.rest.client.RmbtestsClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) Test(org.junit.Test) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) Data(org.folio.rest.jaxrs.model.Data) File(java.io.File) NetworkUtils(org.folio.rest.tools.utils.NetworkUtils) Book(org.folio.rest.jaxrs.model.Book) IOUtils(org.apache.commons.io.IOUtils) Messages(org.folio.rest.tools.messages.Messages) List(java.util.List) InternetHeaders(javax.mail.internet.InternetHeaders) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AdminClient(org.folio.rest.client.AdminClient) HttpMethod(io.vertx.core.http.HttpMethod) SecretKey(javax.crypto.SecretKey) PersistMethod(org.folio.rest.jaxrs.resource.AdminResource.PersistMethod) Handler(io.vertx.core.Handler) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpMethod(io.vertx.core.http.HttpMethod)

Example 67 with HttpClientRequest

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();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest)

Example 68 with HttpClientRequest

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();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) NoSuchElementException(java.util.NoSuchElementException)

Example 69 with HttpClientRequest

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);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) URL(java.net.URL)

Example 70 with HttpClientRequest

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();
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) URL(java.net.URL)

Aggregations

HttpClientRequest (io.vertx.core.http.HttpClientRequest)159 Test (org.junit.Test)82 Buffer (io.vertx.core.buffer.Buffer)73 HttpClient (io.vertx.core.http.HttpClient)56 HttpMethod (io.vertx.core.http.HttpMethod)51 HttpClientOptions (io.vertx.core.http.HttpClientOptions)50 HttpClientResponse (io.vertx.core.http.HttpClientResponse)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 Handler (io.vertx.core.Handler)40 HttpServerResponse (io.vertx.core.http.HttpServerResponse)40 CompletableFuture (java.util.concurrent.CompletableFuture)38 CountDownLatch (java.util.concurrent.CountDownLatch)36 TimeUnit (java.util.concurrent.TimeUnit)36 Vertx (io.vertx.core.Vertx)35 HttpServerOptions (io.vertx.core.http.HttpServerOptions)35 ArrayList (java.util.ArrayList)35 List (java.util.List)35 AtomicReference (java.util.concurrent.atomic.AtomicReference)34 MultiMap (io.vertx.core.MultiMap)33 HttpVersion (io.vertx.core.http.HttpVersion)31