@Test // SPR-13626 public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception { String etagValue = "\"some-etag\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1); }
@Test public void shouldHandleETagAndLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(etagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo); }
@Test public void shouldHandleChangedETagAndLastModified() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; String changedEtagValue = "\"changed-etag-value\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(changedEtagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, null, changedEtagValue, oneMinuteAgo); }
@GetMapping("/events/{projectName}") @ResponseBody public ResponseEntity<RepositoryEvent[]> fetchEvents(@PathVariable String projectName) { GithubProject project = this.repository.findByRepoName(projectName); if (project == null) { return ResponseEntity.notFound().build(); } ResponseEntity<RepositoryEvent[]> response = this.githubClient .fetchEvents(project.getOrgName(), project.getRepoName()); return ResponseEntity.ok() .eTag(response.getHeaders().getETag()) .body(response.getBody()); }
private ResponseEntity<Resource> response(FilePointer filePointer, HttpStatus status, Resource body) { final ResponseEntity.BodyBuilder responseBuilder = ResponseEntity .status(status) .eTag(filePointer.getEtag()) .contentLength(filePointer.getSize()) .lastModified(filePointer.getLastModified().toEpochMilli()); filePointer .getMediaType() .map(this::toMediaType) .ifPresent(responseBuilder::contentType); return responseBuilder.body(body); }
@RequestMapping(path = "/", produces = "text/plain") public ResponseEntity<String> serviceCapabilitiesText( @RequestHeader(value = HttpHeaders.USER_AGENT, required = false) String userAgent) { String appUrl = generateAppUrl(); InitializrMetadata metadata = this.metadataProvider.get(); BodyBuilder builder = ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN); if (userAgent != null) { Agent agent = Agent.fromUserAgent(userAgent); if (agent != null) { if (AgentId.CURL.equals(agent.getId())) { String content = this.commandLineHelpGenerator .generateCurlCapabilities(metadata, appUrl); return builder.eTag(createUniqueId(content)).body(content); } if (AgentId.HTTPIE.equals(agent.getId())) { String content = this.commandLineHelpGenerator .generateHttpieCapabilities(metadata, appUrl); return builder.eTag(createUniqueId(content)).body(content); } if (AgentId.SPRING_BOOT_CLI.equals(agent.getId())) { String content = this.commandLineHelpGenerator .generateSpringBootCliCapabilities(metadata, appUrl); return builder.eTag(createUniqueId(content)).body(content); } } } String content = this.commandLineHelpGenerator .generateGenericCapabilities(metadata, appUrl); return builder.eTag(createUniqueId(content)).body(content); }
@GetMapping(path = "/ui/dependencies", produces = "application/json") public ResponseEntity<String> dependencies( @RequestParam(required = false) String version) { List<DependencyGroup> dependencyGroups = this.metadataProvider.get() .getDependencies().getContent(); List<DependencyItem> content = new ArrayList<>(); Version requestedVersion = (StringUtils.isEmpty(version) ? null : Version.parse(version)); dependencyGroups.forEach((group) -> group.getContent().forEach((dependency) -> { if (requestedVersion != null && dependency.getVersionRange() != null) { if (dependency.match(requestedVersion)) { content.add(new DependencyItem(group.getName(), dependency)); } } else { content.add(new DependencyItem(group.getName(), dependency)); } })); String json = writeDependencies(content); return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON) .eTag(createUniqueId(json)).body(json); }
private ResponseEntity<String> serviceCapabilitiesFor( InitializrMetadataVersion version, MediaType contentType) { String appUrl = generateAppUrl(); String content = getJsonMapper(version).write(this.metadataProvider.get(), appUrl); return ResponseEntity.ok().contentType(contentType).eTag(createUniqueId(content)) .cacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)).body(content); }
private ResponseEntity<String> dependenciesFor(InitializrMetadataVersion version, String bootVersion) { InitializrMetadata metadata = this.metadataProvider.get(); Version v = (bootVersion != null) ? Version.parse(bootVersion) : Version.parse(metadata.getBootVersions().getDefault().getId()); DependencyMetadata dependencyMetadata = this.dependencyMetadataProvider .get(metadata, v); String content = new DependencyMetadataV21JsonMapper().write(dependencyMetadata); return ResponseEntity.ok().contentType(version.getMediaType()) .eTag(createUniqueId(content)) .cacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)).body(content); }
@Test public void handleReturnValueChangedETagAndLastModified() throws Exception { String etag = "\"deadb33f8badf00d\""; String newEtag = "\"changed-etag-value\""; Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path") .ifNoneMatch(etag) .ifModifiedSince(currentTime.toEpochMilli()) ); ResponseEntity<String> entity = ok().eTag(newEtag).lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.OK, "body", newEtag, oneMinAgo); }
@Test public void handleReturnValueETagAndLastModified() throws Exception { String eTag = "\"deadb33f8badf00d\""; Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path") .ifNoneMatch(eTag) .ifModifiedSince(currentTime.toEpochMilli()) ); ResponseEntity<String> entity = ok().eTag(eTag).lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, eTag, oneMinAgo); }
@Test public void shouldHandleNotModifiedResponse() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; ResponseEntity<String> returnValue = ResponseEntity.status(HttpStatus.NOT_MODIFIED) .eTag(etagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo); }
@Test // SPR-14559 public void shouldHandleInvalidIfNoneMatchWithHttp200() throws Exception { String etagValue = "\"deadb33f8badf00d\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, "unquoted"); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1); }
@Test public void Etagheader() throws URISyntaxException { ResponseEntity<Void> responseEntity = ResponseEntity.ok().eTag("\"foo\"").build(); assertEquals("\"foo\"", responseEntity.getHeaders().getETag()); responseEntity = ResponseEntity.ok().eTag("foo").build(); assertEquals("\"foo\"", responseEntity.getHeaders().getETag()); responseEntity = ResponseEntity.ok().eTag("W/\"foo\"").build(); assertEquals("W/\"foo\"", responseEntity.getHeaders().getETag()); }
@Test // SPR-13626 public void shouldHandleIfNoneMatchIfMatch() throws Exception { String etagValue = "\"some-etag\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); servletRequest.addHeader(HttpHeaders.IF_MATCH, "ifmatch"); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1); }
@Test // SPR-13626 public void shouldHandleGetIfNoneMatchWildcard() throws Exception { String wildcardValue = "*"; String etagValue = "\"some-etag\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1); }
@Test // SPR-13496 public void shouldHandleConditionalRequestIfNoneMatchWildcard() throws Exception { String wildcardValue = "*"; String etagValue = "\"some-etag\""; servletRequest.setMethod("POST"); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, "body", etagValue, -1); }
@Test // SPR-14559 public void handleReturnValueEtagInvalidIfNoneMatch() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifNoneMatch("unquoted")); ResponseEntity<String> entity = ok().eTag("\"deadb33f8badf00d\"").body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode()); assertResponseBody(exchange, "body"); }
@Test public void handleEtagWithHttp304() throws Exception { String etagValue = "\"deadb33f8badf00d\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1); }
@Test public void handleReturnValueEtag() throws Exception { String etagValue = "\"deadb33f8badf00d\""; MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifNoneMatch(etagValue)); ResponseEntity<String> entity = ok().eTag(etagValue).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, etagValue, Instant.MIN); }