001/*- 002 * #%L 003 * HAPI FHIR - Client Framework 004 * %% 005 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.client.interceptor; 021 022import ca.uhn.fhir.i18n.Msg; 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(Msg.code(1404) + "Unable to buffer the entity for capturing", e); 087 } 088 } 089}