Skip to content

Commit 0d3d1fb

Browse files
gopackgo90reta
authored andcommitted
CXF-9210 Read JAX-RS requests with CacheControlFeature enabled and no Cache-Control header (#3026)
(cherry picked from commit a63e031)
1 parent d61e808 commit 0d3d1fb

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public Object aroundReadFrom(final ReaderInterceptorContext context) throws IOEx
8888
}
8989
final MultivaluedMap<String, String> responseHeaders = context.getHeaders();
9090
final String cacheControlHeader = responseHeaders.getFirst(HttpHeaders.CACHE_CONTROL);
91+
if (cacheControlHeader == null) {
92+
return context.proceed();
93+
}
9194
final CacheControl cacheControl = CacheControl.valueOf(cacheControlHeader);
9295

9396
byte[] cachedBytes = null;

rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/cache/ClientCacheTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.junit.Test;
5151

5252
import static org.junit.Assert.assertEquals;
53+
import static org.junit.Assert.assertNotEquals;
5354
import static org.junit.Assert.assertNotNull;
5455

5556
public class ClientCacheTest {
@@ -204,6 +205,25 @@ public void testGetJaxbBookCacheByValue() {
204205
}
205206
}
206207

208+
@Test
209+
public void testGetNonCacheBook() {
210+
try (CacheControlFeature feature = new CacheControlFeature()) {
211+
final WebTarget base = ClientBuilder.newBuilder().register(feature).build().target(ADDRESS);
212+
final Invocation.Builder cached =
213+
setAsLocal(base.request("text/xml")).header(HttpHeaders.CACHE_CONTROL, "public");
214+
final Response r = cached.get();
215+
assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
216+
final Book b1 = r.readEntity(Book.class);
217+
assertEquals("JNonCache", b1.getName());
218+
assertNotNull(b1.getId());
219+
waitABit();
220+
final Response r2 = cached.get();
221+
final Book b2 = r2.readEntity(Book.class);
222+
assertNotEquals(b1, b2);
223+
assertEquals(b1.getName(), b2.getName());
224+
}
225+
}
226+
207227
private static Invocation.Builder setAsLocal(final Invocation.Builder client) {
208228
WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
209229
return client;
@@ -233,6 +253,14 @@ public Response getJaxbBook() {
233253
b.setName("JCache");
234254
return Response.ok(b).tag("123").cacheControl(CacheControl.valueOf("max-age=50000")).build();
235255
}
256+
@GET
257+
@Produces("text/xml")
258+
public Response getNonCacheBook() {
259+
Book b = new Book();
260+
b.setId(System.currentTimeMillis());
261+
b.setName("JNonCache");
262+
return Response.ok(b).tag("123").build();
263+
}
236264
}
237265
@XmlRootElement
238266
public static class Book implements Serializable {

0 commit comments

Comments
 (0)