|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.apachehttpclient.v5_2; |
| 7 | + |
| 8 | +import static java.util.logging.Level.FINE; |
| 9 | + |
| 10 | +import java.net.InetAddress; |
| 11 | +import java.net.InetSocketAddress; |
| 12 | +import java.net.URI; |
| 13 | +import java.net.URISyntaxException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.logging.Logger; |
| 18 | +import javax.annotation.Nullable; |
| 19 | +import org.apache.hc.core5.http.Header; |
| 20 | +import org.apache.hc.core5.http.HttpHost; |
| 21 | +import org.apache.hc.core5.http.HttpRequest; |
| 22 | +import org.apache.hc.core5.http.ProtocolVersion; |
| 23 | + |
| 24 | +final class ApacheHttpClient5Request { |
| 25 | + |
| 26 | + private static final Logger logger = Logger.getLogger(ApacheHttpClient5Request.class.getName()); |
| 27 | + |
| 28 | + @Nullable private final URI uri; |
| 29 | + |
| 30 | + private final HttpRequest delegate; |
| 31 | + @Nullable private final HttpHost target; |
| 32 | + |
| 33 | + ApacheHttpClient5Request(@Nullable HttpHost httpHost, HttpRequest httpRequest) { |
| 34 | + URI calculatedUri = getUri(httpRequest); |
| 35 | + if (calculatedUri != null && httpHost != null) { |
| 36 | + uri = getCalculatedUri(httpHost, calculatedUri); |
| 37 | + } else { |
| 38 | + uri = calculatedUri; |
| 39 | + } |
| 40 | + delegate = httpRequest; |
| 41 | + target = httpHost; |
| 42 | + } |
| 43 | + |
| 44 | + /** Returns the actual {@link HttpRequest} being executed by the client. */ |
| 45 | + public HttpRequest getDelegate() { |
| 46 | + return delegate; |
| 47 | + } |
| 48 | + |
| 49 | + List<String> getHeader(String name) { |
| 50 | + return headersToList(delegate.getHeaders(name)); |
| 51 | + } |
| 52 | + |
| 53 | + // minimize memory overhead by not using streams |
| 54 | + static List<String> headersToList(Header[] headers) { |
| 55 | + if (headers.length == 0) { |
| 56 | + return Collections.emptyList(); |
| 57 | + } |
| 58 | + List<String> headersList = new ArrayList<>(headers.length); |
| 59 | + for (Header header : headers) { |
| 60 | + headersList.add(header.getValue()); |
| 61 | + } |
| 62 | + return headersList; |
| 63 | + } |
| 64 | + |
| 65 | + void setHeader(String name, String value) { |
| 66 | + delegate.setHeader(name, value); |
| 67 | + } |
| 68 | + |
| 69 | + String getMethod() { |
| 70 | + return delegate.getMethod(); |
| 71 | + } |
| 72 | + |
| 73 | + @Nullable |
| 74 | + String getUrl() { |
| 75 | + return uri != null ? uri.toString() : null; |
| 76 | + } |
| 77 | + |
| 78 | + String getProtocolName() { |
| 79 | + return delegate.getVersion().getProtocol(); |
| 80 | + } |
| 81 | + |
| 82 | + String getProtocolVersion() { |
| 83 | + ProtocolVersion protocolVersion = delegate.getVersion(); |
| 84 | + if (protocolVersion.getMinor() == 0) { |
| 85 | + return Integer.toString(protocolVersion.getMajor()); |
| 86 | + } |
| 87 | + return protocolVersion.getMajor() + "." + protocolVersion.getMinor(); |
| 88 | + } |
| 89 | + |
| 90 | + @Nullable |
| 91 | + public String getServerAddress() { |
| 92 | + return uri == null ? null : uri.getHost(); |
| 93 | + } |
| 94 | + |
| 95 | + @Nullable |
| 96 | + public Integer getServerPort() { |
| 97 | + return uri == null ? null : uri.getPort(); |
| 98 | + } |
| 99 | + |
| 100 | + @Nullable |
| 101 | + private static URI getUri(HttpRequest httpRequest) { |
| 102 | + try { |
| 103 | + // this can be relative or absolute |
| 104 | + return new URI(httpRequest.getUri().toString()); |
| 105 | + } catch (URISyntaxException e) { |
| 106 | + logger.log(FINE, e.getMessage(), e); |
| 107 | + return null; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Nullable |
| 112 | + private static URI getCalculatedUri(HttpHost httpHost, URI uri) { |
| 113 | + try { |
| 114 | + return new URI( |
| 115 | + httpHost.getSchemeName(), |
| 116 | + uri.getUserInfo(), |
| 117 | + httpHost.getHostName(), |
| 118 | + httpHost.getPort(), |
| 119 | + uri.getPath(), |
| 120 | + uri.getQuery(), |
| 121 | + uri.getFragment()); |
| 122 | + } catch (URISyntaxException e) { |
| 123 | + logger.log(FINE, e.getMessage(), e); |
| 124 | + return null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Nullable |
| 129 | + public InetSocketAddress getServerSocketAddress() { |
| 130 | + if (target == null) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + InetAddress inetAddress = target.getAddress(); |
| 134 | + return inetAddress == null ? null : new InetSocketAddress(inetAddress, target.getPort()); |
| 135 | + } |
| 136 | +} |
0 commit comments