diff options
Diffstat (limited to 'common/src/main/java/org/onap')
6 files changed, 88 insertions, 4 deletions
diff --git a/common/src/main/java/org/onap/so/client/AddCacheHeaders.java b/common/src/main/java/org/onap/so/client/AddCacheHeaders.java new file mode 100644 index 0000000000..1a41be1233 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/AddCacheHeaders.java @@ -0,0 +1,28 @@ +package org.onap.so.client; + +import java.io.IOException; +import java.util.Collections; +import javax.annotation.Priority; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.ext.Provider; + +@Provider +@Priority(1) +public class AddCacheHeaders implements ClientResponseFilter { + + private final CacheProperties props; + + public AddCacheHeaders(CacheProperties props) { + this.props = props; + } + + public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException { + if (request.getMethod().equalsIgnoreCase("GET")) { + response.getHeaders().putIfAbsent("Cache-Control", + Collections.singletonList("public, max-age=" + (props.getMaxAge() / 1000))); + } + + } +} diff --git a/common/src/main/java/org/onap/so/client/CacheFactory.java b/common/src/main/java/org/onap/so/client/CacheFactory.java new file mode 100644 index 0000000000..6bc4858463 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/CacheFactory.java @@ -0,0 +1,25 @@ +package org.onap.so.client; + + +import java.util.concurrent.TimeUnit; +import javax.cache.configuration.Factory; +import javax.cache.expiry.Duration; +import javax.cache.expiry.ExpiryPolicy; +import javax.cache.expiry.TouchedExpiryPolicy; + +public class CacheFactory implements Factory<ExpiryPolicy> { + + private static final long serialVersionUID = 8948728679233836929L; + + private final CacheProperties props; + + public CacheFactory(CacheProperties props) { + this.props = props; + } + + @Override + public ExpiryPolicy create() { + return TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, props.getMaxAge())).create(); + } + +} diff --git a/common/src/main/java/org/onap/so/client/CacheProperties.java b/common/src/main/java/org/onap/so/client/CacheProperties.java new file mode 100644 index 0000000000..4fb2a87a5b --- /dev/null +++ b/common/src/main/java/org/onap/so/client/CacheProperties.java @@ -0,0 +1,13 @@ +package org.onap.so.client; + +public interface CacheProperties { + + + default Long getMaxAge() { + return 60000L; + } + + default String getCacheName() { + return "default-http-cache"; + } +} diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java index 9fce328b1d..be0a0f3f9e 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -188,8 +188,20 @@ public abstract class RestClient { return APPLICATION_MERGE_PATCH_JSON; } + protected ClientBuilder getClientBuilder() { + ClientBuilder builder = ClientBuilder.newBuilder(); + if (props.isCachingEnabled()) { + enableCaching(builder); + } + return builder.readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS); + } + + protected ClientBuilder enableCaching(ClientBuilder builder) { + return builder; + } + protected Client getClient() { - return ClientBuilder.newBuilder().readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); + return getClientBuilder().build(); } protected abstract ONAPComponentsList getTargetEntity(); diff --git a/common/src/main/java/org/onap/so/client/RestClientSSL.java b/common/src/main/java/org/onap/so/client/RestClientSSL.java index 8956e20a5a..c6252e4652 100644 --- a/common/src/main/java/org/onap/so/client/RestClientSSL.java +++ b/common/src/main/java/org/onap/so/client/RestClientSSL.java @@ -24,7 +24,6 @@ import java.net.URI; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; import java.util.Optional; -import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -57,8 +56,7 @@ public abstract class RestClientSSL extends RestClient { } } // Use default SSL context - client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()) - .readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); + client = getClientBuilder().sslContext(SSLContext.getDefault()).build(); logger.info("RestClientSSL using default SSL context!"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); diff --git a/common/src/main/java/org/onap/so/client/RestProperties.java b/common/src/main/java/org/onap/so/client/RestProperties.java index 36da424f93..a7a0ef614c 100644 --- a/common/src/main/java/org/onap/so/client/RestProperties.java +++ b/common/src/main/java/org/onap/so/client/RestProperties.java @@ -49,4 +49,12 @@ public interface RestProperties { public default Long getReadTimeout() { return Long.valueOf(60000); } + + public default boolean isCachingEnabled() { + return false; + } + + public default CacheProperties getCacheProperties() { + return new CacheProperties() {}; + } } |