|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.juneau.ng.rest.client.apachehttpclient45; |
| 18 | + |
| 19 | +import java.io.*; |
| 20 | + |
| 21 | +import org.apache.http.*; |
| 22 | +import org.apache.http.client.methods.*; |
| 23 | +import org.apache.http.entity.*; |
| 24 | +import org.apache.http.impl.client.*; |
| 25 | +import org.apache.juneau.ng.rest.client.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * {@link HttpTransport} implementation backed by Apache HttpClient 4.5. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * This transport is auto-discovered via {@link java.util.ServiceLoader} when |
| 32 | + * {@code org.apache.httpcomponents:httpclient} is on the classpath. You can also instantiate it explicitly: |
| 33 | + * |
| 34 | + * <p class='bjava'> |
| 35 | + * <jv>transport</jv> = ApacheHc45Transport.<jsm>builder</jsm>() |
| 36 | + * .httpClient(HttpClients.createDefault()) |
| 37 | + * .build(); |
| 38 | + * |
| 39 | + * <jv>client</jv> = NgRestClient.<jsm>builder</jsm>() |
| 40 | + * .transport(<jv>transport</jv>) |
| 41 | + * .build(); |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * <p> |
| 45 | + * <b>Beta — API subject to change:</b> This type is part of the next-generation REST client and HTTP stack |
| 46 | + * ({@code org.apache.juneau.ng.*}). |
| 47 | + * It is not API-frozen: binary- and source-incompatible changes may appear in the <b>next major</b> Juneau release |
| 48 | + * (and possibly earlier). |
| 49 | + * |
| 50 | + * <h5 class='section'>See Also:</h5><ul> |
| 51 | + * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/juneau-ng-rest-client">juneau-ng REST client</a> |
| 52 | + * </ul> |
| 53 | + * |
| 54 | + * @since 9.2.1 |
| 55 | + */ |
| 56 | +@SuppressWarnings({ |
| 57 | + "resource" // httpClient is owned by this transport and closed in close() |
| 58 | +}) |
| 59 | +public final class ApacheHc45Transport implements HttpTransport { |
| 60 | + |
| 61 | + private final CloseableHttpClient httpClient; |
| 62 | + |
| 63 | + ApacheHc45Transport(ApacheHc45TransportBuilder builder) { |
| 64 | + this.httpClient = builder.httpClient != null ? builder.httpClient : HttpClients.createDefault(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns a new builder for this transport. |
| 69 | + * |
| 70 | + * @return A new builder. Never <jk>null</jk>. |
| 71 | + */ |
| 72 | + public static ApacheHc45TransportBuilder builder() { |
| 73 | + return new ApacheHc45TransportBuilder(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns a new instance backed by a default {@link CloseableHttpClient}. |
| 78 | + * |
| 79 | + * @return A new instance. Never <jk>null</jk>. |
| 80 | + */ |
| 81 | + public static ApacheHc45Transport create() { |
| 82 | + return builder().build(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override /* HttpTransport */ |
| 86 | + public TransportResponse execute(TransportRequest request) throws TransportException { |
| 87 | + var hcRequest = buildHcRequest(request); |
| 88 | + CloseableHttpResponse hcResponse; |
| 89 | + try { |
| 90 | + hcResponse = httpClient.execute(hcRequest); |
| 91 | + } catch (IOException e) { |
| 92 | + throw new TransportException("HTTP transport error: " + e.getMessage(), e); |
| 93 | + } |
| 94 | + return buildTransportResponse(hcResponse); |
| 95 | + } |
| 96 | + |
| 97 | + @Override /* Closeable */ |
| 98 | + public void close() throws IOException { |
| 99 | + httpClient.close(); |
| 100 | + } |
| 101 | + |
| 102 | + // ----------------------------------------------------------------------------------------------------------------- |
| 103 | + // Internal helpers |
| 104 | + // ----------------------------------------------------------------------------------------------------------------- |
| 105 | + |
| 106 | + private static HttpUriRequest buildHcRequest(TransportRequest request) throws TransportException { |
| 107 | + var builder = RequestBuilder.create(request.getMethod()).setUri(request.getUri()); |
| 108 | + for (var h : request.getHeaders()) |
| 109 | + builder.addHeader(h.name(), h.value()); |
| 110 | + var body = request.getBody(); |
| 111 | + if (body != null) |
| 112 | + builder.setEntity(new TransportBodyEntity(body)); |
| 113 | + try { |
| 114 | + return builder.build(); |
| 115 | + } catch (Exception e) { |
| 116 | + throw new TransportException("Failed to build HTTP request: " + e.getMessage(), e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static TransportResponse buildTransportResponse(CloseableHttpResponse hcResponse) throws TransportException { |
| 121 | + var statusLine = hcResponse.getStatusLine(); |
| 122 | + var builder = TransportResponse.builder() |
| 123 | + .statusCode(statusLine.getStatusCode()) |
| 124 | + .reasonPhrase(statusLine.getReasonPhrase()) |
| 125 | + .closeCallback(hcResponse); |
| 126 | + for (var h : hcResponse.getAllHeaders()) |
| 127 | + builder.header(h.getName(), h.getValue()); |
| 128 | + var entity = hcResponse.getEntity(); |
| 129 | + if (entity != null) { |
| 130 | + try { |
| 131 | + builder.body(entity.getContent()); |
| 132 | + } catch (IOException e) { |
| 133 | + throw new TransportException("Failed to read response body: " + e.getMessage(), e); |
| 134 | + } |
| 135 | + } |
| 136 | + return builder.build(); |
| 137 | + } |
| 138 | + |
| 139 | + // ----------------------------------------------------------------------------------------------------------------- |
| 140 | + // TransportBodyEntity — bridges TransportBody to Apache HttpEntity |
| 141 | + // ----------------------------------------------------------------------------------------------------------------- |
| 142 | + |
| 143 | + /** |
| 144 | + * Bridges a {@link TransportBody} to Apache HttpClient's {@link AbstractHttpEntity}. |
| 145 | + */ |
| 146 | + private static final class TransportBodyEntity extends AbstractHttpEntity { |
| 147 | + |
| 148 | + private final TransportBody body; |
| 149 | + |
| 150 | + TransportBodyEntity(TransportBody body) { |
| 151 | + this.body = body; |
| 152 | + var ct = body.getContentType(); |
| 153 | + if (ct != null) |
| 154 | + setContentType(ct); |
| 155 | + } |
| 156 | + |
| 157 | + @Override /* HttpEntity */ |
| 158 | + public boolean isRepeatable() { |
| 159 | + return body.isRepeatable(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override /* HttpEntity */ |
| 163 | + public long getContentLength() { |
| 164 | + return body.getContentLength(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override /* HttpEntity */ |
| 168 | + public InputStream getContent() throws UnsupportedOperationException { |
| 169 | + throw new UnsupportedOperationException("Use writeTo(OutputStream) instead"); |
| 170 | + } |
| 171 | + |
| 172 | + @Override /* HttpEntity */ |
| 173 | + public void writeTo(OutputStream out) throws IOException { |
| 174 | + body.writeTo(out); |
| 175 | + } |
| 176 | + |
| 177 | + @Override /* HttpEntity */ |
| 178 | + public boolean isStreaming() { |
| 179 | + return !body.isRepeatable(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments