Here are the examples of the java api class javax.ws.rs.core.CacheControl taken from open source projects.
1. CacheControlTest#cacheControlSerialization()
Project: Resteasy
File: CacheControlTest.java
File: CacheControlTest.java
@Test public void cacheControlSerialization() { RuntimeDelegate.HeaderDelegate<CacheControl> hdcc = ResteasyProviderFactory.getInstance().createHeaderDelegate(CacheControl.class); CacheControl control = new CacheControl(); control.setMaxAge(1000); control.setSMaxAge(500); control.setNoTransform(false); control.setPrivate(true); String toString = hdcc.toString(control); System.out.println(toString); CacheControl serialized = hdcc.fromString(toString); Assert.assertTrue(serialized.getMaxAge() == 1000); Assert.assertTrue(serialized.getSMaxAge() == 500); Assert.assertTrue(!serialized.isNoTransform()); Assert.assertTrue(serialized.isPrivate()); }
2. CacheControlFeature#initCacheControl()
Project: Resteasy
File: CacheControlFeature.java
File: CacheControlFeature.java
protected CacheControl initCacheControl(Cache methodCached) { CacheControl cacheControl = new CacheControl(); if (methodCached.isPrivate()) { cacheControl.setPrivate(true); } if (methodCached.maxAge() > -1) { cacheControl.setMaxAge(methodCached.maxAge()); } if (methodCached.sMaxAge() > -1) { cacheControl.setSMaxAge(methodCached.sMaxAge()); } cacheControl.setMustRevalidate((methodCached.mustRevalidate())); cacheControl.setNoStore((methodCached.noStore())); cacheControl.setNoTransform((methodCached.noTransform())); cacheControl.setProxyRevalidate(methodCached.proxyRevalidate()); return cacheControl; }
3. ParserTest#forCacheControl()
Project: isis
File: ParserTest.java
File: ParserTest.java
@Test public void forCacheControl() { final Parser<CacheControl> parser = Parser.forCacheControl(); final CacheControl cc1 = createCacheControl(); cc1.setMaxAge(2000); final CacheControl cc2 = createCacheControl(); cc2.setNoCache(true); for (final CacheControl v : new CacheControl[] { cc1, cc2 }) { final String asString = parser.asString(v); final CacheControl valueOf = parser.valueOf(asString); assertThat(v.getMaxAge(), is(equalTo(valueOf.getMaxAge()))); assertThat(v.isNoCache(), is(equalTo(valueOf.isNoCache()))); } }
4. CacheControlTest#testEveryDirectiveAppearsInStringifiedVersion()
Project: Resteasy
File: CacheControlTest.java
File: CacheControlTest.java
@Test public // TCK requires this void testEveryDirectiveAppearsInStringifiedVersion() { CacheControl cc = new CacheControl(); cc.setNoCache(true); cc.setPrivate(true); cc.setNoStore(true); String value = cc.toString(); assertTrue(value.contains("no-cache")); assertTrue(value.contains("no-store")); assertTrue(value.contains("private")); }
5. CustomerResourceTest#testCustomerResource()
Project: Resteasy
File: CustomerResourceTest.java
File: CustomerResourceTest.java
@Test public void testCustomerResource() throws Exception { System.out.println("*** Create a new Customer ***"); Customer newCustomer = new Customer(); newCustomer.setFirstName("Bill"); newCustomer.setLastName("Burke"); newCustomer.setStreet("256 Clarendon Street"); newCustomer.setCity("Boston"); newCustomer.setState("MA"); newCustomer.setZip("02115"); newCustomer.setCountry("USA"); Response response = client.target("http://localhost:8080/services/customers").request().post(Entity.xml(newCustomer)); if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); String location = response.getLocation().toString(); System.out.println("Location: " + location); response.close(); System.out.println("*** GET Created Customer **"); response = client.target(location).request().get(); CacheControl cc = CacheControl.valueOf(response.getHeaderString(HttpHeaders.CACHE_CONTROL)); System.out.println("Max age: " + cc.getMaxAge()); }
6. CacheControlHeaderDelegateTest#testSerializeCacheControlHeader()
Project: wink
File: CacheControlHeaderDelegateTest.java
File: CacheControlHeaderDelegateTest.java
@Test public void testSerializeCacheControlHeader() { RuntimeDelegate rd = RuntimeDelegate.getInstance(); HeaderDelegate<CacheControl> cacheControlHeaderDelegate = rd.createHeaderDelegate(CacheControl.class); if (cacheControlHeaderDelegate == null) { fail("CacheControl header delegate is not regestered in RuntimeDelegateImpl"); } String expectedCacheControlHeader = "private=\"privateField1\", " + "no-cache=\"noCachefield1, noCachefield2\", " + "no-store, " + "no-transform, " + "must-revalidate, " + "proxy-revalidate, " + "max-age=21600, " + "s-maxage=3000, " + "extension=value, " + "extension3, " + "extension2=\"value with space\""; CacheControl cc = new CacheControl(); cc.setMaxAge(21600); cc.setMustRevalidate(true); cc.setNoCache(true); cc.setNoStore(true); cc.setNoTransform(true); cc.setPrivate(true); cc.setProxyRevalidate(true); cc.setSMaxAge(3000); cc.getCacheExtension().put("extension", "value"); cc.getCacheExtension().put("extension2", "value with space"); cc.getCacheExtension().put("extension3", null); cc.getNoCacheFields().add("noCachefield1"); cc.getNoCacheFields().add("noCachefield2"); cc.getPrivateFields().add("privateField1"); String[] cache_out = cacheControlHeaderDelegate.toString(cc).split(","); String[] cache_expected = expectedCacheControlHeader.split(","); Arrays.sort(cache_expected); Arrays.sort(cache_out); assertArrayEquals(cache_expected, cache_out); try { cacheControlHeaderDelegate.toString(null); fail("CacheControl Header is null - IllegalArgumentException must be thrown"); } catch (IllegalArgumentException e) { } }
7. CacheControlUtil#noCache()
Project: keycloak
File: CacheControlUtil.java
File: CacheControlUtil.java
public static CacheControl noCache() { CacheControl cacheControl = new CacheControl(); cacheControl.setMustRevalidate(true); cacheControl.setNoCache(true); cacheControl.setNoStore(true); return cacheControl; }
8. WebInterfaceAssetsResource#getResponse()
Project: graylog2-server
File: WebInterfaceAssetsResource.java
File: WebInterfaceAssetsResource.java
private Response getResponse(Request request, String filename, URL resourceUrl, boolean fromPlugin) throws IOException, URISyntaxException { final Date lastModified; final InputStream stream; final HashCode hashCode; switch(resourceUrl.getProtocol()) { case "file": { String fileName = resourceUrl.getFile(); final File file = new File(fileName); lastModified = new Date(file.lastModified()); stream = new FileInputStream(file); hashCode = Files.hash(file, Hashing.sha256()); break; } case "jar": { final URI uri = resourceUrl.toURI(); final FileSystem fileSystem = fileSystemCache.getUnchecked(uri); final java.nio.file.Path path = fileSystem.getPath(pluginPrefixFilename(fromPlugin, filename)); final FileTime lastModifiedTime = java.nio.file.Files.getLastModifiedTime(path); lastModified = new Date(lastModifiedTime.toMillis()); stream = resourceUrl.openStream(); hashCode = Resources.asByteSource(resourceUrl).hash(Hashing.sha256()); break; } default: throw new IllegalArgumentException("Not a jar or file"); } final EntityTag entityTag = new EntityTag(hashCode.toString()); final Response.ResponseBuilder response = request.evaluatePreconditions(lastModified, entityTag); if (response != null) { return response.build(); } final String contentType = firstNonNull(mimeTypes.getContentType(filename), MediaType.APPLICATION_OCTET_STREAM); final CacheControl cacheControl = new CacheControl(); cacheControl.setMaxAge((int) TimeUnit.DAYS.toSeconds(365)); cacheControl.setNoCache(false); cacheControl.setPrivate(false); return Response.ok(stream).header(HttpHeaders.CONTENT_TYPE, contentType).tag(entityTag).cacheControl(cacheControl).lastModified(lastModified).build(); }
9. CacheControlFeature#initCacheControl()
Project: Resteasy
File: CacheControlFeature.java
File: CacheControlFeature.java
protected CacheControl initCacheControl(NoCache value) { CacheControl cacheControl = new CacheControl(); cacheControl.setNoCache(true); cacheControl.setNoTransform(false); for (String field : value.fields()) cacheControl.getNoCacheFields().add(field); return cacheControl; }
10. ParserTest#createCacheControl()
Project: isis
File: ParserTest.java
File: ParserTest.java
private static CacheControl createCacheControl() { final CacheControl cacheControl = new CacheControl(); // workaround for bug in cacheControl.getCacheExtension(); // CacheControl's equals() method // workaround for bug in CacheControl's cacheControl.getNoCacheFields(); // equals() method return cacheControl; }
11. FeedREST#getFavicon()
Project: commafeed
File: FeedREST.java
File: FeedREST.java
@GET @Path("/favicon/{id}") @UnitOfWork @ApiOperation(value = "Fetch a feed's icon", notes = "Fetch a feed's icon") @Timed public Response getFavicon(@SecurityCheck User user, @ApiParam(value = "subscription id") @PathParam("id") Long id) { Preconditions.checkNotNull(id); FeedSubscription subscription = feedSubscriptionDAO.findById(user, id); if (subscription == null) { return Response.status(Status.NOT_FOUND).build(); } Feed feed = subscription.getFeed(); Favicon icon = feedService.fetchFavicon(feed); ResponseBuilder builder = Response.ok(icon.getIcon(), Optional.ofNullable(icon.getMediaType()).orElse("image/x-icon")); CacheControl cacheControl = new CacheControl(); cacheControl.setMaxAge(2592000); cacheControl.setPrivate(false); builder.cacheControl(cacheControl); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); builder.expires(calendar.getTime()); builder.lastModified(CommaFeedApplication.STARTUP_TIME); return builder.build(); }
12. ServerCacheInterceptor#aroundWriteTo()
Project: Resteasy
File: ServerCacheInterceptor.java
File: ServerCacheInterceptor.java
@Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { if (!request.getHttpMethod().equalsIgnoreCase("GET") || request.getAttribute(ServerCacheHitFilter.DO_NOT_CACHE_RESPONSE) != null) { context.proceed(); return; } Object occ = context.getHeaders().getFirst(HttpHeaders.CACHE_CONTROL); if (occ == null) { context.proceed(); return; } CacheControl cc = null; if (occ instanceof CacheControl) cc = (CacheControl) occ; else { cc = CacheControl.valueOf(occ.toString()); } if (cc.isNoCache()) { context.proceed(); return; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); OutputStream old = context.getOutputStream(); try { context.setOutputStream(buffer); context.proceed(); byte[] entity = buffer.toByteArray(); Object etagObject = context.getHeaders().getFirst(HttpHeaders.ETAG); String etag = null; if (etagObject == null) { etag = createHash(entity); context.getHeaders().putSingle(HttpHeaders.ETAG, etag); } else // use application provided ETag if it exists { etag = etagObject.toString(); } cache.add(request.getUri().getRequestUri().toString(), context.getMediaType(), cc, context.getHeaders(), entity, etag); // check to see if ETags are the same. If they are, we don't need to send a response back. Response.ResponseBuilder validatedResponse = validation.evaluatePreconditions(new EntityTag(etag)); if (validatedResponse != null) { throw new NoLogWebApplicationException(validatedResponse.status(Response.Status.NOT_MODIFIED).cacheControl(cc).header(HttpHeaders.ETAG, etag).build()); } old.write(entity); } finally { context.setOutputStream(old); } }
13. CustomerResource#getCustomer()
Project: Resteasy
File: CustomerResource.java
File: CustomerResource.java
@GET @Path("{id}") @Produces("application/xml") public Response getCustomer(@PathParam("id") int id, @HeaderParam("If-None-Match") String sent, @Context Request request) { Customer cust = customerDB.get(id); if (cust == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } if (sent == null) System.out.println("No If-None-Match sent by client"); EntityTag tag = new EntityTag(Integer.toString(cust.hashCode())); CacheControl cc = new CacheControl(); cc.setMaxAge(5); Response.ResponseBuilder builder = request.evaluatePreconditions(tag); if (builder != null) { System.out.println("** revalidation on the server was successful"); builder.cacheControl(cc); return builder.build(); } // Preconditions not met! cust.setLastViewed(new Date().toString()); builder = Response.ok(cust, "application/xml"); builder.cacheControl(cc); builder.tag(tag); return builder.build(); }
14. CacheControlUtil#getDefaultCacheControl()
Project: keycloak
File: CacheControlUtil.java
File: CacheControlUtil.java
public static CacheControl getDefaultCacheControl() { CacheControl cacheControl = new CacheControl(); cacheControl.setNoTransform(false); Integer maxAge = Config.scope("theme").getInt("staticMaxAge"); if (maxAge != null && maxAge > 0) { cacheControl.setMaxAge(maxAge); } else { cacheControl.setNoCache(true); } return cacheControl; }
15. CacheControlFeature#configure()
Project: Resteasy
File: CacheControlFeature.java
File: CacheControlFeature.java
@Override public void configure(ResourceInfo resourceInfo, FeatureContext configurable) { final Class<?> declaring = resourceInfo.getResourceClass(); final Method method = resourceInfo.getResourceMethod(); if (declaring == null || method == null) return; if (!method.isAnnotationPresent(GET.class)) return; Cache cache = declaring.getAnnotation(Cache.class); NoCache nocache = declaring.getAnnotation(NoCache.class); Cache methodCached = method.getAnnotation(Cache.class); NoCache noMethodCache = method.getAnnotation(NoCache.class); CacheControl cacheControl = null; if (methodCached != null) { cacheControl = initCacheControl(methodCached); } else if (noMethodCache != null) { cacheControl = initCacheControl(noMethodCache); } else if (cache != null) { cacheControl = initCacheControl(cache); } else if (nocache != null) { cacheControl = initCacheControl(nocache); } if (cacheControl != null) { configurable.register(new CacheControlFilter(cacheControl)); } }