use of nikita.util.exceptions.NoarkNotAcceptableException in project nikita-noark5-core by HiOA-ABI.
the class DocumentObjectHateoasController method handleFileDownload.
// Get a file identified by systemID retrievable with referanseFile
// GET [contextPath][api]/arkivstruktur/dokumentobjekt/{systemID}/referanseFil
@ApiOperation(value = "Downloads a file associated with the documentObject identified by a systemId", response = DocumentObjectHateoas.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "File uploaded successfully", response = DocumentObjectHateoas.class), @ApiResponse(code = 401, message = API_MESSAGE_UNAUTHENTICATED_USER), @ApiResponse(code = 403, message = API_MESSAGE_UNAUTHORISED_FOR_USER), @ApiResponse(code = 500, message = API_MESSAGE_INTERNAL_SERVER_ERROR) })
@Counted
@Timed
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS + SLASH + REFERENCE_FILE, method = RequestMethod.GET)
public void handleFileDownload(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the documentObject that has a file associated with it", required = true) @PathVariable("systemID") final String documentObjectSystemId) throws IOException {
DocumentObject documentObject = documentObjectService.findBySystemIdOrderBySystemId(documentObjectSystemId);
if (documentObject == null) {
throw new NoarkEntityNotFoundException(documentObjectSystemId);
}
Resource fileResource = documentObjectService.loadAsResource(documentObject);
String acceptType = request.getHeader(HttpHeaders.ACCEPT);
if (acceptType != null && !acceptType.equalsIgnoreCase(documentObject.getMimeType())) {
if (!acceptType.equals("*/*")) {
throw new NoarkNotAcceptableException("The request [" + request.getRequestURI() + "] is not acceptable. " + "You have issued an Accept: " + acceptType + ", while the mimeType you are trying to retrieve " + "is [" + documentObject.getMimeType() + "].");
}
}
response.setContentType(documentObject.getMimeType());
response.setContentLength(documentObject.getFileSize().intValue());
response.addHeader("Content-disposition", "inline; filename=" + documentObject.getOriginalFilename());
response.addHeader("Content-Type", documentObject.getMimeType());
InputStream filestream = fileResource.getInputStream();
try {
long bytesTotal = IOUtils.copyLarge(filestream, response.getOutputStream());
filestream.close();
} finally {
try {
// Try close without exceptions if copy() threw an
// exception. If close() is called twice, the second
// close() should be ignored.
filestream.close();
} catch (IOException e) {
// swallow any error to expose exceptions from
// IOUtil.copy() if the second close() failed.
}
}
response.flushBuffer();
}
Aggregations