Skip to content

Commit

Permalink
fix(downloads): set content-type and content-length headers (#706)
Browse files Browse the repository at this point in the history
* fix(downloads): set content-type and content-length headers

* chore(web-client): update to latest

* test(recording-get): fix broken test

* chore(recording-get): apply spotless formatting

* Revert "chore(web-client): update to latest"

This reverts commit fc11421.

Co-authored-by: Elliott Baron <ebaron@redhat.com>
  • Loading branch information
andrewazores and ebaron authored Oct 13, 2021
1 parent 19855bc commit 450c822
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.recordings.RecordingNotFoundException;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
Expand Down Expand Up @@ -95,6 +97,11 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
String recordingName = ctx.pathParam("recordingName");
try {
Path archivedRecording = recordingArchiveHelper.getRecordingPath(recordingName).get();
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.OCTET_STREAM.mime());
ctx.response()
.putHeader(
HttpHeaders.CONTENT_LENGTH,
Long.toString(archivedRecording.toFile().length()));
ctx.response().sendFile(archivedRecording.toString());
} catch (ExecutionException e) {
if (e.getCause() instanceof RecordingNotFoundException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
try {
Path report = reportService.get(recordingName).get();
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.HTML.mime());
ctx.response()
.putHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(report.toFile().length()));
ctx.response().sendFile(report.toAbsolutePath().toString());
} catch (ExecutionException | CompletionException ee) {
if (ExceptionUtils.getRootCause(ee) instanceof RecordingNotFoundException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@
*/
package io.cryostat.net.web.http.api.v1;

import java.io.File;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import io.cryostat.net.AuthManager;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.recordings.RecordingNotFoundException;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -136,9 +139,15 @@ void shouldHandleSuccessfulGETRequest() throws Exception {

Path archivedRecording = Mockito.mock(Path.class);
Mockito.when(future.get()).thenReturn(archivedRecording);
Mockito.when(archivedRecording.toString()).thenReturn("/path/to/recording.jfr");
File file = Mockito.mock(File.class);
Mockito.when(archivedRecording.toFile()).thenReturn(file);
Mockito.when(file.length()).thenReturn(12345L);

handler.handle(ctx);

Mockito.verify(resp).sendFile(Mockito.anyString());
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.OCTET_STREAM.mime());
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_LENGTH, "12345");
Mockito.verify(resp).sendFile(archivedRecording.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -125,7 +125,12 @@ void shouldRespondBySendingFile() throws Exception {
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

Path fakePath = Paths.get("/some/fake/path.html");
Path fakePath = Mockito.mock(Path.class);
Mockito.when(fakePath.toAbsolutePath()).thenReturn(fakePath);
Mockito.when(fakePath.toString()).thenReturn("/some/fake/path.html");
File file = Mockito.mock(File.class);
Mockito.when(fakePath.toFile()).thenReturn(file);
Mockito.when(file.length()).thenReturn(12345L);

when(ctx.pathParam("recordingName")).thenReturn("someRecording");
when(reportService.get(Mockito.anyString()))
Expand All @@ -136,6 +141,7 @@ void shouldRespondBySendingFile() throws Exception {
Mockito.verify(reportService).get("someRecording");
Mockito.verify(resp).sendFile(fakePath.toString());
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_TYPE, "text/html");
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_LENGTH, "12345");
}

@Test
Expand Down

0 comments on commit 450c822

Please sign in to comment.