001/*
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.method;
021
022import ca.uhn.fhir.context.ConfigurationException;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.rest.annotation.Count;
025import ca.uhn.fhir.rest.api.Constants;
026import ca.uhn.fhir.rest.api.server.RequestDetails;
027import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
028import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
029
030import java.lang.reflect.Method;
031import java.util.Collection;
032
033public class GraphQLQueryUrlParameter implements IParameter {
034
035        private Class<?> myType;
036
037        @Override
038        public Object translateQueryParametersIntoServerArgument(
039                        RequestDetails theRequest, BaseMethodBinding theMethodBinding)
040                        throws InternalErrorException, InvalidRequestException {
041                String[] queryParams = theRequest.getParameters().get(Constants.PARAM_GRAPHQL_QUERY);
042                String retVal = null;
043                if (queryParams != null) {
044                        if (queryParams.length > 0) {
045                                retVal = queryParams[0];
046                        }
047                }
048                return retVal;
049        }
050
051        @Override
052        public void initializeTypes(
053                        Method theMethod,
054                        Class<? extends Collection<?>> theOuterCollectionType,
055                        Class<? extends Collection<?>> theInnerCollectionType,
056                        Class<?> theParameterType) {
057                if (theOuterCollectionType != null) {
058                        throw new ConfigurationException(Msg.code(459) + "Method '" + theMethod.getName() + "' in type '"
059                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Count.class.getName()
060                                        + " but can not be of collection type");
061                }
062                if (!String.class.equals(theParameterType)) {
063                        throw new ConfigurationException(Msg.code(460) + "Method '" + theMethod.getName() + "' in type '"
064                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Count.class.getName()
065                                        + " but type '" + theParameterType + "' is an invalid type, must be one of Integer or IntegerType");
066                }
067                myType = theParameterType;
068        }
069}