|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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 | + * https://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.io.OutputStream; |
| 21 | +import java.net.URI; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.concurrent.ExecutionException; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.TimeoutException; |
| 26 | + |
| 27 | +import org.eclipse.jetty.client.api.Request; |
| 28 | +import org.eclipse.jetty.client.api.Response; |
| 29 | +import org.eclipse.jetty.client.util.InputStreamResponseListener; |
| 30 | +import org.eclipse.jetty.client.util.OutputStreamRequestContent; |
| 31 | + |
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.http.HttpMethod; |
| 34 | +import org.springframework.lang.Nullable; |
| 35 | +import org.springframework.util.StreamUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link ClientHttpRequest} implementation based on Jetty's |
| 39 | + * {@link org.eclipse.jetty.client.HttpClient}. |
| 40 | + * |
| 41 | + * @author Arjen Poutsma |
| 42 | + * @since 6.1 |
| 43 | + * @see JettyClientHttpRequestFactory |
| 44 | + */ |
| 45 | +class JettyClientHttpRequest extends AbstractStreamingClientHttpRequest { |
| 46 | + |
| 47 | + private final Request request; |
| 48 | + |
| 49 | + private final Duration timeOut; |
| 50 | + |
| 51 | + |
| 52 | + public JettyClientHttpRequest(Request request, Duration timeOut) { |
| 53 | + this.request = request; |
| 54 | + this.timeOut = timeOut; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public HttpMethod getMethod() { |
| 59 | + return HttpMethod.valueOf(this.request.getMethod()); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public URI getURI() { |
| 64 | + return this.request.getURI(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException { |
| 69 | + if (!headers.isEmpty()) { |
| 70 | + this.request.headers(httpFields -> { |
| 71 | + headers.forEach((headerName, headerValues) -> { |
| 72 | + for (String headerValue : headerValues) { |
| 73 | + httpFields.add(headerName, headerValue); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + String contentType = null; |
| 79 | + if (headers.getContentType() != null) { |
| 80 | + contentType = headers.getContentType().toString(); |
| 81 | + } |
| 82 | + try { |
| 83 | + InputStreamResponseListener responseListener = new InputStreamResponseListener(); |
| 84 | + if (body != null) { |
| 85 | + OutputStreamRequestContent requestContent = new OutputStreamRequestContent(contentType); |
| 86 | + this.request.body(requestContent) |
| 87 | + .send(responseListener); |
| 88 | + try (OutputStream outputStream = requestContent.getOutputStream()) { |
| 89 | + body.writeTo(StreamUtils.nonClosing(outputStream)); |
| 90 | + } |
| 91 | + } |
| 92 | + else { |
| 93 | + this.request.send(responseListener); |
| 94 | + } |
| 95 | + Response response = responseListener.get(TimeUnit.MILLISECONDS.convert(this.timeOut), TimeUnit.MILLISECONDS); |
| 96 | + return new JettyClientHttpResponse(response, responseListener.getInputStream()); |
| 97 | + } |
| 98 | + catch (InterruptedException | TimeoutException | ExecutionException ex) { |
| 99 | + throw new IOException("Could not send request: " + ex.getMessage(), ex); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments