Skip to content

Commit

Permalink
added content length header when downloading files via API #1212
Browse files Browse the repository at this point in the history
  • Loading branch information
nunovieira220 authored and luis100 committed May 30, 2018
1 parent 82c4c2e commit 97cdd79
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ public interface ConsumesOutputStream {
String getFileName();

String getMediaType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ public String getFileName() {
public String getMediaType() {
return BIN_MEDIA_TYPE;
}

};

}

return stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class StreamResponse implements EntityResponse {
private String filename;
private String mediaType;
private long fileSize = -1;
private ConsumesOutputStream stream;

public StreamResponse(String filename, String mediaType, ConsumesOutputStream stream) {
Expand All @@ -19,6 +20,14 @@ public StreamResponse(String filename, String mediaType, ConsumesOutputStream st
this.stream = stream;
}

public StreamResponse(String filename, String mediaType, long fileSize, ConsumesOutputStream stream) {
super();
this.filename = filename;
this.mediaType = mediaType;
this.fileSize = fileSize;
this.stream = stream;
}

public String getFilename() {
return filename;
}
Expand All @@ -37,6 +46,14 @@ public void setMediaType(String mediaType) {
this.mediaType = mediaType;
}

public long getFileSize() {
return fileSize;
}

public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}

public ConsumesOutputStream getStream() {
return stream;
}
Expand All @@ -45,4 +62,5 @@ public void setStream(ConsumesOutputStream stream) {
this.stream = stream;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,8 @@ public void consumeOutputStream(OutputStream out) throws IOException {
}
}
};
return new StreamResponse(filename, mediaType, stream);

return new StreamResponse(filename, mediaType, iFile.getSize(), stream);
} else if (iFile.isDirectory() && (RodaConstants.API_QUERY_VALUE_ACCEPT_FORMAT_ZIP.equals(acceptFormat)
|| RodaConstants.API_QUERY_VALUE_ACCEPT_FORMAT_BIN.equals(acceptFormat))) {
Directory directory = RodaCoreFactory.getStorageService().getDirectory(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,21 @@ public static Response okResponse(StreamResponse streamResponse, CacheControl ca
public static Response okResponse(StreamResponse streamResponse, CacheControl cacheControl, Date lastModifiedDate,
boolean inline) {
StreamingOutput so = new StreamingOutput() {

@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
streamResponse.getStream().consumeOutputStream(output);

}
};
return Response.ok(so, streamResponse.getMediaType())
.header(HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"")
.cacheControl(cacheControl).lastModified(lastModifiedDate).build();

Response.ResponseBuilder response = Response.ok(so, streamResponse.getMediaType()).header(
HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"");

if (streamResponse.getFileSize() > 0) {
response.header(HttpHeaders.CONTENT_LENGTH, streamResponse.getFileSize());
}

return response.cacheControl(cacheControl).lastModified(lastModifiedDate).build();
}

public static Response okResponse(StreamResponse streamResponse, CacheControl cacheControl, EntityTag tag) {
Expand All @@ -191,17 +195,21 @@ public static Response okResponse(StreamResponse streamResponse, CacheControl ca
public static Response okResponse(StreamResponse streamResponse, CacheControl cacheControl, EntityTag tag,
boolean inline) {
StreamingOutput so = new StreamingOutput() {

@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
streamResponse.getStream().consumeOutputStream(output);

}
};
return Response.ok(so, streamResponse.getMediaType())
.header(HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"")
.cacheControl(cacheControl).tag(tag).build();

Response.ResponseBuilder response = Response.ok(so, streamResponse.getMediaType()).header(
HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"");

if (streamResponse.getFileSize() > 0) {
response.header(HttpHeaders.CONTENT_LENGTH, streamResponse.getFileSize());
}

return response.cacheControl(cacheControl).tag(tag).build();
}

public static Response okResponse(StreamResponse streamResponse) {
Expand All @@ -210,17 +218,21 @@ public static Response okResponse(StreamResponse streamResponse) {

public static Response okResponse(StreamResponse streamResponse, boolean inline) {
StreamingOutput so = new StreamingOutput() {

@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
streamResponse.getStream().consumeOutputStream(output);

}
};
return Response.ok(so, streamResponse.getMediaType())
.header(HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"")
.build();

Response.ResponseBuilder response = Response.ok(so, streamResponse.getMediaType()).header(
HttpHeaders.CONTENT_DISPOSITION,
contentDisposition(inline) + CONTENT_DISPOSITION_FILENAME_ARGUMENT + "\"" + streamResponse.getFilename() + "\"");

if (streamResponse.getFileSize() > 0) {
response.header(HttpHeaders.CONTENT_LENGTH, streamResponse.getFileSize());
}

return response.build();
}

private static String contentDisposition(boolean inline) {
Expand All @@ -247,7 +259,6 @@ public static URI getUriFromRequest(HttpServletRequest request) throws RODAExcep
}
}

@SuppressWarnings("unchecked")
public static <T extends IsIndexed, R extends IsModelObject> RODAObjectList<R> indexedResultToRODAObjectList(
Class<T> objectClass, IndexResult<T> result)
throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException {
Expand Down Expand Up @@ -331,7 +342,7 @@ public static <T extends IsIndexed> Response okResponse(T indexed, String accept
} else if (RodaConstants.API_QUERY_VALUE_ACCEPT_FORMAT_JSON.equals(acceptFormat)
|| RodaConstants.API_QUERY_VALUE_ACCEPT_FORMAT_XML.equals(acceptFormat)) {
AIP aip = RodaCoreFactory.getModelService().retrieveAIP(indexedAIP.getId());
representation = new ObjectResponse<AIP>(acceptFormat, aip);
representation = new ObjectResponse<>(acceptFormat, aip);
} else {
throw new GenericException("Unsupported class: " + acceptFormat);
}
Expand Down

0 comments on commit 97cdd79

Please sign in to comment.