001package ca.uhn.fhir.rest.client; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2017 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.lang.reflect.Method; 024import java.util.HashMap; 025import java.util.Map; 026 027import org.hl7.fhir.instance.model.api.IBaseResource; 028 029import ca.uhn.fhir.context.ConfigurationException; 030import ca.uhn.fhir.context.FhirContext; 031import ca.uhn.fhir.rest.api.SummaryEnum; 032import ca.uhn.fhir.rest.client.api.IHttpClient; 033import ca.uhn.fhir.rest.client.api.IRestfulClient; 034import ca.uhn.fhir.rest.method.BaseMethodBinding; 035import ca.uhn.fhir.rest.server.EncodingEnum; 036 037public class ClientInvocationHandlerFactory { 038 039 private final Map<Method, BaseMethodBinding<?>> myBindings = new HashMap<Method, BaseMethodBinding<?>>(); 040 private final IHttpClient myClient; 041 private final FhirContext myContext; 042 private final Map<Method, ILambda> myMethodToLambda = new HashMap<Method, ILambda>(); 043 private final Map<Method, Object> myMethodToReturnValue = new HashMap<Method, Object>(); 044 private final String myUrlBase; 045 046 public ClientInvocationHandlerFactory(IHttpClient theClient, FhirContext theContext, String theUrlBase, Class<? extends IRestfulClient> theClientType) { 047 myClient = theClient; 048 myUrlBase = theUrlBase; 049 myContext = theContext; 050 051 try { 052 myMethodToReturnValue.put(theClientType.getMethod("getFhirContext"), theContext); 053 myMethodToReturnValue.put(theClientType.getMethod("getHttpClient"), theClient); 054 myMethodToReturnValue.put(theClientType.getMethod("getServerBase"), theUrlBase); 055 056 myMethodToLambda.put(theClientType.getMethod("setEncoding", EncodingEnum.class), new SetEncodingLambda()); 057 myMethodToLambda.put(theClientType.getMethod("setPrettyPrint", Boolean.class), new SetPrettyPrintLambda()); 058 myMethodToLambda.put(theClientType.getMethod("registerInterceptor", IClientInterceptor.class), new RegisterInterceptorLambda()); 059 myMethodToLambda.put(theClientType.getMethod("unregisterInterceptor", IClientInterceptor.class), new UnregisterInterceptorLambda()); 060 myMethodToLambda.put(theClientType.getMethod("setSummary", SummaryEnum.class), new SetSummaryLambda()); 061 myMethodToLambda.put(theClientType.getMethod("fetchResourceFromUrl", Class.class, String.class), new FetchResourceFromUrlLambda()); 062 063 } catch (NoSuchMethodException e) { 064 throw new ConfigurationException("Failed to find methods on client. This is a HAPI bug!", e); 065 } catch (SecurityException e) { 066 throw new ConfigurationException("Failed to find methods on client. This is a HAPI bug!", e); 067 } 068 } 069 070 public void addBinding(Method theMethod, BaseMethodBinding<?> theBinding) { 071 myBindings.put(theMethod, theBinding); 072 } 073 074 ClientInvocationHandler newInvocationHandler(RestfulClientFactory theRestfulClientFactory) { 075 return new ClientInvocationHandler(myClient, myContext, myUrlBase, myMethodToReturnValue, myBindings, myMethodToLambda, theRestfulClientFactory); 076 } 077 078 public interface ILambda { 079 Object handle(ClientInvocationHandler theTarget, Object[] theArgs); 080 } 081 082 class RegisterInterceptorLambda implements ILambda { 083 @Override 084 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 085 IClientInterceptor interceptor = (IClientInterceptor) theArgs[0]; 086 theTarget.registerInterceptor(interceptor); 087 return null; 088 } 089 } 090 091 class FetchResourceFromUrlLambda implements ILambda { 092 @Override 093 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 094 @SuppressWarnings("unchecked") 095 Class<? extends IBaseResource> type = (Class<? extends IBaseResource>) theArgs[0]; 096 String url = (String) theArgs[1]; 097 098 return theTarget.fetchResourceFromUrl(type, url); 099 } 100 } 101 102 class SetEncodingLambda implements ILambda { 103 @Override 104 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 105 EncodingEnum encoding = (EncodingEnum) theArgs[0]; 106 theTarget.setEncoding(encoding); 107 return null; 108 } 109 } 110 111 class SetPrettyPrintLambda implements ILambda { 112 @Override 113 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 114 Boolean prettyPrint = (Boolean) theArgs[0]; 115 theTarget.setPrettyPrint(prettyPrint); 116 return null; 117 } 118 } 119 120 class SetSummaryLambda implements ILambda { 121 @Override 122 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 123 SummaryEnum encoding = (SummaryEnum) theArgs[0]; 124 theTarget.setSummary(encoding); 125 return null; 126 } 127 } 128 129 class UnregisterInterceptorLambda implements ILambda { 130 @Override 131 public Object handle(ClientInvocationHandler theTarget, Object[] theArgs) { 132 IClientInterceptor interceptor = (IClientInterceptor) theArgs[0]; 133 theTarget.unregisterInterceptor(interceptor); 134 return null; 135 } 136 } 137 138}