Java Code Examples for org.springframework.http.HttpHeaders#setExpires()

The following examples show how to use org.springframework.http.HttpHeaders#setExpires() . 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: ProjectController.java    From jandy with GNU General Public License v3.0 6 votes vote down vote up
@RequestMapping(value = "/{account}/{projectName}/{branchName}.svg")
public ResponseEntity<String> getBadge(@PathVariable String account, @PathVariable String projectName, @PathVariable String branchName) throws Exception {
  QBuild b = QBuild.build;
  Page<Build> buildPage = buildRepository.findAll(b.branch.name.eq(branchName).and(b.branch.project.account.eq(account)).and(b.branch.project.name.eq(projectName)), new QPageRequest(0, 2, b.number.desc()));
  if (buildPage.getTotalPages() == 0)
    throw new BadgeUnknownException();
  Build latest = buildPage.getContent().get(0);

  long current = System.currentTimeMillis();
  HttpHeaders headers = new HttpHeaders(); // see #7
  headers.setExpires(current);
  headers.setDate(current);

  return ResponseEntity
      .ok()
      .headers(headers)
      .cacheControl(CacheControl.noCache())
      .lastModified(current)
      .eTag(Long.toString(latest.getId()))
      .body(FreeMarkerTemplateUtils.processTemplateIntoString(configurer.getConfiguration().getTemplate("badge/mybadge.ftl"), latest));
}
 
Example 2
Source File: ICaptchaService.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 图片输出的头,避免验证码被缓存
 *
 * @return {HttpHeaders}
 */
default HttpHeaders getCaptchaHeaders() {
	HttpHeaders headers = new HttpHeaders();
	headers.setPragma("no-cache");
	headers.setCacheControl("no-cache");
	headers.setExpires(0);
	headers.setContentType(MediaType.IMAGE_JPEG);
	return headers;
}
 
Example 3
Source File: MockHttpServletResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private String getCookieHeader(Cookie cookie) {
	StringBuilder buf = new StringBuilder();
	buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
	if (StringUtils.hasText(cookie.getPath())) {
		buf.append("; Path=").append(cookie.getPath());
	}
	if (StringUtils.hasText(cookie.getDomain())) {
		buf.append("; Domain=").append(cookie.getDomain());
	}
	int maxAge = cookie.getMaxAge();
	if (maxAge >= 0) {
		buf.append("; Max-Age=").append(maxAge);
		buf.append("; Expires=");
		HttpHeaders headers = new HttpHeaders();
		headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
		buf.append(headers.getFirst(HttpHeaders.EXPIRES));
	}

	if (cookie.getSecure()) {
		buf.append("; Secure");
	}
	if (cookie.isHttpOnly()) {
		buf.append("; HttpOnly");
	}
	if (cookie instanceof MockCookie) {
		MockCookie mockCookie = (MockCookie) cookie;
		if (StringUtils.hasText(mockCookie.getSameSite())) {
			buf.append("; SameSite=").append(mockCookie.getSameSite());
		}
	}
	return buf.toString();
}
 
Example 4
Source File: MockHttpServletResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private String getCookieHeader(Cookie cookie) {
	StringBuilder buf = new StringBuilder();
	buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
	if (StringUtils.hasText(cookie.getPath())) {
		buf.append("; Path=").append(cookie.getPath());
	}
	if (StringUtils.hasText(cookie.getDomain())) {
		buf.append("; Domain=").append(cookie.getDomain());
	}
	int maxAge = cookie.getMaxAge();
	if (maxAge >= 0) {
		buf.append("; Max-Age=").append(maxAge);
		buf.append("; Expires=");
		HttpHeaders headers = new HttpHeaders();
		headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
		buf.append(headers.getFirst(HttpHeaders.EXPIRES));
	}

	if (cookie.getSecure()) {
		buf.append("; Secure");
	}
	if (cookie.isHttpOnly()) {
		buf.append("; HttpOnly");
	}
	if (cookie instanceof MockCookie) {
		MockCookie mockCookie = (MockCookie) cookie;
		if (StringUtils.hasText(mockCookie.getSameSite())) {
			buf.append("; SameSite=").append(mockCookie.getSameSite());
		}
	}
	return buf.toString();
}
 
Example 5
Source File: MockHttpServletResponse.java    From java-technology-stack with MIT License 5 votes vote down vote up
private String getCookieHeader(Cookie cookie) {
	StringBuilder buf = new StringBuilder();
	buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
	if (StringUtils.hasText(cookie.getPath())) {
		buf.append("; Path=").append(cookie.getPath());
	}
	if (StringUtils.hasText(cookie.getDomain())) {
		buf.append("; Domain=").append(cookie.getDomain());
	}
	int maxAge = cookie.getMaxAge();
	if (maxAge >= 0) {
		buf.append("; Max-Age=").append(maxAge);
		buf.append("; Expires=");
		HttpHeaders headers = new HttpHeaders();
		headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
		buf.append(headers.getFirst(HttpHeaders.EXPIRES));
	}

	if (cookie.getSecure()) {
		buf.append("; Secure");
	}
	if (cookie.isHttpOnly()) {
		buf.append("; HttpOnly");
	}
	if (cookie instanceof MockCookie) {
		MockCookie mockCookie = (MockCookie) cookie;
		if (StringUtils.hasText(mockCookie.getSameSite())) {
			buf.append("; SameSite=").append(mockCookie.getSameSite());
		}
	}
	return buf.toString();
}
 
Example 6
Source File: MockHttpServletResponse.java    From java-technology-stack with MIT License 5 votes vote down vote up
private String getCookieHeader(Cookie cookie) {
	StringBuilder buf = new StringBuilder();
	buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
	if (StringUtils.hasText(cookie.getPath())) {
		buf.append("; Path=").append(cookie.getPath());
	}
	if (StringUtils.hasText(cookie.getDomain())) {
		buf.append("; Domain=").append(cookie.getDomain());
	}
	int maxAge = cookie.getMaxAge();
	if (maxAge >= 0) {
		buf.append("; Max-Age=").append(maxAge);
		buf.append("; Expires=");
		HttpHeaders headers = new HttpHeaders();
		headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
		buf.append(headers.getFirst(HttpHeaders.EXPIRES));
	}

	if (cookie.getSecure()) {
		buf.append("; Secure");
	}
	if (cookie.isHttpOnly()) {
		buf.append("; HttpOnly");
	}
	if (cookie instanceof MockCookie) {
		MockCookie mockCookie = (MockCookie) cookie;
		if (StringUtils.hasText(mockCookie.getSameSite())) {
			buf.append("; SameSite=").append(mockCookie.getSameSite());
		}
	}
	return buf.toString();
}
 
Example 7
Source File: PhotographController.java    From fenixedu-academic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@RequestMapping(value = "{username:.+}", method = RequestMethod.GET)
public ResponseEntity<byte[]> get(@PathVariable String username, @RequestParam(value = "s", required = false,
        defaultValue = "100") Integer size, @RequestHeader(value = "If-None-Match", required = false) String ifNoneMatch)
        throws IOException {

    if (size <= 0) {
        size = 100;
    }
    if (size > 512) {
        size = 512;
    }

    User user = User.findByUsername(username);

    if (user != null && user.getPerson() != null) {
        final Photograph personalPhoto =
                user.getPerson().isPhotoAvailableToCurrentUser() ? user.getPerson().getPersonalPhoto() : null;

        HttpHeaders headers = new HttpHeaders();
        String etag = "W/\"" + (personalPhoto == null ? "mm-av" : personalPhoto.getExternalId()) + "-" + size + "\"";
        headers.setETag(etag);
        headers.setExpires(DateTime.now().plusWeeks(2).getMillis());
        headers.setCacheControl("max-age=1209600");

        if (etag.equals(ifNoneMatch)) {
            return new ResponseEntity<>(headers, HttpStatus.NOT_MODIFIED);
        }

        if (personalPhoto != null) {
            headers.set("Content-Type", personalPhoto.getOriginal().getPictureFileFormat().getMimeType());
            return new ResponseEntity<>(personalPhoto.getCustomAvatar(size, size, PictureMode.ZOOM), headers, HttpStatus.OK);
        } else {
            try (InputStream mm =
                    PhotographController.class.getClassLoader().getResourceAsStream("META-INF/resources/img/mysteryman.png")) {
                headers.set("Content-Type", "image/png");
                return new ResponseEntity<>(Avatar.process(mm, "image/png", size), headers, HttpStatus.OK);
            }
        }
    }

    throw BennuCoreDomainException.resourceNotFound(username);
}