001package ca.uhn.fhir.rest.client.method; 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.context.ConfigurationException; 024import ca.uhn.fhir.context.FhirContext; 025import ca.uhn.fhir.model.api.annotation.Description; 026import ca.uhn.fhir.model.valueset.BundleTypeEnum; 027import ca.uhn.fhir.rest.annotation.Operation; 028import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 029import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; 030import ca.uhn.fhir.rest.param.ParameterUtil; 031import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 032import ca.uhn.fhir.util.FhirTerser; 033import ca.uhn.fhir.util.ParametersUtil; 034import org.hl7.fhir.instance.model.api.*; 035 036import java.lang.reflect.Method; 037import java.lang.reflect.Modifier; 038import java.util.ArrayList; 039import java.util.LinkedHashMap; 040import java.util.List; 041import java.util.Map; 042 043import static org.apache.commons.lang3.StringUtils.isBlank; 044import static org.apache.commons.lang3.StringUtils.isNotBlank; 045 046public class OperationMethodBinding extends BaseResourceReturningMethodBinding { 047 048 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OperationMethodBinding.class); 049 private final boolean myIdempotent; 050 private final Integer myIdParamIndex; 051 private final String myName; 052 private final RestOperationTypeEnum myOtherOperationType; 053 private final ReturnTypeEnum myReturnType; 054 private BundleTypeEnum myBundleType; 055 private String myDescription; 056 057 protected OperationMethodBinding(Class<?> theReturnResourceType, Class<? extends IBaseResource> theReturnTypeFromRp, Method theMethod, FhirContext theContext, Object theProvider, 058 boolean theIdempotent, String theOperationName, Class<? extends IBaseResource> theOperationType, 059 BundleTypeEnum theBundleType) { 060 super(theReturnResourceType, theMethod, theContext, theProvider); 061 062 myBundleType = theBundleType; 063 myIdempotent = theIdempotent; 064 myIdParamIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext()); 065 066 Description description = theMethod.getAnnotation(Description.class); 067 if (description != null) { 068 myDescription = ParametersUtil.extractDescription(description); 069 } 070 if (isBlank(myDescription)) { 071 myDescription = null; 072 } 073 074 if (isBlank(theOperationName)) { 075 throw new ConfigurationException("Method '" + theMethod.getName() + "' on type " + theMethod.getDeclaringClass().getName() + " is annotated with @" + Operation.class.getSimpleName() 076 + " but this annotation has no name defined"); 077 } 078 if (theOperationName.startsWith("$") == false) { 079 theOperationName = "$" + theOperationName; 080 } 081 myName = theOperationName; 082 083 if (theReturnTypeFromRp != null) { 084 setResourceName(theContext.getResourceType(theReturnTypeFromRp)); 085 } else { 086 if (Modifier.isAbstract(theOperationType.getModifiers()) == false) { 087 setResourceName(theContext.getResourceType(theOperationType)); 088 } else { 089 setResourceName(null); 090 } 091 } 092 093 myReturnType = ReturnTypeEnum.RESOURCE; 094 095 if (getResourceName() == null) { 096 myOtherOperationType = RestOperationTypeEnum.EXTENDED_OPERATION_SERVER; 097 } else if (myIdParamIndex == null) { 098 myOtherOperationType = RestOperationTypeEnum.EXTENDED_OPERATION_TYPE; 099 } else { 100 myOtherOperationType = RestOperationTypeEnum.EXTENDED_OPERATION_INSTANCE; 101 } 102 103 } 104 105 public OperationMethodBinding(Class<?> theReturnResourceType, Class<? extends IBaseResource> theReturnTypeFromRp, Method theMethod, FhirContext theContext, Object theProvider, 106 Operation theAnnotation) { 107 this(theReturnResourceType, theReturnTypeFromRp, theMethod, theContext, theProvider, theAnnotation.idempotent(), theAnnotation.name(), theAnnotation.type(), theAnnotation.bundleType()); 108 } 109 110 public String getDescription() { 111 return myDescription; 112 } 113 114 public void setDescription(String theDescription) { 115 myDescription = theDescription; 116 } 117 118 /** 119 * Returns the name of the operation, starting with "$" 120 */ 121 public String getName() { 122 return myName; 123 } 124 125 @Override 126 protected BundleTypeEnum getResponseBundleType() { 127 return myBundleType; 128 } 129 130 @Override 131 public RestOperationTypeEnum getRestOperationType() { 132 return myOtherOperationType; 133 } 134 135 @Override 136 public ReturnTypeEnum getReturnType() { 137 return myReturnType; 138 } 139 140 @Override 141 public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { 142 String id = null; 143 if (myIdParamIndex != null) { 144 IIdType idDt = (IIdType) theArgs[myIdParamIndex]; 145 id = idDt.getValue(); 146 } 147 IBaseParameters parameters = (IBaseParameters) getContext().getResourceDefinition("Parameters").newInstance(); 148 149 if (theArgs != null) { 150 for (int idx = 0; idx < theArgs.length; idx++) { 151 IParameter nextParam = getParameters().get(idx); 152 nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, parameters); 153 } 154 } 155 156 return createOperationInvocation(getContext(), getResourceName(), id, null, myName, parameters, false); 157 } 158 159 public boolean isIdempotent() { 160 return myIdempotent; 161 } 162 163 public static BaseHttpClientInvocation createOperationInvocation(FhirContext theContext, String theResourceName, String theId, String theVersion, String theOperationName, IBaseParameters theInput, 164 boolean theUseHttpGet) { 165 StringBuilder b = new StringBuilder(); 166 if (theResourceName != null) { 167 b.append(theResourceName); 168 if (isNotBlank(theId)) { 169 b.append('/'); 170 b.append(theId); 171 if (isNotBlank(theVersion)) { 172 b.append("/_history/"); 173 b.append(theVersion); 174 } 175 } 176 } 177 if (b.length() > 0) { 178 b.append('/'); 179 } 180 if (!theOperationName.startsWith("$")) { 181 b.append("$"); 182 } 183 b.append(theOperationName); 184 185 if (!theUseHttpGet) { 186 return new HttpPostClientInvocation(theContext, theInput, b.toString()); 187 } 188 FhirTerser t = theContext.newTerser(); 189 List<IBase> parameters = t.getValues(theInput, "Parameters.parameter"); 190 191 Map<String, List<String>> params = new LinkedHashMap<>(); 192 for (Object nextParameter : parameters) { 193 IPrimitiveType<?> nextNameDt = (IPrimitiveType<?>) t.getSingleValueOrNull((IBase) nextParameter, "name"); 194 if (nextNameDt == null || nextNameDt.isEmpty()) { 195 ourLog.warn("Ignoring input parameter with no value in Parameters.parameter.name in operation client invocation"); 196 continue; 197 } 198 String nextName = nextNameDt.getValueAsString(); 199 if (!params.containsKey(nextName)) { 200 params.put(nextName, new ArrayList<>()); 201 } 202 203 IBaseDatatype value = (IBaseDatatype) t.getSingleValueOrNull((IBase) nextParameter, "value[x]"); 204 if (value == null) { 205 continue; 206 } 207 if (!(value instanceof IPrimitiveType)) { 208 throw new IllegalArgumentException( 209 "Can not invoke operation as HTTP GET when it has parameters with a composite (non priitive) datatype as the value. Found value: " + value.getClass().getName()); 210 } 211 IPrimitiveType<?> primitive = (IPrimitiveType<?>) value; 212 params.get(nextName).add(primitive.getValueAsString()); 213 } 214 return new HttpGetClientInvocation(theContext, params, b.toString()); 215 } 216 217 public static BaseHttpClientInvocation createProcessMsgInvocation(FhirContext theContext, String theOperationName, IBaseBundle theInput, Map<String, List<String>> urlParams) { 218 StringBuilder b = new StringBuilder(); 219 220 if (b.length() > 0) { 221 b.append('/'); 222 } 223 if (!theOperationName.startsWith("$")) { 224 b.append("$"); 225 } 226 b.append(theOperationName); 227 228 BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(urlParams, b, b.indexOf("?") == -1); 229 230 return new HttpPostClientInvocation(theContext, theInput, b.toString()); 231 232 } 233 234}