|
| 1 | +/* |
| 2 | + * Copyright 2005-2024 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 | + * http://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 | +package org.springframework.ws.client.core.observation; |
| 17 | + |
| 18 | +import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; |
| 19 | +import io.micrometer.observation.Observation; |
| 20 | +import io.micrometer.observation.ObservationRegistry; |
| 21 | +import org.springframework.lang.NonNull; |
| 22 | +import org.springframework.lang.Nullable; |
| 23 | +import org.springframework.ws.FaultAwareWebServiceMessage; |
| 24 | +import org.springframework.ws.WebServiceMessage; |
| 25 | +import org.springframework.ws.client.WebServiceClientException; |
| 26 | +import org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter; |
| 27 | +import org.springframework.ws.context.MessageContext; |
| 28 | +import org.springframework.ws.soap.SoapMessage; |
| 29 | +import org.springframework.ws.support.ObservationHelper; |
| 30 | +import org.springframework.ws.transport.HeadersAwareSenderWebServiceConnection; |
| 31 | +import org.springframework.ws.transport.TransportConstants; |
| 32 | +import org.springframework.ws.transport.WebServiceConnection; |
| 33 | +import org.springframework.ws.transport.context.TransportContext; |
| 34 | +import org.springframework.ws.transport.context.TransportContextHolder; |
| 35 | + |
| 36 | +import javax.xml.namespace.QName; |
| 37 | +import javax.xml.transform.Source; |
| 38 | +import java.net.URI; |
| 39 | +import java.net.URISyntaxException; |
| 40 | + |
| 41 | +/** |
| 42 | + * Interceptor that creates an Observation for each operation. |
| 43 | + * |
| 44 | + * @author Johan Kindgren |
| 45 | + * @see Observation |
| 46 | + * @see io.micrometer.observation.ObservationConvention |
| 47 | + */ |
| 48 | +public class WebServiceObservationInterceptor extends ClientInterceptorAdapter { |
| 49 | + |
| 50 | + private static final WarnThenDebugLogger WARN_THEN_DEBUG_LOGGER = new WarnThenDebugLogger(WebServiceObservationInterceptor.class); |
| 51 | + private static final String OBSERVATION_KEY = "observation"; |
| 52 | + private static final WebServiceTemplateConvention DEFAULT_CONVENTION = new DefaultWebServiceTemplateConvention(); |
| 53 | + |
| 54 | + private final ObservationRegistry observationRegistry; |
| 55 | + |
| 56 | + private final WebServiceTemplateConvention customConvention; |
| 57 | + private final ObservationHelper observationHelper; |
| 58 | + |
| 59 | + public WebServiceObservationInterceptor( |
| 60 | + @NonNull |
| 61 | + ObservationRegistry observationRegistry, |
| 62 | + @NonNull |
| 63 | + ObservationHelper observationHelper, |
| 64 | + @Nullable |
| 65 | + WebServiceTemplateConvention customConvention) { |
| 66 | + |
| 67 | + this.observationRegistry = observationRegistry; |
| 68 | + this.observationHelper = observationHelper; |
| 69 | + this.customConvention = customConvention; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException { |
| 75 | + |
| 76 | + TransportContext transportContext = TransportContextHolder.getTransportContext(); |
| 77 | + HeadersAwareSenderWebServiceConnection connection = |
| 78 | + (HeadersAwareSenderWebServiceConnection) transportContext.getConnection(); |
| 79 | + |
| 80 | + Observation observation = WebServiceTemplateObservationDocumentation.WEB_SERVICE_TEMPLATE.start( |
| 81 | + customConvention, |
| 82 | + DEFAULT_CONVENTION, |
| 83 | + () -> new WebServiceTemplateObservationContext(connection), |
| 84 | + observationRegistry); |
| 85 | + |
| 86 | + messageContext.setProperty(OBSERVATION_KEY, observation); |
| 87 | + |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void afterCompletion(MessageContext messageContext, Exception ex) { |
| 93 | + |
| 94 | + Observation observation = (Observation) messageContext.getProperty(OBSERVATION_KEY); |
| 95 | + if (observation == null) { |
| 96 | + WARN_THEN_DEBUG_LOGGER.log("Missing expected Observation in messageContext; the request will not be observed."); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + WebServiceTemplateObservationContext context = (WebServiceTemplateObservationContext) observation.getContext(); |
| 101 | + |
| 102 | + WebServiceMessage request = messageContext.getRequest(); |
| 103 | + WebServiceMessage response = messageContext.getResponse(); |
| 104 | + |
| 105 | + if (request instanceof SoapMessage soapMessage) { |
| 106 | + |
| 107 | + Source source = soapMessage.getSoapBody().getPayloadSource(); |
| 108 | + QName root = observationHelper.getRootElement(source); |
| 109 | + if (root != null) { |
| 110 | + context.setLocalPart(root.getLocalPart()); |
| 111 | + context.setNamespace(root.getNamespaceURI()); |
| 112 | + } |
| 113 | + if (soapMessage.getSoapAction() != null && !soapMessage.getSoapAction().equals(TransportConstants.EMPTY_SOAP_ACTION)) { |
| 114 | + context.setSoapAction(soapMessage.getSoapAction()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (ex == null) { |
| 119 | + context.setOutcome("success"); |
| 120 | + } else { |
| 121 | + context.setError(ex); |
| 122 | + context.setOutcome("fault"); |
| 123 | + } |
| 124 | + |
| 125 | + if (response instanceof FaultAwareWebServiceMessage faultAwareResponse) { |
| 126 | + if (faultAwareResponse.hasFault()) { |
| 127 | + context.setOutcome("fault"); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + URI uri = getUriFromConnection(); |
| 132 | + if (uri != null) { |
| 133 | + context.setHost(uri.getHost()); |
| 134 | + context.setPath(uri.getPath()); |
| 135 | + } |
| 136 | + |
| 137 | + context.setContextualName("POST"); |
| 138 | + |
| 139 | + observation.stop(); |
| 140 | + } |
| 141 | + |
| 142 | + URI getUriFromConnection() { |
| 143 | + TransportContext transportContext = TransportContextHolder.getTransportContext(); |
| 144 | + WebServiceConnection connection = transportContext.getConnection(); |
| 145 | + try { |
| 146 | + return connection.getUri(); |
| 147 | + } catch (URISyntaxException e) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
0 commit comments