001package ca.uhn.fhir.rest.server.method; 002 003/* 004 * #%L 005 * HAPI FHIR - Server 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.context.ConfigurationException; 025import ca.uhn.fhir.rest.api.Constants; 026import ca.uhn.fhir.rest.api.SummaryEnum; 027import ca.uhn.fhir.rest.api.server.RequestDetails; 028import ca.uhn.fhir.rest.param.binder.CollectionBinder; 029import ca.uhn.fhir.rest.server.ElementsSupportEnum; 030import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 031import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 032import org.apache.commons.lang3.StringUtils; 033 034import java.lang.reflect.Method; 035import java.util.Collection; 036import java.util.HashSet; 037import java.util.Set; 038import java.util.StringTokenizer; 039 040import static org.apache.commons.lang3.StringUtils.isNotBlank; 041 042public class ElementsParameter implements IParameter { 043 044 @SuppressWarnings("rawtypes") 045 private Class<? extends Collection> myInnerCollectionType; 046 047 @Override 048 @SuppressWarnings({"rawtypes", "unchecked"}) 049 public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException { 050 Set<String> value = getElementsValueOrNull(theRequest, false); 051 if (value == null || value.isEmpty()) { 052 return null; 053 } 054 055 if (myInnerCollectionType == null) { 056 return StringUtils.join(value, ','); 057 } 058 059 try { 060 Collection retVal = myInnerCollectionType.newInstance(); 061 retVal.addAll(value); 062 return retVal; 063 } catch (InstantiationException e) { 064 throw new InternalErrorException(Msg.code(413) + "Failed to instantiate " + myInnerCollectionType, e); 065 } catch (IllegalAccessException e) { 066 throw new InternalErrorException(Msg.code(414) + "Failed to instantiate " + myInnerCollectionType, e); 067 } 068 } 069 070 @Override 071 public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) { 072 if (theOuterCollectionType != null) { 073 throw new ConfigurationException(Msg.code(415) + "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is of type " + SummaryEnum.class 074 + " but can not be a collection of collections"); 075 } 076 if (theInnerCollectionType != null) { 077 myInnerCollectionType = CollectionBinder.getInstantiableCollectionType(theInnerCollectionType, SummaryEnum.class.getSimpleName()); 078 } 079 } 080 081 public static Set<String> getElementsValueOrNull(RequestDetails theRequest, boolean theExclude) { 082 boolean standardMode = theRequest.getServer().getElementsSupport() != ElementsSupportEnum.EXTENDED; 083 if (theExclude && standardMode) { 084 return null; 085 } 086 087 String paramName = Constants.PARAM_ELEMENTS; 088 if (theExclude) { 089 paramName = Constants.PARAM_ELEMENTS + Constants.PARAM_ELEMENTS_EXCLUDE_MODIFIER; 090 } 091 String[] elementsValues = theRequest.getParameters().get(paramName); 092 093 if (elementsValues != null && elementsValues.length > 0) { 094 Set<String> retVal = new HashSet<>(); 095 for (String next : elementsValues) { 096 StringTokenizer tok = new StringTokenizer(next, ","); 097 while (tok.hasMoreTokens()) { 098 String token = tok.nextToken(); 099 if (isNotBlank(token)) { 100 if (token.contains(".")) 101 if (standardMode) { 102 continue; 103 } 104 retVal.add(token); 105 } 106 } 107 } 108 if (retVal.isEmpty()) { 109 return null; 110 } 111 112 return retVal; 113 } 114 return null; 115 } 116 117}