Hi,

I can modify org.apache.cxf.jaxrs.client.cache.Entry to be serializable
and at that point the cache is used.

I would also implement optional part of the cache protocol as "If-None-
Match". At now the framework seem not able to handle a 304 reply from
the server because it does not give the interceptor a chance to
retrieve the message from the cache.

I modified
org.apache.cxf.jaxrs.client.cache.CacheControlClientRequestFilter.filte
r with:
    public void filter(final ClientRequestContext request) throws
IOException {
        if (!HttpMethod.GET.equals(request.getMethod())) {
            //TODO: Review the possibility of supporting POST
responses, example,
            //      POST create request may get a created entity
representation returned
            request.setProperty(NO_CACHE_PROPERTY, "true");
            return;
        }
        final URI uri = request.getUri();
        final String accepts =
request.getHeaderString(HttpHeaders.ACCEPT);
        final Key key = new Key(uri, accepts);
        Entry entry = cache.get(key);
        if (entry != null) {
            //TODO: do the extra validation against the conditional
headers
            //      which may be contained in the current request
            if (entry.isOutDated()) {
                String ifNoneMatchHeader =
entry.getCacheHeaders().get(HttpHeaders.IF_NONE_MATCH);
                if (StringUtils.isEmpty(ifNoneMatchHeader)) {
                    cache.remove(key, entry);
                } else {
                    request.getHeaders().add(HttpHeaders.IF_NONE_MATCH,
ifNoneMatchHeader);
                }
            } else {
                Object cachedEntity = entry.getData();
                Response.ResponseBuilder ok =
Response.ok(cachedEntity);
                if (entry.getHeaders() != null) {
                    for (Map.Entry<String, List<String>> h :
entry.getHeaders().entrySet()) {
                        for (final Object instance : h.getValue()) {
                            ok = ok.header(h.getKey(), instance);
                        }
                    }
                }
                request.setProperty(CACHED_ENTITY_PROPERTY,
cachedEntity);
                request.abortWith(ok.build());
            }
        }
        // Should the map of all request headers shared ?
        request.setProperty(CLIENT_ACCEPTS, accepts);
        request.setProperty(CLIENT_CACHE_CONTROL,
request.getHeaderString(HttpHeaders.CACHE_CONTROL));
    }


This will trigger a 304 response from the server, but we now need to
recover the response from the cache.

Any idea where you would do it? at now the interceptor is not called
for this case.


Nicola


On Mon, 2018-11-12 at 14:22 +0000, Nicola Buso wrote:
> Hi everyone,
> 
> I'm interested to add HTTP Cache feature to a cxf client. I
> configured
> it and tried the ehcache jsr107 implementation and the reference
> implementation and both are complaining about the class:
> org.apache.cxf.jaxrs.impl.MetadataMap
> 
> that is not implementing Serializable.
> Caused by: java.io.NotSerializableException:
> org.apache.cxf.jaxrs.impl.MetadataMap
>       at
> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
>       at
> java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java
> :1
> 548)
>       at
> java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:15
> 09
> )
>       at
> java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav
> a:
> 1432)
>       at
> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
>       at
> java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
>       at
> org.jsr107.ri.RISerializingInternalConverter$Serialized.<init>(RISeri
> al
> izingInternalConverter.java:123)
> 
> 
> Is there some configuration I can do to avoid this error?
> 
> These are some of the involved libraries:
>     <dependency>
>         <groupId>org.apache.cxf</groupId>
>         <artifactId>cxf-rt-rs-client</artifactId>
>         <version>3.2.7</version>
>     </dependency>
>     <dependency>
>         <groupId>javax.cache</groupId>
>         <artifactId>cache-api</artifactId>
>         <version>1.1.0</version>
>     </dependency>
>     <!--
>     <dependency>
>         <groupId>org.ehcache.modules</groupId>
>         <artifactId>ehcache-107</artifactId>
>         <version>3.6.1</version>
>     </dependency>
>     -->
>     <dependency>
>       <groupId>org.jsr107.ri</groupId>
>       <artifactId>cache-ri-impl</artifactId>
>       <version>1.1.0</version>
>     </dependency>
> 
> 
> Please let me know,
> 
> 
> Nicola
> 
> 

Reply via email to