|
| 1 | +/** |
| 2 | + * Copyright 2018, 2019, 2020, 2021 SourceLab.org https://github.com/SourceLabOrg/kafka-connect-client |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 7 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.sourcelab.kafka.connect.apiclient.rest; |
| 19 | + |
| 20 | +import org.apache.http.client.AuthCache; |
| 21 | +import org.apache.http.client.CredentialsProvider; |
| 22 | +import org.apache.http.client.config.RequestConfig; |
| 23 | +import org.apache.http.client.protocol.HttpClientContext; |
| 24 | +import org.apache.http.impl.client.BasicAuthCache; |
| 25 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 26 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 27 | +import org.sourcelab.kafka.connect.apiclient.Configuration; |
| 28 | + |
| 29 | +/** |
| 30 | + * HttpClient configuration hooks. |
| 31 | + * |
| 32 | + * Provides an interface for modifying how the underlying HttpClient instance is created. |
| 33 | + * |
| 34 | + * Usage of this would look like: |
| 35 | + * |
| 36 | + * final RestClient restClient = new HttpClientRestClient(new HttpClientConfigHooks { |
| 37 | + * // Override methods as needed to modify behavior. |
| 38 | + * }); |
| 39 | + * |
| 40 | + * // Create client, passing configuration and RestClient implementation |
| 41 | + * final KafkaConnectClient client = new KafkaConnectClient(configuration, restClient); |
| 42 | + * |
| 43 | + * // Use client as normal... |
| 44 | + * |
| 45 | + */ |
| 46 | +public interface HttpClientConfigHooks { |
| 47 | + /** |
| 48 | + * Create HttpClientBuilder instance. |
| 49 | + * @param configuration KafkaConnectClient configuration. |
| 50 | + * @return HttpClientBuilder instance. |
| 51 | + */ |
| 52 | + default HttpClientBuilder createHttpClientBuilder(final Configuration configuration) { |
| 53 | + return HttpClientBuilder.create(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create HttpsContextBuilder instance. |
| 58 | + * @param configuration KafkaConnectClient configuration. |
| 59 | + * @return HttpsContextBuilder instance. |
| 60 | + */ |
| 61 | + default HttpsContextBuilder createHttpsContextBuilder(final Configuration configuration) { |
| 62 | + return new HttpsContextBuilder(configuration); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create RequestConfig.Builder instance. |
| 67 | + * @param configuration KafkaConnectClient configuration. |
| 68 | + * @return RequestConfig.Builder instance. |
| 69 | + */ |
| 70 | + default RequestConfig.Builder createRequestConfigBuilder(final Configuration configuration) { |
| 71 | + return RequestConfig.custom(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create AuthCache instance. |
| 76 | + * @param configuration KafkaConnectClient configuration. |
| 77 | + * @return AuthCache instance. |
| 78 | + */ |
| 79 | + default AuthCache createAuthCache(final Configuration configuration) { |
| 80 | + return new BasicAuthCache(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create CredentialsProvider instance. |
| 85 | + * @param configuration KafkaConnectClient configuration. |
| 86 | + * @return CredentialsProvider instance. |
| 87 | + */ |
| 88 | + default CredentialsProvider createCredentialsProvider(final Configuration configuration) { |
| 89 | + return new BasicCredentialsProvider(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create HttpClientContext instance. |
| 94 | + * @param configuration KafkaConnectClient configuration. |
| 95 | + * @return HttpClientContext instance. |
| 96 | + */ |
| 97 | + default HttpClientContext createHttpClientContext(final Configuration configuration) { |
| 98 | + return HttpClientContext.create(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Ability to modify or replace the AuthCache instance after initial configuration has been performed on it. |
| 103 | + * @param configuration KafkaConnectClient configuration. |
| 104 | + * @return AuthCache instance. |
| 105 | + */ |
| 106 | + default AuthCache modifyAuthCache(final Configuration configuration, final AuthCache authCache) { |
| 107 | + return authCache; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Ability to modify or replace the CredentialsProvider instance after initial configuration has been performed on it. |
| 112 | + * @param configuration KafkaConnectClient configuration. |
| 113 | + * @return CredentialsProvider instance. |
| 114 | + */ |
| 115 | + default CredentialsProvider modifyCredentialsProvider(final Configuration configuration, final CredentialsProvider credentialsProvider) { |
| 116 | + return credentialsProvider; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Ability to modify or replace the RequestConfig.Builder instance after initial configuration has been performed on it. |
| 121 | + * @param configuration KafkaConnectClient configuration. |
| 122 | + * @return RequestConfig.Builder instance. |
| 123 | + */ |
| 124 | + default RequestConfig.Builder modifyRequestConfig(final Configuration configuration, final RequestConfig.Builder builder) { |
| 125 | + return builder; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Ability to modify or replace the HttpClientBuilder instance after initial configuration has been performed on it. |
| 130 | + * @param configuration KafkaConnectClient configuration. |
| 131 | + * @return HttpClientBuilder instance. |
| 132 | + */ |
| 133 | + default HttpClientBuilder modifyHttpClientBuilder(final Configuration configuration, final HttpClientBuilder builder) { |
| 134 | + return builder; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Ability to modify or replace the HttpClientContext instance after initial configuration has been performed on it. |
| 139 | + * @param configuration KafkaConnectClient configuration. |
| 140 | + * @return HttpClientContext instance. |
| 141 | + */ |
| 142 | + default HttpClientContext modifyHttpClientContext(final Configuration configuration, final HttpClientContext context) { |
| 143 | + return context; |
| 144 | + } |
| 145 | +} |
0 commit comments