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