@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.debug("Fetching file `{}` from local repository.", localFilePath); fileSystem.rxReadFile(localFilePath) .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code()) .setHeaders(headers(contentType)).setBody(buffer.getDelegate())) .subscribe( response -> result.handle(Future.succeededFuture(response)), error -> { LOGGER.error(ERROR_MESSAGE, error); result.handle(Future.succeededFuture(processError(error))); } ); }
@Override public void handle(RoutingContext context) { String resourcePath = catalogue + SEPARATOR + getContentPath(context.request().path()); final Optional<String> contentType = Optional .ofNullable(MimeMapping.getMimeTypeForFilename(resourcePath)); RepositoryFileExtension fileExtension = RepositoryFileExtension.fromFilename(resourcePath); vertx.fileSystem().readFile(resourcePath, ar -> { HttpServerResponse response = context.response(); if (ar.succeeded()) { LOGGER.info("Mocked clientRequest [{}] fetch data from file [{}]", context.request().path(), resourcePath); Buffer fileContent = ar.result(); generateResponse(context.request().path(), () -> { setHeaders(response, contentType, fileExtension.isTextFile()); response.setStatusCode(fileExtension.responseStatus.code()).end(fileContent); }); } else { LOGGER.error("Unable to read file.", ar.cause()); context.fail(404); } }); }
protected String getContentType(ClientRequest request) { return Optional.ofNullable(MimeMapping.getMimeTypeForFilename(request.getPath())) .orElse(DEFAULT_MIME); }
private String getContentType(RoutingContext context) { return Optional.ofNullable(MimeMapping.getMimeTypeForFilename(context.request().path())) .orElse(DEFAULT_MIME); }