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