-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace deprecated Spring Framework classes
- Loading branch information
1 parent
5598deb
commit 54d6b4d
Showing
12 changed files
with
406 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
autoconfigure/src/main/java/io/freefair/spring/okhttp/client/OkHttpClientRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.freefair.spring.okhttp.client; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import okhttp3.*; | ||
import okio.Buffer; | ||
import okio.ByteString; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.StreamingHttpOutputMessage; | ||
import org.springframework.http.client.AbstractClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
|
||
/** | ||
* OkHttp based {@link ClientHttpRequest} implementation. | ||
* | ||
* @author Lars Grefer | ||
* @see OkHttpClientRequestFactory | ||
*/ | ||
@RequiredArgsConstructor | ||
class OkHttpClientRequest extends AbstractClientHttpRequest implements StreamingHttpOutputMessage { | ||
|
||
private final OkHttpClient okHttpClient; | ||
|
||
private final URI uri; | ||
|
||
private final HttpMethod method; | ||
|
||
|
||
@Nullable | ||
private Body streamingBody; | ||
|
||
@Nullable | ||
private Buffer bufferBody; | ||
|
||
|
||
@Override | ||
public HttpMethod getMethod() { | ||
return method; | ||
} | ||
|
||
@Override | ||
public URI getURI() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public void setBody(Body body) { | ||
Assert.notNull(body, "body must not be null"); | ||
assertNotExecuted(); | ||
Assert.state(bufferBody == null, "getBody has already been used."); | ||
this.streamingBody = body; | ||
} | ||
|
||
@Override | ||
protected OutputStream getBodyInternal(HttpHeaders headers) { | ||
Assert.state(this.streamingBody == null, "setBody has already been used."); | ||
|
||
if (bufferBody == null) { | ||
bufferBody = new Buffer(); | ||
} | ||
|
||
return bufferBody.outputStream(); | ||
} | ||
|
||
@Override | ||
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { | ||
|
||
Request okHttpRequest = buildRequest(headers); | ||
|
||
Response okHttpResponse = this.okHttpClient.newCall(okHttpRequest).execute(); | ||
|
||
return new OkHttpClientResponse(okHttpResponse); | ||
} | ||
|
||
private Request buildRequest(HttpHeaders headers) throws MalformedURLException { | ||
|
||
Request.Builder builder = new Request.Builder(); | ||
|
||
builder.url(uri.toURL()); | ||
|
||
MediaType contentType = null; | ||
|
||
String contentTypeHeader = headers.getFirst(HttpHeaders.CONTENT_TYPE); | ||
if (StringUtils.hasText(contentTypeHeader)) { | ||
contentType = MediaType.parse(contentTypeHeader); | ||
} | ||
|
||
RequestBody body = null; | ||
|
||
if (bufferBody != null) { | ||
ByteString bodyData = bufferBody.readByteString(); | ||
if (headers.getContentLength() < 0) { | ||
headers.setContentLength(bodyData.size()); | ||
} | ||
body = RequestBody.create(bodyData, contentType); | ||
} else if (streamingBody != null) { | ||
body = new StreamingBodyRequestBody(streamingBody, contentType, headers.getContentLength()); | ||
} else if (okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name())) { | ||
body = RequestBody.create(new byte[0], contentType); | ||
} | ||
|
||
builder.method(getMethod().name(), body); | ||
|
||
headers.forEach((name, values) -> { | ||
for (String value : values) { | ||
builder.addHeader(name, value); | ||
} | ||
}); | ||
|
||
return builder.build(); | ||
|
||
|
||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
autoconfigure/src/main/java/io/freefair/spring/okhttp/client/OkHttpClientRequestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.freefair.spring.okhttp.client; | ||
|
||
import lombok.NonNull; | ||
import okhttp3.OkHttpClient; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* OkHttp based {@link ClientHttpRequestFactory} implementation. | ||
* <p> | ||
* Serves as replacement for the deprecated {@link org.springframework.http.client.OkHttp3ClientHttpRequestFactory}. | ||
* | ||
* @author Lars Grefer | ||
*/ | ||
public record OkHttpClientRequestFactory(@NonNull OkHttpClient okHttpClient) implements ClientHttpRequestFactory { | ||
|
||
@Override | ||
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) { | ||
return new OkHttpClientRequest(okHttpClient, uri, httpMethod); | ||
} | ||
|
||
|
||
} |
78 changes: 78 additions & 0 deletions
78
autoconfigure/src/main/java/io/freefair/spring/okhttp/client/OkHttpClientResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.freefair.spring.okhttp.client; | ||
|
||
import kotlin.Pair; | ||
import lombok.RequiredArgsConstructor; | ||
import okhttp3.Headers; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import java.io.InputStream; | ||
|
||
/** | ||
* OkHttp based {@link ClientHttpResponse} implementation. | ||
* | ||
* @author Lars Grefer | ||
* @see OkHttpClientRequest | ||
*/ | ||
@RequiredArgsConstructor | ||
class OkHttpClientResponse implements ClientHttpResponse { | ||
|
||
private final Response okHttpResponse; | ||
|
||
private HttpHeaders springHeaders; | ||
|
||
@Override | ||
public HttpStatusCode getStatusCode() { | ||
return HttpStatusCode.valueOf(okHttpResponse.code()); | ||
} | ||
|
||
@Override | ||
public String getStatusText() { | ||
return okHttpResponse.message(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
ResponseBody body = okHttpResponse.body(); | ||
if (body != null) { | ||
body.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public InputStream getBody() { | ||
ResponseBody body = okHttpResponse.body(); | ||
if (body != null) { | ||
return body.byteStream(); | ||
} else { | ||
return InputStream.nullInputStream(); | ||
} | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
if (springHeaders == null) { | ||
springHeaders = convertHeaders(okHttpResponse.headers()); | ||
} | ||
|
||
return springHeaders; | ||
} | ||
|
||
/** | ||
* Converts the given {@link Headers OkHttp Headers} to {@link HttpHeaders Spring Web HttpHeaders} | ||
*/ | ||
static HttpHeaders convertHeaders(Headers okHttpHeaders) { | ||
HttpHeaders springHeaders = new HttpHeaders(); | ||
|
||
for (Pair<? extends String, ? extends String> header : okHttpHeaders) { | ||
springHeaders.add(header.getFirst(), header.getSecond()); | ||
} | ||
|
||
return springHeaders; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.