001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 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 java.lang.reflect.Method;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.lang3.Validate;
031
032import ca.uhn.fhir.rest.annotation.RawParam;
033import ca.uhn.fhir.rest.api.server.RequestDetails;
034import ca.uhn.fhir.rest.param.QualifierDetails;
035import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
036import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
037
038public class RawParamsParameter implements IParameter {
039
040        private final List<IParameter> myAllMethodParameters;
041
042        public RawParamsParameter(List<IParameter> theParameters) {
043                myAllMethodParameters = theParameters;
044        }
045
046        
047        @Override
048        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
049                HashMap<String, List<String>> retVal = null;
050
051                for (String nextName : theRequest.getParameters().keySet()) {
052                        if (nextName.startsWith("_")) {
053                                continue;
054                        }
055                        
056                        QualifierDetails qualifiers = QualifierDetails.extractQualifiersFromParameterName(nextName);
057                        
058                        boolean alreadyCaptured = false;
059                        for (IParameter nextParameter : myAllMethodParameters) {
060                                if (nextParameter instanceof SearchParameter) {
061                                        SearchParameter nextSearchParam = (SearchParameter)nextParameter;
062                                        if (nextSearchParam.getName().equals(qualifiers.getParamName())) {
063                                                if (qualifiers.passes(nextSearchParam.getQualifierWhitelist(), nextSearchParam.getQualifierBlacklist())) {
064                                                        alreadyCaptured = true;
065                                                        break;
066                                                }
067                                        }
068                                }
069                        }
070                        
071                        if (!alreadyCaptured) {
072                                if (retVal == null) {
073                                        retVal = new HashMap<>();
074                                }
075                                retVal.put(nextName, Arrays.asList(theRequest.getParameters().get(nextName)));
076                        }
077                        
078                }
079                
080                return retVal;
081        }
082
083        @Override
084        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
085                Validate.isTrue(theParameterType.equals(Map.class), "Parameter with @" + RawParam.class + " must be of type Map<String, List<String>>");
086        } 
087
088}