diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java index 42c8ce1532f..af23c263a25 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java @@ -32,6 +32,8 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.annotation.ResourceDef; import com.phloc.commons.url.URLValidator; +import javax.servlet.http.HttpServletRequest; + public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefinition { private RuntimeResourceDefinition myBaseDefinition; @@ -136,17 +138,26 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini } while (target.equals(Object.class)==false); } + @Deprecated public synchronized IResource toProfile() { if (myProfileDef != null) { return myProfileDef; } - IResource retVal = myContext.getVersion().generateProfile(this); + IResource retVal = myContext.getVersion().generateProfile(this, null); myProfileDef = retVal; return retVal; } + public synchronized IResource toProfile(HttpServletRequest theRequest) { + if (myProfileDef != null) { + return myProfileDef; + } + IResource retVal = myContext.getVersion().generateProfile(this, theRequest); + myProfileDef = retVal; + return retVal; + } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/IFhirVersion.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/IFhirVersion.java index a1b97f896a3..093f99549de 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/IFhirVersion.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/IFhirVersion.java @@ -24,12 +24,14 @@ import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import javax.servlet.http.HttpServletRequest; + public interface IFhirVersion { - IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition); + IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, HttpServletRequest theRequest); Object createServerConformanceProvider(RestfulServer theServer); - IResourceProvider createServerProfilesProvider(RestfulServer theRestfulServer); - + IResourceProvider createServerProfilesProvider(RestfulServer theRestfulServer); + } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java index 6ecd9709ed1..148ca56ad75 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java @@ -72,7 +72,8 @@ public class TestRestfulServer extends RestfulServer { String baseUrl = System.getProperty("fhir.baseurl"); if (StringUtils.isBlank(baseUrl)) { - throw new ServletException("Missing system property: fhir.baseurl"); + //throw new ServletException("Missing system property: fhir.baseurl"); + baseUrl = "http://localhost:8080/base"; } setServerAddressStrategy(new HardcodedServerAddressStrategy(baseUrl)); diff --git a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/FhirDev.java b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/FhirDev.java index de6f79ef724..dc2680aff8c 100644 --- a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/FhirDev.java +++ b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/model/dev/FhirDev.java @@ -32,6 +32,8 @@ import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.provider.dev.ServerConformanceProvider; import ca.uhn.fhir.rest.server.provider.dev.ServerProfileProvider; +import javax.servlet.http.HttpServletRequest; + public class FhirDev implements IFhirVersion { private String myId; @@ -42,7 +44,7 @@ public class FhirDev implements IFhirVersion { } @Override - public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition) { + public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, HttpServletRequest theRequest) { Profile retVal = new Profile(); RuntimeResourceDefinition def = theRuntimeResourceDefinition; diff --git a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/rest/server/provider/dev/ServerProfileProvider.java b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/rest/server/provider/dev/ServerProfileProvider.java index 1e11a5034fc..4c4321b98b1 100644 --- a/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/rest/server/provider/dev/ServerProfileProvider.java +++ b/hapi-fhir-structures-dev/src/main/java/ca/uhn/fhir/rest/server/provider/dev/ServerProfileProvider.java @@ -35,6 +35,8 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.server.IResourceProvider; +import javax.servlet.http.HttpServletRequest; + public class ServerProfileProvider implements IResourceProvider { private FhirContext myContext; @@ -49,16 +51,16 @@ public class ServerProfileProvider implements IResourceProvider { } @Read() - public Profile getProfileById(@IdParam IdDt theId) { + public Profile getProfileById(HttpServletRequest theRequest, @IdParam IdDt theId) { RuntimeResourceDefinition retVal = myContext.getResourceDefinitionById(theId.getValue()); if (retVal==null) { return null; } - return (Profile) retVal.toProfile(); + return (Profile) retVal.toProfile(theRequest); } @Search() - public List getAllProfiles() { + public List getAllProfiles(HttpServletRequest theRequest) { List defs = new ArrayList(myContext.getResourceDefinitions()); Collections.sort(defs, new Comparator() { @Override @@ -71,7 +73,7 @@ public class ServerProfileProvider implements IResourceProvider { }}); ArrayList retVal = new ArrayList(); for (RuntimeResourceDefinition next : defs) { - retVal.add((Profile) next.toProfile()); + retVal.add((Profile) next.toProfile(theRequest)); } return retVal; } diff --git a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java index 4b88824e4dc..a1cc2fefc92 100644 --- a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java +++ b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java @@ -55,6 +55,8 @@ import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider; import ca.uhn.fhir.rest.server.provider.ServerProfileProvider; +import javax.servlet.http.HttpServletRequest; + public class FhirDstu1 implements IFhirVersion { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirDstu1.class); @@ -231,7 +233,7 @@ public class FhirDstu1 implements IFhirVersion { } @Override - public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition) { + public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, HttpServletRequest theRequest) { Profile retVal = new Profile(); RuntimeResourceDefinition def = theRuntimeResourceDefinition; @@ -318,7 +320,7 @@ public class FhirDstu1 implements IFhirVersion { @Override public IResourceProvider createServerProfilesProvider(RestfulServer theRestfulServer) { - return new ServerProfileProvider(theRestfulServer.getFhirContext()); + return new ServerProfileProvider(theRestfulServer); } } diff --git a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/rest/server/provider/ServerProfileProvider.java b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/rest/server/provider/ServerProfileProvider.java index 0bfaf1d1d22..113e0bbc5ba 100644 --- a/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/rest/server/provider/ServerProfileProvider.java +++ b/hapi-fhir-structures-dstu/src/main/java/ca/uhn/fhir/rest/server/provider/ServerProfileProvider.java @@ -34,13 +34,16 @@ import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.RestfulServer; + +import javax.servlet.http.HttpServletRequest; public class ServerProfileProvider implements IResourceProvider { private FhirContext myContext; - public ServerProfileProvider(FhirContext theCtx) { - myContext = theCtx; + public ServerProfileProvider(RestfulServer theServer) { + myContext = theServer.getFhirContext(); } @Override @@ -49,16 +52,16 @@ public class ServerProfileProvider implements IResourceProvider { } @Read() - public Profile getProfileById(@IdParam IdDt theId) { + public Profile getProfileById(HttpServletRequest theRequest, @IdParam IdDt theId) { RuntimeResourceDefinition retVal = myContext.getResourceDefinitionById(theId.getIdPart()); if (retVal==null) { return null; } - return (Profile) retVal.toProfile(); + return (Profile) retVal.toProfile(theRequest); } @Search() - public List getAllProfiles() { + public List getAllProfiles(HttpServletRequest theRequest) { List defs = new ArrayList(myContext.getResourceDefinitions()); Collections.sort(defs, new Comparator() { @Override @@ -71,7 +74,7 @@ public class ServerProfileProvider implements IResourceProvider { }}); ArrayList retVal = new ArrayList(); for (RuntimeResourceDefinition next : defs) { - retVal.add((Profile) next.toProfile()); + retVal.add((Profile) next.toProfile(theRequest)); } return retVal; } diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java index 9f48e00acc6..688cbe09a66 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java @@ -997,7 +997,7 @@ public class RestfulServerMethodTest { public void testServerProfileProviderFindsProfiles() { ServerProfileProvider profileProvider = (ServerProfileProvider)ourRestfulServer.getServerProfilesProvider(); IdDt id = new IdDt("Profile", "observation"); - Profile profile = profileProvider.getProfileById(id); + Profile profile = profileProvider.getProfileById(null, id); assertNotNull(profile); } @@ -1013,12 +1013,14 @@ public class RestfulServerMethodTest { ourCtx = new FhirContext(Patient.class); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); - ServerProfileProvider profProvider = new ServerProfileProvider(ourCtx); ourReportProvider = new DummyDiagnosticReportResourceProvider(); DummyAdverseReactionResourceProvider adv = new DummyAdverseReactionResourceProvider(); ServletHandler proxyHandler = new ServletHandler(); - ourRestfulServer =new DummyRestfulServer(patientProvider, profProvider, ourReportProvider, adv); + DummyRestfulServer dummyServer = new DummyRestfulServer(patientProvider, ourReportProvider, adv); + ourRestfulServer = dummyServer; + ServerProfileProvider profProvider = new ServerProfileProvider(ourRestfulServer); + dummyServer.addResourceProvider(profProvider); ServletHolder servletHolder = new ServletHolder(ourRestfulServer); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); @@ -1370,7 +1372,11 @@ public class RestfulServerMethodTest { private Collection myResourceProviders; public DummyRestfulServer(IResourceProvider... theResourceProviders) { - myResourceProviders = Arrays.asList(theResourceProviders); + myResourceProviders = new ArrayList(Arrays.asList(theResourceProviders)); + } + + public void addResourceProvider(IResourceProvider theResourceProvider) { + myResourceProviders.add(theResourceProvider); } @Override diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerSelfReferenceTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerSelfReferenceTest.java index 8616ec7d97c..2a648b3441b 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerSelfReferenceTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerSelfReferenceTest.java @@ -106,10 +106,10 @@ public class RestfulServerSelfReferenceTest { Server hServer = new Server(port); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); - ServerProfileProvider profProvider = new ServerProfileProvider(ourCtx); ServletHandler proxyHandler = new ServletHandler(); RestfulServer server = new RestfulServer(); + ServerProfileProvider profProvider = new ServerProfileProvider(server); server.setFhirContext(ourCtx); server.setResourceProviders(patientProvider, profProvider); ServletHolder servletHolder = new ServletHolder(server); diff --git a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java index 085b0f926d9..dcb0fb45bc2 100644 --- a/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java +++ b/hapi-tinder-plugin/src/main/java/ca/uhn/fhir/model/dstu/FhirDstu1.java @@ -6,6 +6,8 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import javax.servlet.http.HttpServletRequest; + public class FhirDstu1 implements IFhirVersion { @Override @@ -19,7 +21,7 @@ public class FhirDstu1 implements IFhirVersion { } @Override - public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition) { + public IResource generateProfile(RuntimeResourceDefinition theRuntimeResourceDefinition, HttpServletRequest theRequest) { throw new UnsupportedOperationException(); }