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.Offset;
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 OffsetParameter 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[] sinceParams = theRequest.getParameters().get(Constants.PARAM_OFFSET);
046                if (sinceParams != null) {
047                        if (sinceParams.length > 0) {
048                                if (StringUtils.isNotBlank(sinceParams[0])) {
049                                        try {
050                                                IntegerDt since = new IntegerDt(sinceParams[0]);
051                                                return ParameterUtil.fromInteger(myType, since);
052                                        } catch (DataFormatException e) {
053                                                throw new InvalidRequestException(
054                                                                Msg.code(461) + "Invalid " + Constants.PARAM_OFFSET + " value: " + sinceParams[0]);
055                                        }
056                                }
057                        }
058                }
059                return ParameterUtil.fromInteger(myType, 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(462) + "Method '" + theMethod.getName() + "' in type '"
070                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @"
071                                        + Offset.class.getName() + " but can not be of collection type");
072                }
073                if (!ParameterUtil.isBindableIntegerType(theParameterType)) {
074                        throw new ConfigurationException(Msg.code(463) + "Method '" + theMethod.getName() + "' in type '"
075                                        + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @"
076                                        + Offset.class.getName() + " but type '" + theParameterType
077                                        + "' is an invalid type, must be one of Integer or IntegerType");
078                }
079                myType = theParameterType;
080        }
081}