001/*
002 * #%L
003 * HAPI FHIR JAX-RS Server
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.jaxrs.server;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.api.BundleInclusionRule;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsExceptionInterceptor;
026import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsResponseException;
027import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest;
028import ca.uhn.fhir.rest.api.server.IRestfulServer;
029import ca.uhn.fhir.rest.api.*;
030import ca.uhn.fhir.rest.server.IPagingProvider;
031import ca.uhn.fhir.rest.server.PageProvider;
032import ca.uhn.fhir.rest.server.method.PageMethodBinding;
033import jakarta.interceptor.Interceptors;
034import jakarta.ws.rs.GET;
035import jakarta.ws.rs.Produces;
036import jakarta.ws.rs.QueryParam;
037import jakarta.ws.rs.core.MediaType;
038import jakarta.ws.rs.core.Response;
039
040import java.io.IOException;
041
042/**
043 * Base class for a provider to provide the <code>[baseUrl]?_getpages=foo</code> request, which is a request to the
044 * server to retrieve the next page of a set of paged results.
045 */
046@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
047@Interceptors(JaxRsExceptionInterceptor.class)
048public abstract class AbstractJaxRsPageProvider extends AbstractJaxRsProvider implements IRestfulServer<JaxRsRequest> {
049
050        private PageMethodBinding myBinding;
051
052        /**
053         * The default constructor.
054         */
055        protected AbstractJaxRsPageProvider() {
056                try {
057                        myBinding = new PageMethodBinding(getFhirContext(), PageProvider.class.getMethod("getPage"));
058                } catch (Exception e) {
059                        throw new ca.uhn.fhir.context.ConfigurationException(Msg.code(1983), e);
060                }
061        }
062
063        /**
064         * Provides the ability to set the {@link FhirContext} instance.
065         * @param ctx the {@link FhirContext} instance.
066         */
067        protected AbstractJaxRsPageProvider(FhirContext ctx) {
068                super(ctx);
069                try {
070                        myBinding = new PageMethodBinding(getFhirContext(), PageProvider.class.getMethod("getPage"));
071                } catch (Exception e) {
072                        throw new ca.uhn.fhir.context.ConfigurationException(Msg.code(1984), e);
073                }
074        }
075
076        @Override
077        public String getBaseForRequest() {
078                try {
079                        return getUriInfo().getBaseUri().toURL().toExternalForm();
080                } catch (Exception e) {
081                        // cannot happen
082                        return null;
083                }
084        }
085
086        /**
087         * This method implements the "getpages" action
088         */
089        @GET
090        public Response getPages(@QueryParam(Constants.PARAM_PAGINGACTION) String thePageId) throws IOException {
091                JaxRsRequest theRequest =
092                                getRequest(RequestTypeEnum.GET, RestOperationTypeEnum.GET_PAGE).build();
093                try {
094                        return (Response) myBinding.invokeServer(this, theRequest);
095                } catch (JaxRsResponseException theException) {
096                        return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, theException);
097                }
098        }
099
100        /**
101         * Default: no paging provider
102         */
103        @Override
104        public IPagingProvider getPagingProvider() {
105                return null;
106        }
107
108        /**
109         * Default: BundleInclusionRule.BASED_ON_INCLUDES
110         */
111        @Override
112        public BundleInclusionRule getBundleInclusionRule() {
113                return BundleInclusionRule.BASED_ON_INCLUDES;
114        }
115
116        @Override
117        public PreferReturnEnum getDefaultPreferReturn() {
118                return PreferReturnEnum.REPRESENTATION;
119        }
120}