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.model.primitive.IntegerDt;
025import ca.uhn.fhir.parser.DataFormatException;
026import ca.uhn.fhir.rest.annotation.Count;
027import ca.uhn.fhir.rest.api.Constants;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.param.ParameterUtil;
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;
036
037public class CountParameter implements IParameter {
038
039        private Class<?> myType;
040
041        @Override
042        public Object translateQueryParametersIntoServerArgument(
043                        RequestDetails theRequest, BaseMethodBinding theMethodBinding)
044                        throws InternalErrorException, InvalidRequestException {
045                String[] countParam = theRequest.getParameters().get(Constants.PARAM_COUNT);
046                if (countParam != null) {
047                        if (countParam.length > 0) {
048                                if (StringUtils.isNotBlank(countParam[0])) {
049                                        try {
050                                                IntegerDt count = new IntegerDt(countParam[0]);
051                                                return ParameterUtil.fromInteger(myType, count);
052                                        } catch (DataFormatException e) {
053                                                throw new InvalidRequestException(
054                                                                Msg.code(375) + "Invalid " + Constants.PARAM_COUNT + " value: " + countParam[0]);
055                                        }
056                                }
057                        }
058                }
059                return null;
060        }
061
062        @Override
063        public void initializeTypes(
064                        Method theMethod,
065                        Class<? extends Collection<?>> theOuterCollectionType,
066                        Class<? extends Collection<?>> theInnerCollectionType,
067                        Class<?> theParameterType) {
068                if (theOuterCollectionType != null) {
069                        throw new ConfigurationException(Msg.code(376) + "Method '" + theMethod.getName() + "' in type '"
070                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Count.class.getName()
071                                        + " but can not be of collection type");
072                }
073                if (!ParameterUtil.isBindableIntegerType(theParameterType)) {
074                        throw new ConfigurationException(Msg.code(377) + "Method '" + theMethod.getName() + "' in type '"
075                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + Count.class.getName()
076                                        + " but type '" + theParameterType + "' is an invalid type, must be one of Integer or IntegerType");
077                }
078                myType = theParameterType;
079        }
080}