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