Skip to content

Commit 70c724e

Browse files
committed
CXF-9210 Read JAX-RS requests with CacheControlFeature enabled and no Cache-Control header
1 parent e92e6eb commit 70c724e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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
@@ -49,6 +49,7 @@
4949
import org.junit.Test;
5050

5151
import static org.junit.Assert.assertEquals;
52+
import static org.junit.Assert.assertNotEquals;
5253
import static org.junit.Assert.assertNotNull;
5354

5455
public class ClientCacheTest {
@@ -203,6 +204,25 @@ public void testGetJaxbBookCacheByValue() {
203204
}
204205
}
205206

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

0 commit comments

Comments
 (0)