001package org.hl7.fhir.r4.hapi.rest.server; 002 003/* 004 * #%L 005 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0) 006 * %% 007 * Copyright (C) 2014 - 2015 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 */ 022import java.util.*; 023 024import javax.servlet.http.HttpServletRequest; 025 026import ca.uhn.fhir.rest.api.server.RequestDetails; 027import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; 028import org.hl7.fhir.instance.model.api.IBaseResource; 029import org.hl7.fhir.r4.model.IdType; 030import org.hl7.fhir.r4.model.StructureDefinition; 031 032import ca.uhn.fhir.context.FhirContext; 033import ca.uhn.fhir.context.RuntimeResourceDefinition; 034import ca.uhn.fhir.rest.annotation.*; 035import ca.uhn.fhir.rest.server.IResourceProvider; 036import ca.uhn.fhir.rest.server.RestfulServer; 037 038public class ServerProfileProvider implements IResourceProvider { 039 040 private final FhirContext myContext; 041 private final RestfulServer myRestfulServer; 042 043 public ServerProfileProvider(RestfulServer theServer) { 044 myContext = theServer.getFhirContext(); 045 myRestfulServer = theServer; 046 } 047 048 @Override 049 public Class<? extends IBaseResource> getResourceType() { 050 return StructureDefinition.class; 051 } 052 053 @Read() 054 public StructureDefinition getProfileById(ServletRequestDetails theRequest, @IdParam IdType theId) { 055 RuntimeResourceDefinition retVal = myContext.getResourceDefinitionById(theId.getIdPart()); 056 if (retVal==null) { 057 return null; 058 } 059 String serverBase = getServerBase(theRequest); 060 return (StructureDefinition) retVal.toProfile(serverBase); 061 } 062 063 @Search() 064 public List<StructureDefinition> getAllProfiles(ServletRequestDetails theRequest) { 065 final String serverBase = getServerBase(theRequest); 066 List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitionsWithExplicitId()); 067 Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() { 068 @Override 069 public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) { 070 int cmp = theO1.getName().compareTo(theO2.getName()); 071 if (cmp==0) { 072 cmp=theO1.getResourceProfile(serverBase).compareTo(theO2.getResourceProfile(serverBase)); 073 } 074 return cmp; 075 }}); 076 ArrayList<StructureDefinition> retVal = new ArrayList<StructureDefinition>(); 077 for (RuntimeResourceDefinition next : defs) { 078 retVal.add((StructureDefinition) next.toProfile(serverBase)); 079 } 080 return retVal; 081 } 082 083 private String getServerBase(ServletRequestDetails theHttpRequest) { 084 return myRestfulServer.getServerBaseForRequest(theHttpRequest); 085 } 086}