|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Joyent, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | +package com.joyent.manta.http; |
| 9 | + |
| 10 | +import com.joyent.manta.exception.MantaIOException; |
| 11 | +import com.joyent.manta.exception.MantaNoHttpResponseException; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.apache.http.HttpClientConnection; |
| 14 | +import org.apache.http.HttpException; |
| 15 | +import org.apache.http.HttpRequest; |
| 16 | +import org.apache.http.HttpResponse; |
| 17 | +import org.apache.http.NoHttpResponseException; |
| 18 | +import org.apache.http.protocol.HttpContext; |
| 19 | +import org.apache.http.protocol.HttpRequestExecutor; |
| 20 | +import org.slf4j.MDC; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Extended implementation of {@link HttpRequestExecutor} with Manta specific |
| 26 | + * extensions for logging and exception handling. |
| 27 | + * |
| 28 | + * @author <a href="https://github.com/dekobon">Elijah Zupancic</a> |
| 29 | + * @since 3.1.7 |
| 30 | + */ |
| 31 | +public class MantaHttpRequestExecutor extends HttpRequestExecutor { |
| 32 | + /** |
| 33 | + * Creates new instance of HttpRequestExecutor. |
| 34 | + * |
| 35 | + * @param waitForContinue Maximum time in milliseconds to wait for a 100-continue response |
| 36 | + */ |
| 37 | + public MantaHttpRequestExecutor(final int waitForContinue) { |
| 38 | + super(waitForContinue); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates new instance of HttpRequestExecutor. |
| 43 | + */ |
| 44 | + public MantaHttpRequestExecutor() { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Adds a context value for the Manta load balancer associated with the |
| 49 | + * request to the MDC object with the key <code>mantaLoadBalancerAddress</code> |
| 50 | + * and proxies the parent class |
| 51 | + * {@link HttpRequestExecutor#doSendRequest(HttpRequest, HttpClientConnection, HttpContext)} |
| 52 | + * method. |
| 53 | + * |
| 54 | + * {@inheritDoc} |
| 55 | + */ |
| 56 | + @Override |
| 57 | + protected HttpResponse doSendRequest(final HttpRequest request, |
| 58 | + final HttpClientConnection conn, |
| 59 | + final HttpContext context) throws IOException, HttpException { |
| 60 | + MDC.put("mantaLoadBalancerAddress", extractLoadBalancerAddress(conn)); |
| 61 | + return super.doSendRequest(request, conn, context); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Proxies the parent class |
| 66 | + * {@link HttpRequestExecutor#doReceiveResponse(HttpRequest, HttpClientConnection, HttpContext)} |
| 67 | + * method and catches {@link IOException} instances thrown. Those exceptions |
| 68 | + * are then wrapped in a {@link MantaIOException} or |
| 69 | + * {@link MantaNoHttpResponseException} instance in order to provide |
| 70 | + * detailed information for debugging. |
| 71 | + * |
| 72 | + * {@inheritDoc} |
| 73 | + */ |
| 74 | + @Override |
| 75 | + protected HttpResponse doReceiveResponse( |
| 76 | + final HttpRequest request, |
| 77 | + final HttpClientConnection conn, |
| 78 | + final HttpContext context) throws HttpException, IOException { |
| 79 | + HttpResponse response = null; |
| 80 | + |
| 81 | + try { |
| 82 | + response = super.doReceiveResponse(request, conn, context); |
| 83 | + |
| 84 | + /* We catch all IOExceptions and wrap then in a MantaIOException because |
| 85 | + * this allows us to capture key information like the request id and |
| 86 | + * load balancer address directly in the exception message. */ |
| 87 | + } catch (IOException e) { |
| 88 | + final MantaIOException mioe; |
| 89 | + |
| 90 | + /* If the source exception is NoHttpResponseException we create |
| 91 | + * a MantaNoHttpResponseException, so that we can act upon that |
| 92 | + * exception type directly within Manta. */ |
| 93 | + if (e instanceof NoHttpResponseException) { |
| 94 | + mioe = new MantaNoHttpResponseException(e); |
| 95 | + } else { |
| 96 | + mioe = new MantaIOException(e); |
| 97 | + } |
| 98 | + |
| 99 | + HttpHelper.annotateContextedException(mioe, request, response); |
| 100 | + |
| 101 | + if (request.getFirstHeader(MantaHttpHeaders.REQUEST_ID) != null) { |
| 102 | + mioe.setContextValue("requestId", |
| 103 | + request.getFirstHeader(MantaHttpHeaders.REQUEST_ID).getValue()); |
| 104 | + } |
| 105 | + |
| 106 | + mioe.setContextValue("loadBalancerAddress", extractLoadBalancerAddress(conn)); |
| 107 | + |
| 108 | + throw mioe; |
| 109 | + } |
| 110 | + |
| 111 | + return response; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Extracts the remote load balancer IP address from the toString() method |
| 116 | + * of a {@link HttpClientConnection}. |
| 117 | + * |
| 118 | + * @param conn connection to extract IP information from |
| 119 | + * @return IP address string or null if connection is null |
| 120 | + */ |
| 121 | + private static String extractLoadBalancerAddress(final HttpClientConnection conn) { |
| 122 | + if (conn == null) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + return StringUtils.substringBetween(conn.toString(), "<->", ":"); |
| 127 | + } |
| 128 | +} |
0 commit comments