diff options
author | Vitaly Takmazov | 2018-10-24 01:07:30 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-10-24 01:07:30 +0300 |
commit | 82de22618ef0e95450ce43fa519ed1203d256674 (patch) | |
tree | d9c98315f0527f5537795c3b1fc2a1c280f3d96a /juick-server/src/main | |
parent | aa4dab444166f530adcd86003f802350db44b0b1 (diff) |
ActivityPub: cache requests
Diffstat (limited to 'juick-server/src/main')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/SignatureManager.java | 4 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java | 40 |
2 files changed, 43 insertions, 1 deletions
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<Context> 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 |