1+ /**
2+ * The contents of this file are subject to the license and copyright
3+ * detailed in the LICENSE and NOTICE files at the root of the source
4+ * tree and available online at
5+ *
6+ * http://www.dspace.org/license/
7+ */
8+ package org .dspace .app .client ;
9+
10+ import static org .apache .commons .collections4 .ListUtils .emptyIfNull ;
11+
12+ import java .util .List ;
13+
14+ import org .apache .http .HttpRequestInterceptor ;
15+ import org .apache .http .HttpResponseInterceptor ;
16+ import org .apache .http .client .HttpClient ;
17+ import org .apache .http .client .config .RequestConfig ;
18+ import org .apache .http .impl .client .CloseableHttpClient ;
19+ import org .apache .http .impl .client .HttpClientBuilder ;
20+ import org .dspace .services .ConfigurationService ;
21+ import org .dspace .utils .DSpace ;
22+ import org .springframework .beans .factory .annotation .Autowired ;
23+
24+ /**
25+ * Factory of {@link HttpClient} with common configurations.
26+ *
27+ * @author Luca Giamminonni (luca.giamminonni at 4science.it)
28+ *
29+ */
30+ public class DSpaceHttpClientFactory {
31+
32+ @ Autowired
33+ private ConfigurationService configurationService ;
34+
35+ @ Autowired
36+ private DSpaceProxyRoutePlanner proxyRoutePlanner ;
37+
38+ @ Autowired (required = false )
39+ private List <HttpRequestInterceptor > requestInterceptors ;
40+
41+ @ Autowired (required = false )
42+ private List <HttpResponseInterceptor > responseInterceptors ;
43+
44+ /**
45+ * Get an instance of {@link DSpaceHttpClientFactory} from the Spring context.
46+ * @return the bean instance
47+ */
48+ public static DSpaceHttpClientFactory getInstance () {
49+ return new DSpace ().getSingletonService (DSpaceHttpClientFactory .class );
50+ }
51+
52+ /**
53+ * Build an instance of {@link HttpClient} setting the proxy if configured.
54+ *
55+ * @return the client
56+ */
57+ public CloseableHttpClient build () {
58+ return build (HttpClientBuilder .create (), true );
59+ }
60+
61+ /**
62+ * return a Builder if an instance of {@link HttpClient} pre-setting the proxy if configured.
63+ *
64+ * @return the client
65+ */
66+ public HttpClientBuilder builder (boolean setProxy ) {
67+ HttpClientBuilder clientBuilder = HttpClientBuilder .create ();
68+ if (setProxy ) {
69+ clientBuilder .setRoutePlanner (proxyRoutePlanner );
70+ }
71+ getRequestInterceptors ().forEach (clientBuilder ::addInterceptorLast );
72+ getResponseInterceptors ().forEach (clientBuilder ::addInterceptorLast );
73+ return clientBuilder ;
74+ }
75+
76+ /**
77+ * Build an instance of {@link HttpClient} without setting the proxy, even if
78+ * configured.
79+ *
80+ * @return the client
81+ */
82+ public CloseableHttpClient buildWithoutProxy () {
83+ return build (HttpClientBuilder .create (), false );
84+ }
85+
86+ /**
87+ * Build an instance of {@link HttpClient} setting the proxy if configured,
88+ * disabling automatic retries and setting the maximum total connection.
89+ *
90+ * @param maxConnTotal the maximum total connection value
91+ * @return the client
92+ */
93+ public CloseableHttpClient buildWithoutAutomaticRetries (int maxConnTotal ) {
94+ HttpClientBuilder clientBuilder = HttpClientBuilder .create ()
95+ .disableAutomaticRetries ()
96+ .setMaxConnTotal (maxConnTotal );
97+ return build (clientBuilder , true );
98+ }
99+
100+ /**
101+ * Build an instance of {@link HttpClient} setting the proxy if configured with
102+ * the given request configuration.
103+ * @param requestConfig the request configuration
104+ * @return the client
105+ */
106+ public CloseableHttpClient buildWithRequestConfig (RequestConfig requestConfig ) {
107+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ()
108+ .setDefaultRequestConfig (requestConfig );
109+ return build (httpClientBuilder , true );
110+ }
111+
112+ private CloseableHttpClient build (HttpClientBuilder clientBuilder , boolean setProxy ) {
113+ if (setProxy ) {
114+ clientBuilder .setRoutePlanner (proxyRoutePlanner );
115+ }
116+ getRequestInterceptors ().forEach (clientBuilder ::addInterceptorLast );
117+ getResponseInterceptors ().forEach (clientBuilder ::addInterceptorLast );
118+ return clientBuilder .build ();
119+ }
120+
121+ public ConfigurationService getConfigurationService () {
122+ return configurationService ;
123+ }
124+
125+ public void setConfigurationService (ConfigurationService configurationService ) {
126+ this .configurationService = configurationService ;
127+ }
128+
129+ public List <HttpRequestInterceptor > getRequestInterceptors () {
130+ return emptyIfNull (requestInterceptors );
131+ }
132+
133+ public void setRequestInterceptors (List <HttpRequestInterceptor > requestInterceptors ) {
134+ this .requestInterceptors = requestInterceptors ;
135+ }
136+
137+ public List <HttpResponseInterceptor > getResponseInterceptors () {
138+ return emptyIfNull (responseInterceptors );
139+ }
140+
141+ public void setResponseInterceptors (List <HttpResponseInterceptor > responseInterceptors ) {
142+ this .responseInterceptors = responseInterceptors ;
143+ }
144+
145+ public DSpaceProxyRoutePlanner getProxyRoutePlanner () {
146+ return proxyRoutePlanner ;
147+ }
148+
149+ public void setProxyRoutePlanner (DSpaceProxyRoutePlanner proxyRoutePlanner ) {
150+ this .proxyRoutePlanner = proxyRoutePlanner ;
151+ }
152+ }
0 commit comments