aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java
blob: 637b9fd728878a8485aeaa12f2f229a2a22a61e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.juick.server.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.server.api.activity.model.Activity;
import com.juick.server.helpers.HeaderRequestInterceptor;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import javax.inject.Inject;
import java.net.URI;
import java.util.Collections;

@Configuration
public class ActivityPubClientConfig {
    @Inject
    ActivityPubClientErrorHandler activityPubClientErrorHandler;
    @Inject
    ObjectMapper jsonMapper;
    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(jsonMapper);
        return converter;
    }
    @Bean
    public RestTemplate apClient() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {
            @Override
            protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
                HttpClientContext context = HttpClientContext.create();
                context.setRequestConfig(getRequestConfig());
                return context;
            }
            RequestConfig getRequestConfig() {
                RequestConfig.Builder builder = RequestConfig.custom()
                        .setCookieSpec(CookieSpecs.IGNORE_COOKIES);
                return builder.build();
            }
        });
        restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());
        restTemplate.setErrorHandler(activityPubClientErrorHandler);
        restTemplate.setInterceptors(Collections.singletonList(
                new HeaderRequestInterceptor("Accept", Activity.ACTIVITY_MEDIA_TYPE)));
        return restTemplate;
    }
}