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