|
| 1 | +/* |
| 2 | + * Copyright 2019 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.bot.client; |
| 18 | + |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +import java.net.URI; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | + |
| 31 | +import com.linecorp.bot.model.objectmapper.ModelObjectMapper; |
| 32 | + |
| 33 | +import lombok.NonNull; |
| 34 | +import lombok.Setter; |
| 35 | +import lombok.ToString; |
| 36 | +import lombok.experimental.Accessors; |
| 37 | +import lombok.experimental.PackagePrivate; |
| 38 | +import okhttp3.Interceptor; |
| 39 | +import okhttp3.OkHttpClient; |
| 40 | +import okhttp3.logging.HttpLoggingInterceptor; |
| 41 | +import okhttp3.logging.HttpLoggingInterceptor.Level; |
| 42 | +import retrofit2.Retrofit; |
| 43 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 44 | + |
| 45 | +@ToString |
| 46 | +@Accessors(fluent = true) |
| 47 | +public class LineBlobClientBuilder { |
| 48 | + private static final ObjectMapper objectMapper = ModelObjectMapper.createNewObjectMapper(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Use {@link LineBlobClient#builder} to create instance. |
| 52 | + */ |
| 53 | + @PackagePrivate |
| 54 | + LineBlobClientBuilder() { |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * API Endpoint. |
| 59 | + * |
| 60 | + * <p>Default value = "https://api.line.me/". |
| 61 | + */ |
| 62 | + private URI apiEndPoint = LineClientConstants.DEFAULT_API_END_POINT; |
| 63 | + |
| 64 | + /** |
| 65 | + * API Endpoint. |
| 66 | + * |
| 67 | + * @deprecated use {@link #apiEndPoint(URI)}. |
| 68 | + */ |
| 69 | + @Deprecated |
| 70 | + public LineBlobClientBuilder apiEndPoint(String apiEndPoint) { |
| 71 | + return apiEndPoint(URI.create(apiEndPoint)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * API Endpoint. |
| 76 | + * |
| 77 | + * <p>Default value = "https://api.line.me/". |
| 78 | + */ // We can remove this after delete `setApiEndPoint(String apiEndPoint)`. |
| 79 | + public LineBlobClientBuilder apiEndPoint(URI apiEndPoint) { |
| 80 | + this.apiEndPoint = requireNonNull(apiEndPoint, "apiEndPoint"); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Connection timeout. |
| 86 | + * |
| 87 | + * <p>Default value = {@value LineClientConstants#DEFAULT_CONNECT_TIMEOUT_MILLIS}ms. |
| 88 | + */ |
| 89 | + @Setter |
| 90 | + private long connectTimeout = LineClientConstants.DEFAULT_CONNECT_TIMEOUT_MILLIS; |
| 91 | + |
| 92 | + /** |
| 93 | + * Connection timeout. |
| 94 | + * |
| 95 | + * <p>Default value = {@value LineClientConstants#DEFAULT_READ_TIMEOUT_MILLIS}ms. |
| 96 | + */ |
| 97 | + @Setter |
| 98 | + private long readTimeout = LineClientConstants.DEFAULT_READ_TIMEOUT_MILLIS; |
| 99 | + |
| 100 | + /** |
| 101 | + * Write timeout. |
| 102 | + * |
| 103 | + * <p>Default value = {@value LineClientConstants#DEFAULT_WRITE_TIMEOUT_MILLIS}ms. |
| 104 | + */ |
| 105 | + @Setter |
| 106 | + private long writeTimeout = LineClientConstants.DEFAULT_WRITE_TIMEOUT_MILLIS; |
| 107 | + |
| 108 | + /** |
| 109 | + * Channel token supplier of this client. |
| 110 | + * |
| 111 | + * <p>MUST BE NULL except you configured your own |
| 112 | + */ |
| 113 | + @Setter |
| 114 | + private ChannelTokenSupplier channelTokenSupplier; |
| 115 | + |
| 116 | + /** |
| 117 | + * Custom {@link Retrofit.Builder} used internally. |
| 118 | + * |
| 119 | + * <p>If you want to use your own setting, specify {@link Retrofit.Builder} instance. |
| 120 | + * Default builder is used in case of {@code null} (default). |
| 121 | + * |
| 122 | + * <p>To use this method, please add dependency to 'com.squareup.retrofit2:retrofit'. |
| 123 | + * |
| 124 | + * @see #createDefaultRetrofitBuilder() |
| 125 | + */ |
| 126 | + @Setter |
| 127 | + private Retrofit.Builder retrofitBuilder; |
| 128 | + |
| 129 | + /** |
| 130 | + * Add authentication header. |
| 131 | + * |
| 132 | + * <p>Default = {@value}. If you manage authentication header yourself, set to {@code false}. |
| 133 | + */ |
| 134 | + @Setter |
| 135 | + private boolean addAuthenticationHeader = true; |
| 136 | + |
| 137 | + private OkHttpClient.Builder okHttpClientBuilder; |
| 138 | + |
| 139 | + /** |
| 140 | + * Custom interceptors. |
| 141 | + * |
| 142 | + * <p>You can add your own interceptors. |
| 143 | + * |
| 144 | + * <p>Note: Authentication interceptor is automatically added by default. |
| 145 | + * |
| 146 | + * @see #addAuthenticationHeader(boolean) |
| 147 | + */ |
| 148 | + @Setter |
| 149 | + private List<Interceptor> additionalInterceptors = new ArrayList<>(); |
| 150 | + |
| 151 | + /** |
| 152 | + * Set fixed channel token. This overwrites {@link #channelTokenSupplier(ChannelTokenSupplier)}. |
| 153 | + * |
| 154 | + * @see #channelTokenSupplier(ChannelTokenSupplier) |
| 155 | + */ |
| 156 | + public LineBlobClientBuilder channelToken(String channelToken) { |
| 157 | + channelTokenSupplier(FixedChannelTokenSupplier.of(channelToken)); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Set customized OkHttpClient.Builder. |
| 163 | + * |
| 164 | + * <p>In case of you need your own customized {@link OkHttpClient}, |
| 165 | + * this builder allows specify {@link OkHttpClient.Builder} instance. |
| 166 | + * |
| 167 | + * <p>To use this method, please add dependency to 'com.squareup.retrofit2:retrofit'. |
| 168 | + * |
| 169 | + * @param addAuthenticationHeader If true, all default okhttp interceptors ignored. |
| 170 | + * You should insert authentication headers yourself. |
| 171 | + */ |
| 172 | + public LineBlobClientBuilder okHttpClientBuilder( |
| 173 | + final @NonNull OkHttpClient.Builder okHttpClientBuilder, |
| 174 | + final boolean addAuthenticationHeader) { |
| 175 | + this.okHttpClientBuilder = okHttpClientBuilder; |
| 176 | + this.addAuthenticationHeader = addAuthenticationHeader; |
| 177 | + |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Creates a new {@link LineBlobService}. |
| 183 | + */ |
| 184 | + <T> T buildRetrofitIface(URI apiEndPoint, Class<T> retrofitIFace) { |
| 185 | + if (okHttpClientBuilder == null) { |
| 186 | + okHttpClientBuilder = new OkHttpClient.Builder(); |
| 187 | + } |
| 188 | + |
| 189 | + // Add interceptors. |
| 190 | + if (addAuthenticationHeader) { |
| 191 | + okHttpClientBuilder.addInterceptor(buildAuthenticationInterceptor(channelTokenSupplier)); |
| 192 | + } |
| 193 | + if (additionalInterceptors != null) { |
| 194 | + additionalInterceptors.forEach(okHttpClientBuilder::addInterceptor); |
| 195 | + } |
| 196 | + okHttpClientBuilder.addInterceptor(buildLoggingInterceptor()); |
| 197 | + |
| 198 | + // Set timeout. |
| 199 | + okHttpClientBuilder |
| 200 | + .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) |
| 201 | + .readTimeout(readTimeout, TimeUnit.MILLISECONDS) |
| 202 | + .writeTimeout(writeTimeout, TimeUnit.MILLISECONDS); |
| 203 | + |
| 204 | + final OkHttpClient okHttpClient = okHttpClientBuilder.build(); |
| 205 | + |
| 206 | + if (retrofitBuilder == null) { |
| 207 | + retrofitBuilder = createDefaultRetrofitBuilder(); |
| 208 | + } |
| 209 | + retrofitBuilder.client(okHttpClient); |
| 210 | + retrofitBuilder.baseUrl(apiEndPoint.toString()); |
| 211 | + |
| 212 | + final Retrofit retrofit = retrofitBuilder.build(); |
| 213 | + |
| 214 | + return retrofit.create(retrofitIFace); |
| 215 | + } |
| 216 | + |
| 217 | + static HeaderInterceptor buildAuthenticationInterceptor(ChannelTokenSupplier channelTokenSupplier) { |
| 218 | + requireNonNull(channelTokenSupplier, "channelTokenSupplier"); |
| 219 | + return HeaderInterceptor.forChannelTokenSupplier(channelTokenSupplier); |
| 220 | + } |
| 221 | + |
| 222 | + static Interceptor buildLoggingInterceptor() { |
| 223 | + final Logger slf4jLogger = LoggerFactory.getLogger("com.linecorp.bot.client.wire"); |
| 224 | + |
| 225 | + return new HttpLoggingInterceptor(slf4jLogger::info) |
| 226 | + .setLevel(Level.BODY); |
| 227 | + } |
| 228 | + |
| 229 | + static Retrofit.Builder createDefaultRetrofitBuilder() { |
| 230 | + return new Retrofit.Builder() |
| 231 | + .addConverterFactory(JacksonConverterFactory.create(objectMapper)); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Creates a new {@link LineBlobService}. |
| 236 | + */ |
| 237 | + public LineBlobClient build() { |
| 238 | + return new LineBlobClientImpl( |
| 239 | + buildRetrofitIface(apiEndPoint, LineBlobService.class)); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Creates a new {@link LineBlobService}. |
| 244 | + */ |
| 245 | + public LineBlobClient buildBlobClient() { |
| 246 | + return new LineBlobClientImpl(buildRetrofitIface( |
| 247 | + apiEndPoint, |
| 248 | + LineBlobService.class)); |
| 249 | + } |
| 250 | +} |
0 commit comments