001package ca.uhn.fhir.rest.client.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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.rest.client.api.IClientInterceptor;
024import ca.uhn.fhir.rest.client.api.IHttpRequest;
025import ca.uhn.fhir.rest.client.api.IHttpResponse;
026import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
027import org.apache.http.HttpEntity;
028import org.apache.http.HttpResponse;
029
030import java.io.IOException;
031
032/**
033 * Client interceptor which simply captures request and response objects and stores them so that they can be inspected after a client
034 * call has returned
035 *
036 * @see ThreadLocalCapturingInterceptor for an interceptor that uses a ThreadLocal in order to work in multithreaded environments
037 */
038public class CapturingInterceptor implements IClientInterceptor {
039
040        private IHttpRequest myLastRequest;
041        private IHttpResponse myLastResponse;
042
043        /**
044         * Clear the last request and response values
045         */
046        public void clear() {
047                myLastRequest = null;
048                myLastResponse = null;
049        }
050
051        public IHttpRequest getLastRequest() {
052                return myLastRequest;
053        }
054
055        public IHttpResponse getLastResponse() {
056                return myLastResponse;
057        }
058
059        @Override
060        public void interceptRequest(IHttpRequest theRequest) {
061                myLastRequest = theRequest;
062        }
063
064        @Override
065        public void interceptResponse(IHttpResponse theResponse) {
066                //Buffer the reponse to avoid errors when content has already been read and the entity is not repeatable
067                bufferResponse(theResponse);
068
069                myLastResponse = theResponse;
070        }
071
072        static void bufferResponse(IHttpResponse theResponse) {
073                try {
074                        if (theResponse.getResponse() instanceof HttpResponse) {
075                                HttpEntity entity = ((HttpResponse) theResponse.getResponse()).getEntity();
076                                if (entity != null && !entity.isRepeatable()) {
077                                        theResponse.bufferEntity();
078                                }
079                        } else {
080                                theResponse.bufferEntity();
081                        }
082                } catch (IOException e) {
083                        throw new InternalErrorException("Unable to buffer the entity for capturing", e);
084                }
085        }
086
087}