use of org.springframework.http.ResponseEntity.BodyBuilder in project connectors-workspace-one by vmware.
the class ExceptionHandlers method handleStatusCodeException.
@ExceptionHandler
@ResponseBody
public ResponseEntity<Object> handleStatusCodeException(WebClientResponseException e) {
// Map the status to a 500 unlesss it's a 401. If that's the case
// then we know the client has passed in an invalid X-xxx-Authorization header and it's a 400.
logger.error("Backend returned {} {} \n {}", e.getStatusCode(), e.getStatusCode().getReasonPhrase(), e.getResponseBodyAsString());
String backendStatus = Integer.toString(e.getRawStatusCode());
if (e.getStatusCode() == UNAUTHORIZED) {
Map<String, String> body = Collections.singletonMap("error", "invalid_connector_token");
return ResponseEntity.status(BAD_REQUEST).header(BACKEND_STATUS, backendStatus).contentType(APPLICATION_JSON).body(body);
} else {
BodyBuilder builder = ResponseEntity.status(INTERNAL_SERVER_ERROR).header(BACKEND_STATUS, backendStatus);
if (e.getHeaders().getContentType() != null) {
builder.contentType(e.getHeaders().getContentType());
}
return builder.body(e.getResponseBodyAsString());
}
}
use of org.springframework.http.ResponseEntity.BodyBuilder in project CzechIdMng by bcvsolutions.
the class IdmIdentityController method getProfileImage.
/**
* Returns profile image attachment from identity
*
* @return
* @since 9.0.0
*/
@RequestMapping(value = "/{backendId}/profile/image", method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasAuthority('" + CoreGroupPermission.PROFILE_AUTOCOMPLETE + "')")
@ApiOperation(value = "Profile image", nickname = "getProfileImage", tags = { IdmIdentityController.TAG }, notes = "Returns input stream to identity profile image.", authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.PROFILE_AUTOCOMPLETE, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.PROFILE_AUTOCOMPLETE, description = "") }) })
public ResponseEntity<InputStreamResource> getProfileImage(@ApiParam(value = "Identity's uuid identifier or username.", required = true) @PathVariable String backendId) {
IdmProfileDto profile = profileService.findOneByIdentity(backendId, IdmBasePermission.AUTOCOMPLETE);
if (profile == null) {
return new ResponseEntity<InputStreamResource>(HttpStatus.NO_CONTENT);
}
if (profile.getImage() == null) {
return new ResponseEntity<InputStreamResource>(HttpStatus.NO_CONTENT);
}
IdmAttachmentDto attachment = attachmentManager.get(profile.getImage());
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\"", 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 IdmFormDefinitionController method previewAttachment.
/**
* Returns input stream to attachment saved in given form value.
*
* @param value
* @return
* @since 9.4.0
*/
public ResponseEntity<InputStreamResource> previewAttachment(IdmFormValueDto value) {
Assert.notNull(value, "Form value holds the attachment identifier is required to download the attachment.");
//
if (value.getPersistentType() != PersistentType.ATTACHMENT) {
throw new ResultCodeException(CoreResultCode.FORM_VALUE_WRONG_TYPE, "Download attachment of form value [%s], attribute [%s] with type [%s] is not supported. Download supports [%] persistent type only.", ImmutableMap.of("value", Objects.toString(value.getId()), "formAttribute", value.getFormAttribute().toString(), "persistentType", value.getPersistentType().toString(), "requiredPersistentType", PersistentType.ATTACHMENT.toString()));
}
if (value.getUuidValue() == null) {
throw new ResultCodeException(CoreResultCode.BAD_VALUE, "Attachment of form value [%s], attribute [%s] is empty, cannot be dowloaded.", ImmutableMap.of("value", Objects.toString(value.getId()), "formAttribute", value.getFormAttribute().toString()));
}
//
IdmAttachmentDto attachment = attachmentManager.get(value.getUuidValue());
if (attachment == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", value.getUuidValue()));
}
String mimetype = attachment.getMimetype();
// TODO: naive check => implement better + image resize (thumbnail)
if (!mimetype.startsWith("image/")) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
//
InputStream is = attachmentManager.getAttachmentData(attachment.getId());
try {
BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=\"%s\"", 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 IdmFormDefinitionController method downloadAttachment.
/**
* Returns input stream to attachment saved in given form value.
*
* @param value
* @return
* @since 9.4.0
*/
public ResponseEntity<InputStreamResource> downloadAttachment(IdmFormValueDto value) {
Assert.notNull(value, "Form value holds the attachment identifier is required to download the attachment.");
//
if (value.getPersistentType() != PersistentType.ATTACHMENT) {
throw new ResultCodeException(CoreResultCode.FORM_VALUE_WRONG_TYPE, "Download attachment of form value [%s], attribute [%s] with type [%s] is not supported. Download supports [%] persistent type only.", ImmutableMap.of("value", Objects.toString(value.getId()), "formAttribute", value.getFormAttribute().toString(), "persistentType", value.getPersistentType().toString(), "requiredPersistentType", PersistentType.ATTACHMENT.toString()));
}
if (value.getUuidValue() == null) {
throw new ResultCodeException(CoreResultCode.BAD_VALUE, "Attachment of form value [%s], attribute [%s] is empty, cannot be dowloaded.", ImmutableMap.of("value", Objects.toString(value.getId()), "formAttribute", value.getFormAttribute().toString()));
}
//
IdmAttachmentDto attachment = attachmentManager.get(value.getUuidValue());
if (attachment == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", value.getUuidValue()));
}
String mimetype = attachment.getMimetype();
InputStream is = attachmentManager.getAttachmentData(attachment.getId());
try {
BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; 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 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);
}
Aggregations