Example usage for org.springframework.http HttpHeaders ETAG

List of usage examples for org.springframework.http HttpHeaders ETAG

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders ETAG.

Prototype

String ETAG

To view the source code for org.springframework.http HttpHeaders ETAG.

Click Source Link

Document

The HTTP ETag header field name.

Usage

From source file:com.github.zhanhb.ckfinder.download.PathPartial.java

/**
 * Check if the conditions specified in the optional If headers are satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param attr The resource information//from ww  w .j  av  a 2s  .c  o  m
 * @param etag ETag of the entity
 * @return boolean true if the resource meets all the specified conditions,
 * and false if any of the conditions is not satisfied, in which case request
 * processing is stopped
 */
private boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse response,
        BasicFileAttributes attr, String etag) throws IOException {
    try {
        checkIfMatch(request, etag);
        checkIfUnmodifiedSince(request, attr);
        checkIfNoneMatch(request, etag);
        checkIfModifiedSince(request, attr);
        return true;
    } catch (UncheckException ex) {
        if (ex.isError()) {
            response.sendError(ex.getCode());
        } else {
            response.setStatus(ex.getCode());
            response.setHeader(HttpHeaders.ETAG, etag);
        }
        return false;
    }
}

From source file:com.github.zhanhb.ckfinder.download.PathPartial.java

/**
 * Serve the specified resource, optionally including the data content.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param content Should the content be included?
 * @param path the resource to serve/*  ww  w. j  av a  2s  .co  m*/
 *
 * @exception IOException if an input/output error occurs
 */
private void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content, Path path)
        throws IOException, ServletException {
    ActionContext context = new ActionContext().put(HttpServletRequest.class, request)
            .put(HttpServletResponse.class, response).put(ServletContext.class, request.getServletContext())
            .put(Path.class, path);
    if (path == null) {
        notFound.handle(context);
        return;
    }
    BasicFileAttributes attr;
    try {
        attr = Files.readAttributes(path, BasicFileAttributes.class);
    } catch (IOException ex) {
        notFound.handle(context);
        return;
    }
    context.put(BasicFileAttributes.class, attr);

    boolean isError = response.getStatus() >= HttpServletResponse.SC_BAD_REQUEST;
    // Check if the conditions specified in the optional If headers are
    // satisfied.
    // Checking If headers
    boolean included = (request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null);
    String etag = this.eTag.getValue(context);
    if (!included && !isError && !checkIfHeaders(request, response, attr, etag)) {
        return;
    }
    // Find content type.
    String contentType = contentTypeResolver.getValue(context);
    // Get content length
    long contentLength = attr.size();
    // Special case for zero length files, which would cause a
    // (silent) ISE
    boolean serveContent = content && contentLength != 0;
    Range[] ranges = null;
    if (!isError) {
        if (useAcceptRanges) {
            // Accept ranges header
            response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
        }
        // Parse range specifier
        ranges = serveContent ? parseRange(request, response, attr, etag) : FULL;
        // ETag header
        response.setHeader(HttpHeaders.ETAG, etag);
        // Last-Modified header
        response.setDateHeader(HttpHeaders.LAST_MODIFIED, attr.lastModifiedTime().toMillis());
    }
    ServletOutputStream ostream = null;
    if (serveContent) {
        ostream = response.getOutputStream();
    }

    String disposition = contentDisposition.getValue(context);
    if (disposition != null) {
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
    }

    // Check to see if a Filter, Valve of wrapper has written some content.
    // If it has, disable range requests and setting of a content length
    // since neither can be done reliably.
    if (isError || ranges == FULL) {
        // Set the appropriate output headers
        if (contentType != null) {
            log.debug("serveFile: contentType='{}'", contentType);
            response.setContentType(contentType);
        }
        if (contentLength >= 0) {
            setContentLengthLong(response, contentLength);
        }
        // Copy the input stream to our output stream (if requested)
        if (serveContent) {
            log.trace("Serving bytes");
            Files.copy(path, ostream);
        }
    } else if (ranges != null && ranges.length != 0) {
        // Partial content response.
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        if (ranges.length == 1) {
            Range range = ranges[0];
            response.addHeader(HttpHeaders.CONTENT_RANGE, range.toString());
            long length = range.end - range.start + 1;
            setContentLengthLong(response, length);
            if (contentType != null) {
                log.debug("serveFile: contentType='{}'", contentType);
                response.setContentType(contentType);
            }
            if (serveContent) {
                try (InputStream stream = Files.newInputStream(path)) {
                    copyRange(stream, ostream, range, new byte[Math.min((int) length, 8192)]);
                }
            }
        } else {
            response.setContentType("multipart/byteranges; boundary=" + MIME_SEPARATION);
            if (serveContent) {
                copy(path, ostream, ranges, contentType, new byte[Math.min((int) contentLength, 8192)]);
            }
        }
    }
}

From source file:io.spring.initializr.web.project.MainControllerDependenciesTests.java

@Test
public void noBootVersion() {
    ResponseEntity<String> response = execute("/dependencies", String.class, null, "application/json");
    assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
    validateContentType(response, CURRENT_METADATA_MEDIA_TYPE);
    validateDependenciesOutput("1.1.4", new JSONObject(response.getBody()));
}

From source file:io.spring.initializr.web.project.MainControllerDependenciesTests.java

@Test
public void filteredDependencies() {
    ResponseEntity<String> response = execute("/dependencies?bootVersion=1.2.1.RELEASE", String.class, null,
            "application/json");
    assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
    validateContentType(response, CURRENT_METADATA_MEDIA_TYPE);
    validateDependenciesOutput("1.2.1", new JSONObject(response.getBody()));
}

From source file:io.spring.initializr.web.project.MainControllerIntegrationTests.java

@Test
public void metadataWithCurrentAcceptHeader() {
    getRequests().setFields("_links.maven-project", "dependencies.values[0]", "type.values[0]",
            "javaVersion.values[0]", "packaging.values[0]", "bootVersion.values[0]", "language.values[0]");
    ResponseEntity<String> response = invokeHome(null, "application/vnd.initializr.v2.1+json");
    assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
    validateContentType(response, AbstractInitializrControllerIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
    validateCurrentMetadata(new JSONObject(response.getBody()));
}

From source file:io.spring.initializr.web.project.MainControllerIntegrationTests.java

@Test
public void metadataWithHalAcceptHeader() {
    ResponseEntity<String> response = invokeHome(null, "application/hal+json");
    assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
    validateContentType(response, MainController.HAL_JSON_CONTENT_TYPE);
    validateCurrentMetadata(new JSONObject(response.getBody()));
}

From source file:io.spring.initializr.web.project.MainControllerIntegrationTests.java

private void validateCurlHelpContent(ResponseEntity<String> response) {
    validateContentType(response, MediaType.TEXT_PLAIN);
    assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
    assertThat(response.getBody(),/*from  w ww. j a v a  2  s  . co m*/
            allOf(containsString("Spring Initializr"), containsString("Examples:"), containsString("curl")));
}