Search in sources :

Example 1 with NoarkNotAcceptableException

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();
}
Also used : InputStream(java.io.InputStream) NoarkNotAcceptableException(nikita.util.exceptions.NoarkNotAcceptableException) Resource(org.springframework.core.io.Resource) DocumentObject(nikita.model.noark5.v4.DocumentObject) NoarkEntityNotFoundException(nikita.util.exceptions.NoarkEntityNotFoundException) IOException(java.io.IOException) Counted(com.codahale.metrics.annotation.Counted) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Counted (com.codahale.metrics.annotation.Counted)1 Timed (com.codahale.metrics.annotation.Timed)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DocumentObject (nikita.model.noark5.v4.DocumentObject)1 NoarkEntityNotFoundException (nikita.util.exceptions.NoarkEntityNotFoundException)1 NoarkNotAcceptableException (nikita.util.exceptions.NoarkNotAcceptableException)1 Resource (org.springframework.core.io.Resource)1