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);
}
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);
}
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);
}
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);
}
}
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);
}
}
Aggregations