Search in sources :

Example 81 with ResponseEntity

use of org.springframework.http.ResponseEntity in project nikita-noark5-core by HiOA-ABI.

the class DocumentObjectHateoasController method handleFileUpload.

// API - All POST Requests (CRUD - CREATE)
// upload a file and associate it with a documentObject
// POST [contextPath][api]/arkivstruktur/dokumentobjekt/{systemID}/referanseFil
@ApiOperation(value = "Uploads a file and associates it 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.POST, headers = "Accept=*/*", produces = { NOARK5_V4_CONTENT_TYPE_JSON, NOARK5_V4_CONTENT_TYPE_JSON_XML })
public ResponseEntity<DocumentObjectHateoas> handleFileUpload(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the documentObject you wish to associate a file with", required = true) @PathVariable("systemID") final String documentObjectSystemId) {
    try {
        DocumentObject documentObject = documentObjectService.findBySystemIdOrderBySystemId(documentObjectSystemId);
        if (documentObject == null) {
            throw new NoarkEntityNotFoundException(documentObjectSystemId);
        }
        InputStream inputStream;
        // Following will be needed for uploading file in chunks
        //String headerContentRange = request.getHeader("content-range");//Content-Range:bytes 737280-819199/845769
        // Check that content-length is set, > 0 and in agreement with the value set in documentObject
        Long contentLength = 0L;
        if (request.getHeader("content-length") == null) {
            throw new StorageException("Attempt to upload a document without content-length set. The document " + "was attempted to be associated with " + documentObject);
        }
        contentLength = (long) request.getIntHeader("content-length");
        if (contentLength < 1) {
            throw new StorageException("Attempt to upload a document with 0 or negative content-length set. " + "Actual value was (" + contentLength + "). The document  was attempted to be associated with " + documentObject);
        }
        if (null == documentObject.getFileSize()) {
            throw new StorageException("Attempt to upload a document with a content-length set in the header (" + contentLength + "), but the value in documentObject has not been set (== null).  The " + "document was attempted to be associated with " + documentObject);
        }
        if (!contentLength.equals(documentObject.getFileSize())) {
            throw new StorageException("Attempt to upload a document with a content-length set in the header (" + contentLength + ") that is not the same as the value in documentObject (" + documentObject.getFileSize() + ").  The document was attempted to be associated with " + documentObject);
        }
        // Check that the content-type is set and in agreement with mimeType value in documentObject
        String headerContentType = request.getHeader("content-type");
        if (headerContentType == null) {
            throw new StorageException("Attempt to upload a document without content-type set. The document " + "was attempted to be associated with " + documentObject);
        }
        if (!headerContentType.equals(documentObject.getMimeType())) {
            throw new StorageException("Attempt to upload a document with a content-type set in the header (" + contentLength + ") that is not the same as the mimeType in documentObject (" + documentObject.getMimeType() + ").  The document was attempted to be associated with " + documentObject);
        }
        documentObjectService.storeAndCalculateChecksum(request.getInputStream(), documentObject);
        // We need to update the documentObject in the database as checksum and checksum algorithm are set after
        // the document has been uploaded
        documentObjectService.update(documentObject);
        DocumentObjectHateoas documentObjectHateoas = new DocumentObjectHateoas(documentObject);
        documentObjectHateoasHandler.addLinks(documentObjectHateoas, request, new Authorisation());
        return new ResponseEntity<>(documentObjectHateoas, HttpStatus.OK);
    } catch (IOException e) {
        throw new StorageException(e.toString());
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) DocumentObjectHateoas(nikita.model.noark5.v4.hateoas.DocumentObjectHateoas) InputStream(java.io.InputStream) Authorisation(no.arkivlab.hioa.nikita.webapp.security.Authorisation) DocumentObject(nikita.model.noark5.v4.DocumentObject) NoarkEntityNotFoundException(nikita.util.exceptions.NoarkEntityNotFoundException) IOException(java.io.IOException) StorageException(nikita.util.exceptions.StorageException) Counted(com.codahale.metrics.annotation.Counted) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 82 with ResponseEntity

use of org.springframework.http.ResponseEntity in project sic by belluccifranco.

the class PedidoController method getReportePedido.

@GetMapping("/pedidos/{idPedido}/reporte")
public ResponseEntity<byte[]> getReportePedido(@PathVariable long idPedido) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_PDF);
    headers.add("content-disposition", "inline; filename=Pedido.pdf");
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    byte[] reportePDF = pedidoService.getReportePedido(pedidoService.getPedidoPorId(idPedido));
    return new ResponseEntity<>(reportePDF, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 83 with ResponseEntity

use of org.springframework.http.ResponseEntity in project vulnmanager by xebia-research.

the class NMapController method getNMapReport.

@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
ResponseEntity<?> getNMapReport(@ModelAttribute("isAuthenticated") boolean isAuthenticated) {
    if (!isAuthenticated) {
        return new ResponseEntity(new ErrorMsg("Auth not correct!"), HttpStatus.BAD_REQUEST);
    }
    Object parsedDocument = ReportUtil.parseDocument(ReportUtil.getDocumentFromFile(new File("example_logs/nmap.xml")));
    NMapReport report = getNMapReportFromObject(parsedDocument);
    if (report == null) {
        return new ResponseEntity<>(new ErrorMsg("The file requested is not of the right type"), HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(report, HttpStatus.OK);
}
Also used : NMapReport(com.xebia.vulnmanager.models.nmap.objects.NMapReport) ResponseEntity(org.springframework.http.ResponseEntity) ErrorMsg(com.xebia.vulnmanager.models.net.ErrorMsg) File(java.io.File)

Example 84 with ResponseEntity

use of org.springframework.http.ResponseEntity in project nikita-noark5-core by HiOA-ABI.

the class DocumentObjectHateoasController method handleFileUpload.

// API - All POST Requests (CRUD - CREATE)
// upload a file and associate it with a documentObject
// POST [contextPath][api]/arkivstruktur/dokumentobjekt/{systemID}/referanseFil
@ApiOperation(value = "Uploads a file and associates it 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
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS + SLASH + REFERENCE_FILE, method = RequestMethod.POST, headers = "Accept=*/*", produces = { NOARK5_V4_CONTENT_TYPE_JSON, NOARK5_V4_CONTENT_TYPE_JSON_XML })
public ResponseEntity<DocumentObjectHateoas> handleFileUpload(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the documentObject you wish to associate a file with", required = true) @PathVariable("systemID") final String documentObjectSystemId) {
    try {
        DocumentObject documentObject = documentObjectService.findBySystemId(documentObjectSystemId);
        if (documentObject == null) {
            throw new NoarkEntityNotFoundException(documentObjectSystemId);
        }
        InputStream inputStream;
        // Following will be needed for uploading file in chunks
        // String headerContentRange = request.getHeader("content-range");//Content-Range:bytes 737280-819199/845769
        // Check that content-length is set, > 0 and in agreement with the value set in documentObject
        Long contentLength = 0L;
        if (request.getHeader("content-length") == null) {
            throw new StorageException("Attempt to upload a document without content-length set. The document " + "was attempted to be associated with " + documentObject);
        }
        contentLength = (long) request.getIntHeader("content-length");
        if (contentLength < 1) {
            throw new StorageException("Attempt to upload a document with 0 or negative content-length set. " + "Actual value was (" + contentLength + "). The document  was attempted to be associated with " + documentObject);
        }
        if (null == documentObject.getFileSize()) {
            throw new StorageException("Attempt to upload a document with a content-length set in the header (" + contentLength + "), but the value in documentObject has not been set (== null).  The " + "document was attempted to be associated with " + documentObject);
        }
        if (!contentLength.equals(documentObject.getFileSize())) {
            throw new StorageException("Attempt to upload a document with a content-length set in the header (" + contentLength + ") that is not the same as the value in documentObject (" + documentObject.getFileSize() + ").  The document was attempted to be associated with " + documentObject);
        }
        // Check that the content-type is set and in agreement with mimeType value in documentObject
        String headerContentType = request.getHeader("content-type");
        if (headerContentType == null) {
            throw new StorageException("Attempt to upload a document without content-type set. The document " + "was attempted to be associated with " + documentObject);
        }
        if (!headerContentType.equals(documentObject.getMimeType())) {
            throw new StorageException("Attempt to upload a document with a content-type set in the header (" + contentLength + ") that is not the same as the mimeType in documentObject (" + documentObject.getMimeType() + ").  The document was attempted to be associated with " + documentObject);
        }
        documentObjectService.storeAndCalculateChecksum(request.getInputStream(), documentObject);
        // We need to update the documentObject in the database as checksum and checksum algorithm are set after
        // the document has been uploaded
        documentObjectService.update(documentObject);
        DocumentObjectHateoas documentObjectHateoas = new DocumentObjectHateoas(documentObject);
        documentObjectHateoasHandler.addLinks(documentObjectHateoas, new Authorisation());
        return new ResponseEntity<>(documentObjectHateoas, HttpStatus.OK);
    } catch (IOException e) {
        throw new StorageException(e.toString());
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) DocumentObjectHateoas(nikita.common.model.noark5.v4.hateoas.DocumentObjectHateoas) InputStream(java.io.InputStream) Authorisation(nikita.webapp.security.Authorisation) DocumentObject(nikita.common.model.noark5.v4.DocumentObject) NoarkEntityNotFoundException(nikita.common.util.exceptions.NoarkEntityNotFoundException) IOException(java.io.IOException) StorageException(nikita.common.util.exceptions.StorageException) Counted(com.codahale.metrics.annotation.Counted) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 85 with ResponseEntity

use of org.springframework.http.ResponseEntity in project cas by apereo.

the class Saml2ClientMetadataController method getSaml2ClientServiceProviderMetadataResponseEntity.

private ResponseEntity<String> getSaml2ClientServiceProviderMetadataResponseEntity(final SAML2Client saml2Client) {
    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_XML);
    return new ResponseEntity<>(saml2Client.getServiceProviderMetadataResolver().getMetadata(), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1188 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)419 HttpHeaders (org.springframework.http.HttpHeaders)398 Test (org.junit.Test)120 ApiOperation (io.swagger.annotations.ApiOperation)116 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)108 HashMap (java.util.HashMap)104 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)103 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)98 HttpStatus (org.springframework.http.HttpStatus)88 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)85 ArrayList (java.util.ArrayList)80 GetMapping (org.springframework.web.bind.annotation.GetMapping)79 Timed (com.codahale.metrics.annotation.Timed)68 IOException (java.io.IOException)67 List (java.util.List)65 URI (java.net.URI)49 MediaType (org.springframework.http.MediaType)48 Test (org.junit.jupiter.api.Test)46 HttpEntity (org.springframework.http.HttpEntity)46