Skip to content

Commit

Permalink
Merge pull request #17644 from geoand/#17630
Browse files Browse the repository at this point in the history
Add support for @Cache and @nocache in RESTEasy Reactive
  • Loading branch information
geoand authored Jun 3, 2021
2 parents baceb71 + 9129a00 commit 45d5237
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jboss.resteasy.reactive.server.model.ContextResolvers;
import org.jboss.resteasy.reactive.server.model.ParamConverterProviders;
import org.jboss.resteasy.reactive.server.processor.scanning.AsyncReturnTypeScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.CacheControlScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveContextResolverScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveExceptionMappingScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveFeatureScanner;
Expand Down Expand Up @@ -72,6 +73,11 @@ public MethodScannerBuildItem asyncSupport() {
return new MethodScannerBuildItem(new AsyncReturnTypeScanner());
}

@BuildStep
public MethodScannerBuildItem cacheControlSupport() {
return new MethodScannerBuildItem(new CacheControlScanner());
}

@BuildStep
public ResourceInterceptorsContributorBuildItem scanForInterceptors(CombinedIndexBuildItem combinedIndexBuildItem,
ApplicationResultBuildItem applicationResultBuildItem) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.resteasy.reactive.server.test.cache;

import static org.hamcrest.CoreMatchers.equalTo;

import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.Cache;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class CacheOnClassAndMethodsTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(ResourceWithCache.class);
}
});

@Test
public void testWith() {
RestAssured.get("/test/with")
.then()
.statusCode(200)
.body(equalTo("with"))
.header("Cache-Control", "no-store");
}

@Test
public void testWithout() {
RestAssured.get("/test/without")
.then()
.statusCode(200)
.body(equalTo("without"))
.header("Cache-Control", "no-cache, no-transform, proxy-revalidate, s-maxage=100");
}

@Path("test")
@Cache(sMaxAge = 100, noTransform = true, proxyRevalidate = true, noCache = true)
public static class ResourceWithCache {

@Path("with")
@Cache(noStore = true)
@GET
public String with() {
return "with";
}

@Path("without")
@GET
public String without() {
return "without";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkus.resteasy.reactive.server.test.cache;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.Cache;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class CacheOnMethodsTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class).addClasses(ResourceWithCache.class);
}
});

@Test
public void testWith() {
RestAssured.get("/test/with")
.then()
.statusCode(200)
.body(equalTo("with"))
.header("Cache-Control", "must-revalidate, no-store, max-age=100, private");
}

@Test
public void testWithout() {
RestAssured.get("/test/without")
.then()
.statusCode(200)
.body(equalTo("without"))
.header("Cache-Control", nullValue());
}

@Path("test")
public static class ResourceWithCache {

@Path("with")
@GET
@Cache(maxAge = 100, noStore = true, mustRevalidate = true, isPrivate = true)
public String with() {
return "with";
}

@Path("without")
@GET
public String without() {
return "without";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.resteasy.reactive.server.test.cache;

import static org.hamcrest.CoreMatchers.equalTo;

import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.NoCache;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class NoCacheOnClassAndMethodsTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(ResourceWithNoCache.class);
}
});

@Test
public void testWith() {
RestAssured.get("/test/with")
.then()
.statusCode(200)
.body(equalTo("with"))
.header("Cache-Control", "no-cache=\"f1\", no-cache=\"f2\"");
}

@Test
public void testWithout() {
RestAssured.get("/test/without")
.then()
.statusCode(200)
.body(equalTo("without"))
.header("Cache-Control", "no-cache=\"f1\"");
}

@NoCache(fields = "f1")
@Path("test")
public static class ResourceWithNoCache {

@Path("with")
@GET
@NoCache(fields = { "f1", "f2" })
public String with() {
return "with";
}

@Path("without")
@GET
public String without() {
return "without";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.resteasy.reactive.server.test.cache;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.NoCache;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class NoCacheOnMethodsTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(ResourceWithNoCache.class);
}
});

@Test
public void testWith() {
RestAssured.get("/test/with")
.then()
.statusCode(200)
.body(equalTo("with"))
.header("Cache-Control", "no-cache=\"f1\", no-cache=\"f2\"");
}

@Test
public void testWithout() {
RestAssured.get("/test/without")
.then()
.statusCode(200)
.body(equalTo("without"))
.header("Cache-Control", nullValue());
}

@Path("test")
public static class ResourceWithNoCache {

@Path("with")
@GET
@NoCache(fields = { "f1", "f2" })
public String with() {
return "with";
}

@Path("without")
@GET
public String without() {
return "without";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jboss.resteasy.reactive;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Cache {

int maxAge() default -1;

int sMaxAge() default -1;

boolean noStore() default false;

boolean noTransform() default false;

boolean mustRevalidate() default false;

boolean proxyRevalidate() default false;

boolean isPrivate() default false;

boolean noCache() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jboss.resteasy.reactive;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface NoCache {

String[] fields() default {};
}
Loading

0 comments on commit 45d5237

Please sign in to comment.