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