Search in sources :

Example 1 with HttpException

use of io.georocket.util.HttpException in project georocket by georocket.

the class StoreEndpoint method detectContentType.

/**
 * Try to detect the content type of a file
 * @param filepath the absolute path to the file to analyse
 * @return an observable emitting either the detected content type or an error
 * if the content type could not be detected or the file could not be read
 */
private Observable<String> detectContentType(String filepath) {
    ObservableFuture<String> result = RxHelper.observableFuture();
    Handler<AsyncResult<String>> resultHandler = result.toHandler();
    vertx.<String>executeBlocking(f -> {
        try {
            String mimeType = MimeTypeUtils.detect(new File(filepath));
            if (mimeType == null) {
                log.warn("Could not detect file type for " + filepath + ". Using " + "application/octet-stream.");
                mimeType = "application/octet-stream";
            }
            f.complete(mimeType);
        } catch (IOException e) {
            f.fail(e);
        }
    }, ar -> {
        if (ar.failed()) {
            resultHandler.handle(Future.failedFuture(ar.cause()));
        } else {
            String ct = ar.result();
            if (ct != null) {
                resultHandler.handle(Future.succeededFuture(ar.result()));
            } else {
                resultHandler.handle(Future.failedFuture(new HttpException(215)));
            }
        }
    });
    return result;
}
Also used : HttpException(io.georocket.util.HttpException) IOException(java.io.IOException) AsyncResult(io.vertx.core.AsyncResult) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

Example 2 with HttpException

use of io.georocket.util.HttpException 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 3 with HttpException

use of io.georocket.util.HttpException in project georocket by georocket.

the class ElasticsearchInstaller method doDownload.

/**
 * Download a file
 * @param downloadUrl the URL to download from
 * @param dest the destination file
 * @return a single emitting exactly one item once the file has been
 * downloaded
 */
private Single<Void> doDownload(String downloadUrl, AsyncFile dest) {
    ObservableFuture<Void> observable = RxHelper.observableFuture();
    Handler<AsyncResult<Void>> handler = observable.toHandler();
    HttpClientOptions options = new HttpClientOptions();
    if (downloadUrl.startsWith("https")) {
        options.setSsl(true);
    }
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest req = client.getAbs(downloadUrl);
    req.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
    req.handler(res -> {
        if (res.statusCode() != 200) {
            handler.handle(Future.failedFuture(new HttpException(res.statusCode(), res.statusMessage())));
            return;
        }
        // get content-length
        int length;
        int[] read = { 0 };
        int[] lastOutput = { 0 };
        String contentLength = res.getHeader("Content-Length");
        if (contentLength != null) {
            length = Integer.parseInt(contentLength);
        } else {
            length = 0;
        }
        res.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
        // download file contents, log progress
        res.handler(buf -> {
            read[0] += buf.length();
            if (lastOutput[0] == 0 || read[0] - lastOutput[0] > 1024 * 2048) {
                logProgress(length, read[0]);
                lastOutput[0] = read[0];
            }
            dest.write(buf);
        });
        res.endHandler(v -> {
            logProgress(length, read[0]);
            handler.handle(Future.succeededFuture());
        });
    });
    req.end();
    return observable.toSingle();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpException(io.georocket.util.HttpException) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpException (io.georocket.util.HttpException)3 AsyncResult (io.vertx.core.AsyncResult)3 AsyncFile (io.vertx.core.file.AsyncFile)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 Buffer (io.vertx.rxjava.core.buffer.Buffer)1 HttpClient (io.vertx.rxjava.core.http.HttpClient)1 HttpClientRequest (io.vertx.rxjava.core.http.HttpClientRequest)1 File (java.io.File)1 IOException (java.io.IOException)1