001package ca.uhn.fhir.rest.client.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.interceptor.api.Hook;
024import ca.uhn.fhir.interceptor.api.Interceptor;
025import ca.uhn.fhir.interceptor.api.Pointcut;
026import ca.uhn.fhir.rest.client.api.IHttpRequest;
027import ca.uhn.fhir.rest.client.api.IHttpResponse;
028import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
029import org.apache.http.HttpEntity;
030import org.apache.http.HttpResponse;
031
032import java.io.IOException;
033
034/**
035 * Client interceptor which simply captures request and response objects and stores them so that they can be inspected after a client
036 * call has returned
037 *
038 * @see ThreadLocalCapturingInterceptor for an interceptor that uses a ThreadLocal in order to work in multithreaded environments
039 */
040@Interceptor
041public class CapturingInterceptor {
042
043        private IHttpRequest myLastRequest;
044        private IHttpResponse myLastResponse;
045
046        /**
047         * Clear the last request and response values
048         */
049        public void clear() {
050                myLastRequest = null;
051                myLastResponse = null;
052        }
053
054        public IHttpRequest getLastRequest() {
055                return myLastRequest;
056        }
057
058        public IHttpResponse getLastResponse() {
059                return myLastResponse;
060        }
061
062        @Hook(value = Pointcut.CLIENT_REQUEST, order = InterceptorOrders.CAPTURING_INTERCEPTOR_REQUEST)
063        public void interceptRequest(IHttpRequest theRequest) {
064                myLastRequest = theRequest;
065        }
066
067        @Hook(value = Pointcut.CLIENT_RESPONSE, order = InterceptorOrders.CAPTURING_INTERCEPTOR_RESPONSE)
068        public void interceptResponse(IHttpResponse theResponse) {
069                //Buffer the reponse to avoid errors when content has already been read and the entity is not repeatable
070                bufferResponse(theResponse);
071
072                myLastResponse = theResponse;
073        }
074
075        static void bufferResponse(IHttpResponse theResponse) {
076                try {
077                        if (theResponse.getResponse() instanceof HttpResponse) {
078                                HttpEntity entity = ((HttpResponse) theResponse.getResponse()).getEntity();
079                                if (entity != null && !entity.isRepeatable()) {
080                                        theResponse.bufferEntity();
081                                }
082                        } else {
083                                theResponse.bufferEntity();
084                        }
085                } catch (IOException e) {
086                        throw new InternalErrorException("Unable to buffer the entity for capturing", e);
087                }
088        }
089
090}