001package ca.uhn.fhir.rest.method;
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.Arrays;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.lang3.Validate;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032
033import ca.uhn.fhir.context.FhirContext;
034import ca.uhn.fhir.rest.annotation.RawParam;
035import ca.uhn.fhir.rest.method.SearchMethodBinding.QualifierDetails;
036import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
037import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
038
039public class RawParamsParmeter implements IParameter {
040
041        private final List<IParameter> myAllMethodParameters;
042
043        public RawParamsParmeter(List<IParameter> theParameters) {
044                myAllMethodParameters = theParameters;
045        }
046
047        @Override
048        public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map<String, List<String>> theTargetQueryArguments, IBaseResource theTargetResource)
049                        throws InternalErrorException {
050                // not supported on client for now
051        }
052
053        @Override
054        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
055                HashMap<String, List<String>> retVal = null;
056
057                for (String nextName : theRequest.getParameters().keySet()) {
058                        if (nextName.startsWith("_")) {
059                                continue;
060                        }
061                        
062                        QualifierDetails qualifiers = SearchMethodBinding.extractQualifiersFromParameterName(nextName);
063                        
064                        boolean alreadyCaptured = false;
065                        for (IParameter nextParameter : myAllMethodParameters) {
066                                if (nextParameter instanceof SearchParameter) {
067                                        SearchParameter nextSearchParam = (SearchParameter)nextParameter;
068                                        if (nextSearchParam.getName().equals(qualifiers.getParamName())) {
069                                                if (qualifiers.passes(nextSearchParam.getQualifierWhitelist(), nextSearchParam.getQualifierBlacklist())) {
070                                                        alreadyCaptured = true;
071                                                        break;
072                                                }
073                                        }
074                                }
075                        }
076                        
077                        if (!alreadyCaptured) {
078                                if (retVal == null) {
079                                        retVal = new HashMap<String, List<String>>();
080                                }
081                                retVal.put(nextName, Arrays.asList(theRequest.getParameters().get(nextName)));
082                        }
083                        
084                }
085                
086                return retVal;
087        }
088
089        @Override
090        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
091                Validate.isTrue(theParameterType.equals(Map.class), "Parameter with @" + RawParam.class + " must be of type Map<String, List<String>>");
092        } 
093
094}