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