Example usage for org.springframework.http CacheControl noCache

List of usage examples for org.springframework.http CacheControl noCache

Introduction

In this page you can find the example usage for org.springframework.http CacheControl noCache.

Prototype

boolean noCache

To view the source code for org.springframework.http CacheControl noCache.

Click Source Link

Usage

From source file:com.opopov.cloud.image.service.ImageStitchingServiceImpl.java

@Override
public DeferredResult<ResponseEntity<?>> getStitchedImage(@RequestBody ImageStitchingConfiguration config) {

    validator.validateConfig(config);//from w w w  .j av  a  2s.  c  om

    List<ListenableFuture<ResponseEntity<byte[]>>> futures = config.getUrlList().stream()
            .map(url -> remoteResource.getForEntity(url, byte[].class)).collect(Collectors.toList());

    //wrap the listenable futures into the completable futures
    //writing loop in pre-8 style, since it would be more concise compared to stream api in this case
    CompletableFuture[] imageFutures = new CompletableFuture[futures.size()];
    int taskIndex = 0;
    IndexMap indexMap = new IndexMap(config.getRowCount() * config.getColumnCount());
    for (ListenableFuture<ResponseEntity<byte[]>> f : futures) {
        imageFutures[taskIndex] = imageDataFromResponse(taskIndex, indexMap, utils.fromListenableFuture(f));
        taskIndex++;
    }

    CompletableFuture<Void> allDownloadedAndDecompressed = CompletableFuture.allOf(imageFutures);

    //Synchronous part - start - writing decompressed bytes to the large image
    final int DOWNLOAD_AND_DECOMPRESS_TIMEOUT = 30; //30 seconds for each of the individual tasks
    DeferredResult<ResponseEntity<?>> response = new DeferredResult<>();
    boolean allSuccessful = false;
    byte[] imageBytes = null;
    try {
        Void finishResult = allDownloadedAndDecompressed.get(DOWNLOAD_AND_DECOMPRESS_TIMEOUT, TimeUnit.SECONDS);

        imageBytes = combineImagesIntoStitchedImage(config, indexMap);

        HttpHeaders headers = new HttpHeaders();
        headers.setCacheControl(CacheControl.noCache().getHeaderValue());
        headers.setContentType(MediaType.IMAGE_JPEG);
        allSuccessful = true;
    } catch (InterruptedException | ExecutionException e) {
        // basically either download or decompression of the source image failed
        // just skip it then, we have no image to show
        response.setErrorResult(
                new SourceImageLoadException("Unable to load and decode one or more source images", e));
    } catch (TimeoutException e) {
        //send timeout response, via ImageLoadTimeoutException
        response.setErrorResult(new ImageLoadTimeoutException(
                String.format("Some of the images were not loaded and decoded before timeout of %d seconds",
                        DOWNLOAD_AND_DECOMPRESS_TIMEOUT),
                e

        ));
    } catch (IOException e) {
        response.setErrorResult(new ImageWriteException("Error writing image into output buffer", e));
    }

    //Synchronous part - end

    if (!allSuccessful) {
        //shoud not get here, some unknown error
        response.setErrorResult(
                new ImageLoadTimeoutException("Unknown error", new RuntimeException("Something went wrong")

                ));

        return response;
    }

    ResponseEntity<?> successResult = ResponseEntity.ok(imageBytes);
    response.setResult(successResult);

    return response;

}

From source file:org.craftercms.engine.controller.StaticAssetsRequestHandler.java

protected void init() {
    if (disableCaching) {
        setCacheControl(CacheControl.noCache());
    }
    setRequireSession(false);
}

From source file:org.egov.infra.utils.FileStoreUtils.java

public ResponseEntity<InputStreamResource> fileAsResponseEntity(String fileStoreId, String moduleName,
        boolean toSave) {
    try {// w w  w . j a v  a2s . co  m
        Optional<FileStoreMapper> fileStoreMapper = getFileStoreMapper(fileStoreId);
        if (fileStoreMapper.isPresent()) {
            Path file = getFileAsPath(fileStoreId, moduleName);
            byte[] fileBytes = Files.readAllBytes(file);
            String contentType = isBlank(fileStoreMapper.get().getContentType()) ? Files.probeContentType(file)
                    : fileStoreMapper.get().getContentType();
            return ResponseEntity.ok().contentType(parseMediaType(defaultIfBlank(contentType, JPG_MIME_TYPE)))
                    .cacheControl(CacheControl.noCache()).contentLength(fileBytes.length)
                    .header(CONTENT_DISPOSITION,
                            format(toSave ? CONTENT_DISPOSITION_ATTACH : CONTENT_DISPOSITION_INLINE,
                                    fileStoreMapper.get().getFileName()))
                    .body(new InputStreamResource(new ByteArrayInputStream(fileBytes)));
        }
        return ResponseEntity.notFound().build();
    } catch (IOException e) {
        LOGGER.error("Error occurred while creating response entity from file mapper", e);
        return ResponseEntity.badRequest().build();
    }
}

From source file:org.egov.infra.utils.FileStoreUtils.java

public ResponseEntity<InputStreamResource> fileAsPDFResponse(String fileStoreId, String fileName,
        String moduleName) {//from   w  ww.jav  a 2s.co  m
    try {
        File file = fileStoreService.fetch(fileStoreId, moduleName);
        byte[] fileBytes = FileUtils.readFileToByteArray(file);
        return ResponseEntity.ok().contentType(parseMediaType(APPLICATION_PDF_VALUE))
                .cacheControl(CacheControl.noCache()).contentLength(fileBytes.length)
                .header(CONTENT_DISPOSITION, format(CONTENT_DISPOSITION_INLINE, fileName + ".pdf"))
                .body(new InputStreamResource(new ByteArrayInputStream(fileBytes)));
    } catch (IOException e) {
        throw new ApplicationRuntimeException("Error while reading file", e);
    }
}