|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.URL; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import okhttp3.OkHttpClient; |
| 28 | +import okhttp3.Request; |
| 29 | +import okhttp3.RequestBody; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.DisposableBean; |
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.http.HttpMethod; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link ClientHttpRequestFactory} implementation that uses |
| 39 | + * <a href="http://square.github.io/okhttp/">OkHttp</a> 3.x to create requests. |
| 40 | + * |
| 41 | + * @author Luciano Leggieri |
| 42 | + * @author Arjen Poutsma |
| 43 | + * @author Roy Clarkson |
| 44 | + * @since 4.3 |
| 45 | + */ |
| 46 | +public class OkHttp3ClientHttpRequestFactory |
| 47 | + implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, DisposableBean { |
| 48 | + |
| 49 | + private OkHttpClient client; |
| 50 | + |
| 51 | + private final boolean defaultClient; |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a factory with a default {@link OkHttpClient} instance. |
| 56 | + */ |
| 57 | + public OkHttp3ClientHttpRequestFactory() { |
| 58 | + this.client = new OkHttpClient(); |
| 59 | + this.defaultClient = true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Create a factory with the given {@link OkHttpClient} instance. |
| 64 | + * @param client the client to use |
| 65 | + */ |
| 66 | + public OkHttp3ClientHttpRequestFactory(OkHttpClient client) { |
| 67 | + Assert.notNull(client, "OkHttpClient must not be null"); |
| 68 | + this.client = client; |
| 69 | + this.defaultClient = false; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * Sets the underlying read timeout in milliseconds. |
| 75 | + * A value of 0 specifies an infinite timeout. |
| 76 | + * @see okhttp3.OkHttpClient.Builder#readTimeout(long, TimeUnit) |
| 77 | + */ |
| 78 | + public void setReadTimeout(int readTimeout) { |
| 79 | + this.client = this.client.newBuilder() |
| 80 | + .readTimeout(readTimeout, TimeUnit.MILLISECONDS) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets the underlying write timeout in milliseconds. |
| 86 | + * A value of 0 specifies an infinite timeout. |
| 87 | + * @see okhttp3.OkHttpClient.Builder#writeTimeout(long, TimeUnit) |
| 88 | + */ |
| 89 | + public void setWriteTimeout(int writeTimeout) { |
| 90 | + this.client = this.client.newBuilder() |
| 91 | + .writeTimeout(writeTimeout, TimeUnit.MILLISECONDS) |
| 92 | + .build(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets the underlying connect timeout in milliseconds. |
| 97 | + * A value of 0 specifies an infinite timeout. |
| 98 | + * @see okhttp3.OkHttpClient.Builder#connectTimeout(long, TimeUnit) |
| 99 | + */ |
| 100 | + public void setConnectTimeout(int connectTimeout) { |
| 101 | + this.client = this.client.newBuilder() |
| 102 | + .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) |
| 103 | + .build(); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + @Override |
| 108 | + public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) { |
| 109 | + return new OkHttp3ClientHttpRequest(this.client, uri, httpMethod); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) { |
| 114 | + return new OkHttp3AsyncClientHttpRequest(this.client, uri, httpMethod); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Override |
| 119 | + public void destroy() throws IOException { |
| 120 | + if (this.defaultClient) { |
| 121 | + // Clean up the client if we created it in the constructor |
| 122 | + if (this.client.cache() != null) { |
| 123 | + this.client.cache().close(); |
| 124 | + } |
| 125 | + this.client.dispatcher().executorService().shutdown(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, |
| 131 | + HttpMethod method) throws MalformedURLException { |
| 132 | + |
| 133 | + okhttp3.MediaType contentType = getContentType(headers); |
| 134 | + RequestBody body = (content.length > 0 ? RequestBody.create(contentType, content) : null); |
| 135 | + |
| 136 | + URL url = uri.toURL(); |
| 137 | + String methodName = method.name(); |
| 138 | + Request.Builder builder = new Request.Builder().url(url).method(methodName, body); |
| 139 | + |
| 140 | + for (Map.Entry<String, List<String>> entry : headers.entrySet()) { |
| 141 | + String headerName = entry.getKey(); |
| 142 | + for (String headerValue : entry.getValue()) { |
| 143 | + builder.addHeader(headerName, headerValue); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return builder.build(); |
| 148 | + } |
| 149 | + |
| 150 | + private static okhttp3.MediaType getContentType(HttpHeaders headers) { |
| 151 | + String rawContentType = headers.getFirst(HttpHeaders.CONTENT_TYPE); |
| 152 | + return (StringUtils.hasText(rawContentType) ? okhttp3.MediaType.parse(rawContentType) : null); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments