Java Code Examples for org.springframework.http.ResponseEntity#BodyBuilder

The following examples show how to use org.springframework.http.ResponseEntity#BodyBuilder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ETagResponseEntityBuilder.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 6 votes vote down vote up
private final ResponseEntity.BodyBuilder newResponse(final HttpStatus status, final String etag)
{
	ResponseEntity.BodyBuilder response = ResponseEntity.status(status)
			.eTag(etag)
			.cacheControl(CacheControl.maxAge(cacheMaxAgeSec, TimeUnit.SECONDS));

	final String adLanguage = getAdLanguage();
	if (adLanguage != null && !adLanguage.isEmpty())
	{
		final String contentLanguage = ADLanguageList.toHttpLanguageTag(adLanguage);
		response.header(HttpHeaders.CONTENT_LANGUAGE, contentLanguage);
		response.header(HttpHeaders.VARY, HttpHeaders.ACCEPT_LANGUAGE); // advice browser to include ACCEPT_LANGUAGE in their caching key
	}

	return response;
}
 
Example 2
Source File: PlatformsController.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
@ApiOperation("Update a platform")
@PutMapping("/{application_name}/platforms")
public ResponseEntity<PlatformIO> updatePlatform(Authentication authentication,
                                                 @PathVariable("application_name") final String applicationName,
                                                 @ApiParam(value = "Copie les propriétés du module déployé de la plateforme source avec l'ID correspondant. " +
                                                         "Si ce module ne contient pas de propriétés, à défaut on utilise les propriétés du module avec le même properties_path.")
                                                 @RequestParam(value = "copyPropertiesForUpgradedModules", required = false) final Boolean copyPropertiesForUpgradedModules,
                                                 @Valid @RequestBody final PlatformIO platformInput) {

    Platform.Key platformKey = new Platform.Key(applicationName, platformInput.getPlatformName());

    platformUseCases.updatePlatform(platformKey,
            platformInput.toDomainInstance(),
            Boolean.TRUE.equals(copyPropertiesForUpgradedModules), // on traite le cas `null`
            new User(authentication)
    );

    final ResponseEntity.BodyBuilder response = ResponseEntity.status(HttpStatus.OK);
    PlatformView platformView = platformUseCases.getPlatform(platformKey);
    return response.body(new PlatformIO(platformView));
}
 
Example 3
Source File: FilesServiceImpl.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Transactional(readOnly = true)
@Override
public ResponseEntity<StreamingResponseBody> download(String fileId) {
  FileMeta fileMeta = getFileMeta(fileId);

  ResponseEntity.BodyBuilder builder = ResponseEntity.ok();
  builder.header(CONTENT_TYPE, fileMeta.getContentType());
  builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\"");

  Long contentLength = fileMeta.getSize();
  if (contentLength != null) {
    builder.contentLength(contentLength);
  }

  return builder.body(
      outputStream -> {
        try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) {
          ByteStreams.copy(fromChannel, Channels.newChannel(outputStream));
        }
      });
}
 
Example 4
Source File: TestController.java    From cloud-security-xsuaa-integration with Apache License 2.0 5 votes vote down vote up
@GetMapping("/v1/sayHello")
public Mono<ResponseEntity<String>> sayHello() {
	ResponseEntity.BodyBuilder unAuthenticated = ResponseEntity.status(HttpStatus.UNAUTHORIZED);

	return ReactiveSecurityContext.getToken()
			.doOnError(throwable -> Mono.just(unAuthenticated))
			.map(token -> ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN)
					.body(new Base64JwtDecoder().decode(token.getAppToken()).getPayload()));
}
 
Example 5
Source File: ZipkinQueryApiV2.java    From pivotal-bank-demo with Apache License 2.0 5 votes vote down vote up
/**
 * We cache names if there are more than 3 services. This helps people getting started: if we
 * cache empty results, users have more questions. We assume caching becomes a concern when zipkin
 * is in active use, and active use usually implies more than 3 services.
 */
ResponseEntity<List<String>> maybeCacheNames(List<String> names) {
  ResponseEntity.BodyBuilder response = ResponseEntity.ok();
  if (serviceCount > 3) {
    response.cacheControl(CacheControl.maxAge(namesMaxAge, TimeUnit.SECONDS).mustRevalidate());
  }
  return response.body(names);
}
 
Example 6
Source File: CustomAdvice.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@ExceptionHandler(HandledResponseEntityException.class)
public ResponseEntity<Error> handleResponseEntityException(HandledResponseEntityException e,
        HttpServletRequest request) {

    ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity
            .status(HttpStatus.PAYMENT_REQUIRED)
            .header("custom-header", "custom-value");

    if (e.getContentType() != null) {
        bodyBuilder.contentType(e.getContentType());
    }

    return bodyBuilder.body(new Error(request.getRequestURI() + ":" + e.getMessage()));
}
 
Example 7
Source File: WebuiImage.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
public ResponseEntity<byte[]> toResponseEntity(@NonNull final ResponseEntity.BodyBuilder responseBuilder)
{
	return responseBuilder
			.contentType(MediaType.parseMediaType(getContentType()))
			.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + getImageName() + "\"")
			.body(getImageData());
}
 
Example 8
Source File: PropertiesControllerImpl.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseEntity<InputStreamResource> getIcon(String definitionName, DefinitionImageType imageType) {
    notNull(definitionName, "Definition name cannot be null.");
    notNull(imageType, "Definition image type cannot be null.");
    final Definition<?> definition = propertiesHelpers.getDefinition(definitionName);
    notNull(definition, "Could not find definition of name %s", definitionName);

    // Undefined and missing icon resources are simply 404.
    String imagePath = definition.getImagePath(imageType);
    if (imagePath == null) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    InputStream is = definition.getClass().getResourceAsStream(imagePath);
    if (is == null) {
        log.info("The image type %s should exist for %s at %s, but is missing. "
                + "The component should provide this resource.", imageType, definitionName, imagePath);
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // At this point, we have enough information for a correct response.
    ResponseEntity.BodyBuilder response = ResponseEntity.ok();

    // Add the content type if it can be determined.
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String contentType = mimeTypesMap.getContentType(imagePath);
    if (contentType != null) {
        response = response.contentType(MediaType.parseMediaType(contentType));
    }

    return response.body(new InputStreamResource(is));
}
 
Example 9
Source File: ResponseEntityPro.java    From spring-boot-start-current with Apache License 2.0 4 votes vote down vote up
private static ResponseEntity.BodyBuilder buildStatus ( int status ) {
	return ResponseEntity.status( status );
}