|
| 1 | +/* |
| 2 | + * Licensed to Islandora Foundation under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for additional |
| 4 | + * information regarding copyright ownership. |
| 5 | + * |
| 6 | + * The Islandora Foundation licenses this file to you under the MIT License. |
| 7 | + * You may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://opensource.org/licenses/MIT |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package ca.islandora.alpaca.connector.derivative; |
| 20 | + |
| 21 | +import org.apache.camel.component.http4.HttpClientConfigurer; |
| 22 | +import org.apache.http.client.config.RequestConfig; |
| 23 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * Provides a default HttpClient {@link RequestConfig} with custom values for connection request, connect, and socket |
| 27 | + * timeout values. Custom values must be set on this class prior to invoking |
| 28 | + * {@link #configureHttpClient(HttpClientBuilder)}. |
| 29 | + * |
| 30 | + * @author Elliot Metsger ([email protected]) |
| 31 | + */ |
| 32 | +public class RequestConfigConfigurer implements HttpClientConfigurer { |
| 33 | + |
| 34 | + /** |
| 35 | + * The RequestConfig instance that is built by this configurer; exposed for testing purposes. |
| 36 | + */ |
| 37 | + RequestConfig built; |
| 38 | + |
| 39 | + private int connectionRequestTimeoutMs = RequestConfig.DEFAULT.getConnectionRequestTimeout(); |
| 40 | + |
| 41 | + private int connectTimeoutMs = RequestConfig.DEFAULT.getConnectTimeout(); |
| 42 | + |
| 43 | + private int socketTimeoutMs = RequestConfig.DEFAULT.getSocketTimeout(); |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a {@link RequestConfig} using custom values for {@code connectionRequestTimeout}, {@code connectTimeout}, |
| 47 | + * and {@code socketTimeout}. If custom values are not provided by this class, then the default values from |
| 48 | + * {@link RequestConfig#DEFAULT} are used. |
| 49 | + * |
| 50 | + * @param clientBuilder the HttpClientBuilder |
| 51 | + * @see #setConnectionRequestTimeoutMs(int) |
| 52 | + * @see #setConnectTimeoutMs(int) |
| 53 | + * @see #setSocketTimeoutMs(int) |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public void configureHttpClient(final HttpClientBuilder clientBuilder) { |
| 57 | + final RequestConfig.Builder builder = RequestConfig.copy(RequestConfig.DEFAULT); |
| 58 | + final RequestConfig config = buildConfig(builder); |
| 59 | + clientBuilder.setDefaultRequestConfig(config); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Package-private to support testing. |
| 64 | + * |
| 65 | + * @param builder the RequestConfig builder |
| 66 | + * @return the RequestConfig |
| 67 | + */ |
| 68 | + RequestConfig buildConfig(final RequestConfig.Builder builder) { |
| 69 | + builder.setConnectionRequestTimeout(connectionRequestTimeoutMs) |
| 70 | + .setSocketTimeout(socketTimeoutMs) |
| 71 | + .setConnectTimeout(connectTimeoutMs); |
| 72 | + built = builder.build(); |
| 73 | + return built; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the value of the connection request timeout |
| 78 | + * |
| 79 | + * @return the value |
| 80 | + */ |
| 81 | + public int getConnectionRequestTimeoutMs() { |
| 82 | + return connectionRequestTimeoutMs; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set the value of the connection request timeout |
| 87 | + * |
| 88 | + * @param connectionRequestTimeoutMs the value |
| 89 | + */ |
| 90 | + public void setConnectionRequestTimeoutMs(final int connectionRequestTimeoutMs) { |
| 91 | + this.connectionRequestTimeoutMs = connectionRequestTimeoutMs; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the value of the connect timeout |
| 96 | + * |
| 97 | + * @return the value |
| 98 | + */ |
| 99 | + public int getConnectTimeoutMs() { |
| 100 | + return connectTimeoutMs; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Set the value of the connect timeout |
| 105 | + * |
| 106 | + * @param connectTimeoutMs the value |
| 107 | + */ |
| 108 | + public void setConnectTimeoutMs(final int connectTimeoutMs) { |
| 109 | + this.connectTimeoutMs = connectTimeoutMs; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the value of the socket timeout |
| 114 | + * |
| 115 | + * @return the value |
| 116 | + */ |
| 117 | + public int getSocketTimeoutMs() { |
| 118 | + return socketTimeoutMs; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Set the value of the socket timeout |
| 123 | + * |
| 124 | + * @param socketTimeoutMs the value |
| 125 | + */ |
| 126 | + public void setSocketTimeoutMs(final int socketTimeoutMs) { |
| 127 | + this.socketTimeoutMs = socketTimeoutMs; |
| 128 | + } |
| 129 | +} |
0 commit comments