Search in sources :

Example 1 with BodyBuilder

use of org.springframework.http.ResponseEntity.BodyBuilder in project jhipster-registry by jhipster.

the class ExceptionTranslator method processRuntimeException.

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processRuntimeException(Exception ex) {
    BodyBuilder builder;
    ErrorVM errorVM;
    ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
    if (responseStatus != null) {
        builder = ResponseEntity.status(responseStatus.value());
        errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
    } else {
        builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
        errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
    }
    return builder.body(errorVM);
}
Also used : BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder)

Example 2 with BodyBuilder

use of org.springframework.http.ResponseEntity.BodyBuilder in project tutorials by eugenp.

the class ExceptionTranslator method processRuntimeException.

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processRuntimeException(Exception ex) {
    BodyBuilder builder;
    ErrorVM errorVM;
    ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
    if (responseStatus != null) {
        builder = ResponseEntity.status(responseStatus.value());
        errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
    } else {
        builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
        errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
    }
    return builder.body(errorVM);
}
Also used : BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder)

Example 3 with BodyBuilder

use of org.springframework.http.ResponseEntity.BodyBuilder in project tutorials by eugenp.

the class ExceptionTranslator method processRuntimeException.

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVM> processRuntimeException(Exception ex) {
    BodyBuilder builder;
    ErrorVM errorVM;
    ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
    if (responseStatus != null) {
        builder = ResponseEntity.status(responseStatus.value());
        errorVM = new ErrorVM("error." + responseStatus.value().value(), responseStatus.reason());
    } else {
        builder = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
        errorVM = new ErrorVM(ErrorConstants.ERR_INTERNAL_SERVER_ERROR, "Internal server error");
    }
    return builder.body(errorVM);
}
Also used : BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder)

Example 4 with BodyBuilder

use of org.springframework.http.ResponseEntity.BodyBuilder in project CzechIdMng by bcvsolutions.

the class PublicIdmConfigurationController method downloadApplicationLogo.

/**
 * Download application logo.
 *
 * @return logo input stream
 * @since 12.0.0
 */
@RequestMapping(value = "/application/logo", method = RequestMethod.GET)
@ApiOperation(value = "Download application logo", nickname = "downloadApplicationLogo", tags = { IdmConfigurationController.TAG })
public ResponseEntity<InputStreamResource> downloadApplicationLogo() {
    UUID uuid = applicationConfiguration.getApplicationLogoId();
    // not configured
    if (uuid == null) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    // configuration is wrong
    IdmAttachmentDto attachment = attachmentManager.get(uuid);
    if (attachment == null) {
        LOG.warn("Application logo  with attachment identifier [{}] not found, returning null. Change configuration [{}]", uuid, ApplicationConfiguration.PROPERTY_APPLICATION_LOGO);
        // 
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    String mimetype = attachment.getMimetype();
    InputStream is = attachmentManager.getAttachmentData(attachment.getId());
    try {
        BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", attachmentManager.getValidFileName(attachment.getName())));
        // append media type, if it's filled
        if (StringUtils.isNotBlank(mimetype)) {
            response = response.contentType(MediaType.valueOf(attachment.getMimetype()));
        }
        // 
        return response.body(new InputStreamResource(is));
    } catch (IOException e) {
        throw new ResultCodeException(CoreResultCode.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) ResponseEntity(org.springframework.http.ResponseEntity) InputStream(java.io.InputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException) UUID(java.util.UUID) BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder) InputStreamResource(org.springframework.core.io.InputStreamResource) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with BodyBuilder

use of org.springframework.http.ResponseEntity.BodyBuilder in project CzechIdMng by bcvsolutions.

the class IdmNotificationAttachmentController method download.

@RequestMapping(value = "/{backendId}/download", method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasAuthority('" + NotificationGroupPermission.NOTIFICATION_READ + "')")
@ApiOperation(value = "Download notification attachment", nickname = "downloadNotificationAttachment", tags = { IdmNotificationAttachmentController.TAG }, notes = "Returns input stream to notification attachment.", authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }) })
public ResponseEntity<InputStreamResource> download(@ApiParam(value = "Notification attachment uuid identifier.", required = true) @PathVariable String backendId) {
    IdmNotificationAttachmentDto dto = getDto(backendId);
    if (dto == null) {
        throw new EntityNotFoundException(getService().getEntityClass(), backendId);
    }
    // 
    UUID attachmentId = dto.getAttachment();
    IdmAttachmentDto attachment = attachmentManager.get(attachmentId);
    if (attachment == null) {
        throw new EntityNotFoundException(attachmentManager.getEntityClass(), attachmentId);
    }
    // 
    InputStream is = attachmentManager.getAttachmentData(attachment.getId());
    // 
    try {
        BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", attachment.getName()));
        // append media type, if it's filled
        String mimetype = attachment.getMimetype();
        if (StringUtils.isNotBlank(mimetype)) {
            response = response.contentType(MediaType.valueOf(attachment.getMimetype()));
        }
        // 
        return response.body(new InputStreamResource(is));
    } catch (IOException e) {
        throw new ResultCodeException(CoreResultCode.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmNotificationAttachmentDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto) InputStream(java.io.InputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException) UUID(java.util.UUID) BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder) InputStreamResource(org.springframework.core.io.InputStreamResource) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

BodyBuilder (org.springframework.http.ResponseEntity.BodyBuilder)11 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)5 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 InputStreamResource (org.springframework.core.io.InputStreamResource)5 ApiOperation (io.swagger.annotations.ApiOperation)3 ResponseEntity (org.springframework.http.ResponseEntity)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 UUID (java.util.UUID)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 IdmProfileDto (eu.bcvsolutions.idm.core.api.dto.IdmProfileDto)1 EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)1 IdmNotificationAttachmentDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1