Example usage for io.vertx.core.http.impl MimeMapping getMimeTypeForFilename

List of usage examples for io.vertx.core.http.impl MimeMapping getMimeTypeForFilename

Introduction

In this page you can find the example usage for io.vertx.core.http.impl MimeMapping getMimeTypeForFilename.

Prototype

public static String getMimeTypeForFilename(String filename) 

Source Link

Usage

From source file:io.knotx.mocks.adapter.MockAdapterHandler.java

License:Apache License

protected String getContentType(ClientRequest request) {
    return Optional.ofNullable(MimeMapping.getMimeTypeForFilename(request.getPath())).orElse(DEFAULT_MIME);
}

From source file:io.knotx.mocks.adapter.MockRemoteRepositoryHandler.java

License:Apache License

@Override
public void handle(RoutingContext context) {
    String resourcePath = catalogue + SEPARATOR + getContentPath(context.request().path());
    final Optional<String> contentType = Optional.ofNullable(MimeMapping.getMimeTypeForFilename(resourcePath));
    final String fileExtension = getFileExtension(resourcePath);
    final boolean isTextFile = fileExtension != null && TEST_FILES_EXTENSIONS.contains(fileExtension);

    vertx.fileSystem().readFile(resourcePath, ar -> {
        HttpServerResponse response = context.response();
        if (ar.succeeded()) {
            LOGGER.info("Mocked clientRequest [{}] fetch data from file [{}]", context.request().path(),
                    resourcePath);/* w w  w.  j av  a2 s  .c o m*/
            Buffer fileContent = ar.result();
            generateResponse(context.request().path(), () -> {
                setHeaders(response, contentType, isTextFile);
                response.setStatusCode(HttpResponseStatus.OK.code()).end(fileContent);
            });
        } else {
            LOGGER.error("Unable to read file.", ar.cause());
            context.fail(404);
        }
    });
}

From source file:io.knotx.mocks.adapter.MockServiceHandler.java

License:Apache License

private String getContentType(RoutingContext context) {
    return Optional.ofNullable(MimeMapping.getMimeTypeForFilename(context.request().path()))
            .orElse(DEFAULT_MIME);
}

From source file:io.knotx.repository.impl.FilesystemRepositoryConnectorProxyImpl.java

License:Apache License

@Override
public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) {
    final String localFilePath = catalogue + StringUtils.stripStart(request.getPath(), "/");
    final Optional<String> contentType = Optional.ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath));

    LOGGER.trace("Fetching file `{}` from local repository.", localFilePath);

    ObservableFuture<Buffer> fileObservable = RxHelper.observableFuture();
    fileObservable/*from   w  ww .  j  a v a 2  s  . c om*/
            .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code())
                    .setHeaders(headers(contentType)).setBody(buffer))
            .defaultIfEmpty(new ClientResponse().setStatusCode(HttpResponseStatus.NOT_FOUND.code()))
            .subscribe(response -> result.handle(Future.succeededFuture(response)), error -> {
                LOGGER.error(ERROR_MESSAGE, error);
                result.handle(Future.succeededFuture(processError(error)));
            });

    fileSystem.readFile(localFilePath, fileObservable.toHandler());
}