Example usage for org.springframework.http HttpStatus NOT_MODIFIED

List of usage examples for org.springframework.http HttpStatus NOT_MODIFIED

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus NOT_MODIFIED.

Prototype

HttpStatus NOT_MODIFIED

To view the source code for org.springframework.http HttpStatus NOT_MODIFIED.

Click Source Link

Document

304 Not Modified .

Usage

From source file:org.dd4t.mvc.controllers.AbstractBinaryController.java

@RequestMapping(method = { RequestMethod.GET, RequestMethod.HEAD })
public void getBinary(final HttpServletRequest request, final HttpServletResponse response)
        throws ItemNotFoundException {
    String binaryPath = getBinaryPath(request);
    LOG.debug(">> {} binary {}", request.getMethod(), binaryPath);

    int resizeToWidth = -1;
    if (request.getParameterMap().containsKey("resizeToWidth")) {
        resizeToWidth = Integer.parseInt(request.getParameter("resizeToWidth"));
    }//from   w  w  w. j ava 2  s . com

    Binary binary;
    int publicationId = publicationResolver.getPublicationId();
    String path = String.format("%s/%d%s", binaryRootFolder, publicationId, binaryPath);
    if (resizeToWidth > -1) {
        path = insertIntoPath(path, request.getParameter("resizeToWidth"));
    }

    try {

        binary = binaryFactory.getBinaryByURL(binaryPath, publicationId);

        if (binary == null) {
            response.setStatus(HttpStatus.NOT_FOUND.value());
            LOG.error("Item not found:" + binaryPath);
            return;
        }

        // Check if anything changed, if nothing changed return a 304
        // TODO: test if this works..
        String modifiedHeader = request.getHeader(IF_MODIFIED_SINCE);
        if (StringUtils.isNotEmpty(modifiedHeader)
                && createDateFormat().format(binary.getLastPublishedDate().toDate()).equals(modifiedHeader)) {
            response.setStatus(HttpStatus.NOT_MODIFIED.value());
            return;
        }

        fillResponse(request, response, binary, path, resizeToWidth);
    } catch (IOException | FactoryException e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        LOG.error(e.getMessage(), e);
        throw new ItemNotFoundException(e);
    } finally {
        try {
            if (response != null && response.getOutputStream() != null) {
                response.getOutputStream().close();
            }
        } catch (IOException ioe) {
            LOG.error("Failed to close servlet output stream", ioe);
        }
    }
}

From source file:org.haiku.haikudepotserver.job.controller.JobController.java

/**
 * <p>This is helper-code that can be used to check to see if the data is stale and
 * will then enqueue the job, run it and then redirect the user to the data
 * download.</p>/*from w  ww  .  j  av  a2s. c om*/
 * @param response is the HTTP response to send the redirect to.
 * @param ifModifiedSinceHeader is the inbound header from the client.
 * @param lastModifyTimestamp is the actual last modified date for the data.
 * @param jobSpecification is the job that would be run if the data is newer than in the
 *                         inbound header.
 */

public static void handleRedirectToJobData(HttpServletResponse response, JobService jobService,
        String ifModifiedSinceHeader, Date lastModifyTimestamp, JobSpecification jobSpecification)
        throws IOException {

    if (!Strings.isNullOrEmpty(ifModifiedSinceHeader)) {
        try {
            Date requestModifyTimestamp = new Date(Instant
                    .from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(ifModifiedSinceHeader)).toEpochMilli());

            if (requestModifyTimestamp.getTime() >= lastModifyTimestamp.getTime()) {
                response.setStatus(HttpStatus.NOT_MODIFIED.value());
                return;
            }
        } catch (DateTimeParseException dtpe) {
            LOGGER.warn("bad [{}] header on request; [{}] -- will ignore", HttpHeaders.IF_MODIFIED_SINCE,
                    StringUtils.abbreviate(ifModifiedSinceHeader, 128));
        }
    }

    // what happens here is that we get the report and if it is too old, delete it and try again.

    JobSnapshot jobSnapshot = getJobSnapshotStartedAfter(jobService, lastModifyTimestamp, jobSpecification);
    Set<String> jobDataGuids = jobSnapshot.getDataGuids();

    if (1 != jobDataGuids.size()) {
        throw new IllegalStateException("found [" + jobDataGuids.size()
                + "] job data guids related to the job [" + jobSnapshot.getGuid() + "] - was expecting 1");
    }

    String lastModifiedValue = DateTimeFormatter.RFC_1123_DATE_TIME
            .format(ZonedDateTime.ofInstant(lastModifyTimestamp.toInstant(), ZoneOffset.UTC));
    String destinationLocationUrl = UriComponentsBuilder.newInstance()
            .pathSegment(AuthenticationFilter.SEGMENT_SECURED).pathSegment(JobController.SEGMENT_JOBDATA)
            .pathSegment(jobDataGuids.iterator().next()).pathSegment(JobController.SEGMENT_DOWNLOAD)
            .toUriString();

    response.addHeader(HttpHeaders.LAST_MODIFIED, lastModifiedValue);
    response.sendRedirect(destinationLocationUrl);
}

From source file:org.springframework.web.client.RestTemplateIntegrationTests.java

@Test
public void getNotModified() {
    String s = template.getForObject(baseUrl + "/status/notmodified", String.class);
    assertNull("Invalid content", s);

    ResponseEntity<String> entity = template.getForEntity(baseUrl + "/status/notmodified", String.class);
    assertEquals("Invalid response code", HttpStatus.NOT_MODIFIED, entity.getStatusCode());
    assertNull("Invalid content", entity.getBody());
}