From 82de22618ef0e95450ce43fa519ed1203d256674 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 24 Oct 2018 01:07:30 +0300 Subject: ActivityPub: cache requests --- .../java/com/juick/server/SignatureManager.java | 4 ++- .../configuration/ActivityPubClientConfig.java | 40 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java (limited to 'juick-server/src/main') diff --git a/juick-server/src/main/java/com/juick/server/SignatureManager.java b/juick-server/src/main/java/com/juick/server/SignatureManager.java index e9227b9d..4fa6c5ff 100644 --- a/juick-server/src/main/java/com/juick/server/SignatureManager.java +++ b/juick-server/src/main/java/com/juick/server/SignatureManager.java @@ -41,6 +41,8 @@ public class SignatureManager { private ObjectMapper jsonMapper; @Inject private ApplicationEventPublisher applicationEventPublisher; + @Inject + private RestTemplate apClient; public void post(Person from, Person to, Context data) throws IOException { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(to.getInbox()); @@ -85,7 +87,7 @@ public class SignatureManager { } public Optional getContext(URI contextUri) { try { - return Optional.of(new RestTemplate().getForEntity(contextUri, Context.class).getBody()); + return Optional.of(apClient.getForEntity(contextUri, Context.class).getBody()); } catch (HttpClientErrorException e) { logger.warn("Cannot identify {}", contextUri); if (e.getStatusCode().equals(HttpStatus.GONE)) { diff --git a/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java new file mode 100644 index 00000000..b7762221 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java @@ -0,0 +1,40 @@ +package com.juick.server.configuration; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.cache.CacheConfig; +import org.apache.http.impl.client.cache.CachingHttpClientBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class ActivityPubClientConfig { + @Bean + public RestTemplate apClient() { + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + RestTemplate restTemplate = new RestTemplate(requestFactory); + // BufferingClientHttpRequestFactory allows us to read the response more than once - Necessary for debugging. + restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient()))); + return restTemplate; + } + @Bean + public HttpClient httpClient() { + return CachingHttpClientBuilder + .create() + .setCacheConfig(cacheConfig()) + .build(); + } + @Bean + public CacheConfig cacheConfig() { + return CacheConfig + .custom() + .setMaxObjectSize(500000) // 500KB + .setMaxCacheEntries(2000) + // Set this to false and a response with queryString + // will be cached when it is explicitly cacheable .setNeverCacheHTTP10ResponsesWithQueryString(false) + .build(); + } +} \ No newline at end of file -- cgit v1.2.3