add test for conformance
This commit is contained in:
parent
9f7a104315
commit
d2cb35b6a1
|
@ -1,33 +1,40 @@
|
||||||
package ca.uhn.fhir.jaxrs.server;
|
package ca.uhn.fhir.jaxrs.server;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.ejb.Local;
|
||||||
|
import javax.ejb.Stateless;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.OPTIONS;
|
import javax.ws.rs.OPTIONS;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.ResponseBuilder;
|
|
||||||
|
|
||||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ca.uhn.fhir.context.ConfigurationException;
|
import ca.uhn.fhir.context.ConfigurationException;
|
||||||
import ca.uhn.fhir.context.RuntimeResourceDefinition;
|
import ca.uhn.fhir.context.RuntimeResourceDefinition;
|
||||||
|
import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Conformance;
|
import ca.uhn.fhir.model.dstu2.resource.Conformance;
|
||||||
import ca.uhn.fhir.rest.annotation.IdParam;
|
import ca.uhn.fhir.rest.annotation.IdParam;
|
||||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||||
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
|
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
|
||||||
import ca.uhn.fhir.rest.method.BaseMethodBinding;
|
import ca.uhn.fhir.rest.method.BaseMethodBinding;
|
||||||
|
import ca.uhn.fhir.rest.method.ParseAction;
|
||||||
import ca.uhn.fhir.rest.server.Constants;
|
import ca.uhn.fhir.rest.server.Constants;
|
||||||
import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
|
import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
|
||||||
|
import ca.uhn.fhir.rest.server.IResourceProvider;
|
||||||
|
import ca.uhn.fhir.rest.server.IRestfulResponse;
|
||||||
import ca.uhn.fhir.rest.server.ResourceBinding;
|
import ca.uhn.fhir.rest.server.ResourceBinding;
|
||||||
import ca.uhn.fhir.rest.server.RestulfulServerConfiguration;
|
import ca.uhn.fhir.rest.server.RestulfulServerConfiguration;
|
||||||
import ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider;
|
import ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider;
|
||||||
|
@ -37,8 +44,11 @@ import ca.uhn.fhir.util.ReflectionUtil;
|
||||||
* Conformance Rest Service
|
* Conformance Rest Service
|
||||||
* @author Peter Van Houte
|
* @author Peter Van Houte
|
||||||
*/
|
*/
|
||||||
|
@Local
|
||||||
|
@Path("")
|
||||||
|
@Stateless
|
||||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProvider implements IJaxRsConformanceProvider {
|
public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProvider {
|
||||||
|
|
||||||
public static final String PATH = "/";
|
public static final String PATH = "/";
|
||||||
private static final org.slf4j.Logger ourLog = LoggerFactory.getLogger(AbstractJaxRsConformanceProvider.class);
|
private static final org.slf4j.Logger ourLog = LoggerFactory.getLogger(AbstractJaxRsConformanceProvider.class);
|
||||||
|
@ -49,15 +59,19 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv
|
||||||
|
|
||||||
private Conformance myConformance;
|
private Conformance myConformance;
|
||||||
|
|
||||||
protected AbstractJaxRsConformanceProvider(String implementationDescription, String serverName, String serverVersion) {
|
public AbstractJaxRsConformanceProvider(String implementationDescription, String serverName, String serverVersion) {
|
||||||
serverConfiguration.setFhirContext(getFhirContext());
|
serverConfiguration.setFhirContext(getFhirContext());
|
||||||
serverConfiguration.setImplementationDescription(implementationDescription);
|
serverConfiguration.setImplementationDescription(implementationDescription);
|
||||||
serverConfiguration.setServerName(serverName);
|
serverConfiguration.setServerName(serverName);
|
||||||
serverConfiguration.setServerVersion(serverVersion);
|
serverConfiguration.setServerVersion(serverVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
protected void setUpPostConstruct()
|
protected void setUpPostConstruct()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
for (Entry<Class<? extends IResourceProvider>, IResourceProvider> provider : getProviders().entrySet()) {
|
||||||
|
addProvider(provider.getValue(), provider.getKey());
|
||||||
|
}
|
||||||
List<BaseMethodBinding<?>> serverBindings = new ArrayList<BaseMethodBinding<?>>();
|
List<BaseMethodBinding<?>> serverBindings = new ArrayList<BaseMethodBinding<?>>();
|
||||||
for (ResourceBinding baseMethodBinding : myResourceNameToBinding.values()) {
|
for (ResourceBinding baseMethodBinding : myResourceNameToBinding.values()) {
|
||||||
serverBindings.addAll(baseMethodBinding.getMethodBindings());
|
serverBindings.addAll(baseMethodBinding.getMethodBindings());
|
||||||
|
@ -72,20 +86,27 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv
|
||||||
myConformance = serverConformanceProvider.getServerConformance(null);
|
myConformance = serverConformanceProvider.getServerConformance(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
protected abstract ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> getProviders() throws Exception;
|
||||||
|
|
||||||
@OPTIONS
|
@OPTIONS
|
||||||
@Path("/metadata")
|
@Path("/metadata")
|
||||||
public Response conformance(String string) {
|
public Response conformanceUsingOptions() throws IOException {
|
||||||
String conformanceString = getParser(createRequestDetails(null, RequestTypeEnum.OPTIONS, RestOperationTypeEnum.METADATA)).encodeResourceToString(myConformance);
|
return conformance();
|
||||||
ResponseBuilder entity = Response.status(Constants.STATUS_HTTP_200_OK).entity(conformanceString);
|
|
||||||
entity.header("Access-Control-Allow-Origin", "*");
|
|
||||||
return entity.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int findResourceMethods(Object theProvider, Class<?> clazz) throws ConfigurationException {
|
@GET
|
||||||
|
@Path("/metadata")
|
||||||
|
public Response conformance() throws IOException {
|
||||||
|
JaxRsRequest request = createRequestDetails(null, RequestTypeEnum.OPTIONS, RestOperationTypeEnum.METADATA);
|
||||||
|
IRestfulResponse response = request.getResponse();
|
||||||
|
response.addHeader(Constants.HEADER_CORS_ALLOW_ORIGIN, "*");
|
||||||
|
return (Response) response.returnResponse(ParseAction.create(myConformance), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addProvider(Object theProvider, Class<?> theProviderInterface) throws ConfigurationException {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (Method m : ReflectionUtil.getDeclaredMethods(clazz)) {
|
for (Method m : ReflectionUtil.getDeclaredMethods(theProviderInterface)) {
|
||||||
BaseMethodBinding<?> foundMethodBinding = BaseMethodBinding.bindMethod(m, getFhirContext(), theProvider);
|
BaseMethodBinding<?> foundMethodBinding = BaseMethodBinding.bindMethod(m, getFhirContext(), theProvider);
|
||||||
if (foundMethodBinding == null) {
|
if (foundMethodBinding == null) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -144,9 +165,8 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends IBaseResource> getResourceType() {
|
public Class<Conformance> getResourceType() {
|
||||||
return Conformance.class;
|
return Conformance.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package ca.uhn.fhir.jaxrs.server;
|
package ca.uhn.fhir.jaxrs.server;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
@ -16,6 +18,7 @@ import ca.uhn.fhir.rest.server.AddProfileTagEnum;
|
||||||
import ca.uhn.fhir.rest.server.ETagSupportEnum;
|
import ca.uhn.fhir.rest.server.ETagSupportEnum;
|
||||||
import ca.uhn.fhir.rest.server.EncodingEnum;
|
import ca.uhn.fhir.rest.server.EncodingEnum;
|
||||||
import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
|
import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
|
||||||
|
import ca.uhn.fhir.rest.server.IResourceProvider;
|
||||||
import ca.uhn.fhir.rest.server.IRestfulServerDefaults;
|
import ca.uhn.fhir.rest.server.IRestfulServerDefaults;
|
||||||
import ca.uhn.fhir.rest.server.IServerAddressStrategy;
|
import ca.uhn.fhir.rest.server.IServerAddressStrategy;
|
||||||
import ca.uhn.fhir.rest.server.RestfulServerUtils;
|
import ca.uhn.fhir.rest.server.RestfulServerUtils;
|
||||||
|
@ -25,7 +28,7 @@ import ca.uhn.fhir.rest.server.RestfulServerUtils;
|
||||||
* @author Peter Van Houte
|
* @author Peter Van Houte
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
|
public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults, IResourceProvider {
|
||||||
|
|
||||||
public static FhirContext CTX = FhirContext.forDstu2();
|
public static FhirContext CTX = FhirContext.forDstu2();
|
||||||
|
|
||||||
|
@ -43,10 +46,10 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
|
||||||
* param and query methods
|
* param and query methods
|
||||||
*/
|
*/
|
||||||
public HashMap<String, String[]> getQueryMap() {
|
public HashMap<String, String[]> getQueryMap() {
|
||||||
MultivaluedMap<String, String> queryParameters = getInfo().getQueryParameters();
|
MultivaluedMap<String, String> queryParameters = getUriInfo().getQueryParameters();
|
||||||
HashMap<String, String[]> params = new HashMap<String, String[]>();
|
HashMap<String, String[]> params = new HashMap<String, String[]>();
|
||||||
for (String key : queryParameters.keySet()) {
|
for (Entry<String, List<String>> paramEntry : queryParameters.entrySet()) {
|
||||||
params.put(key, queryParameters.get(key).toArray(new String[] {}));
|
params.put(paramEntry.getKey(), paramEntry.getValue().toArray(new String[paramEntry.getValue().size()]));
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
@ -58,7 +61,7 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBaseUri() {
|
public String getBaseUri() {
|
||||||
return getInfo().getBaseUri().toASCIIString();
|
return getUriInfo().getBaseUri().toASCIIString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,19 +76,35 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the info
|
* Get the uriInfo
|
||||||
* @return the info
|
* @return the uri info
|
||||||
*/
|
*/
|
||||||
public UriInfo getInfo() {
|
public UriInfo getUriInfo() {
|
||||||
return theUriInfo;
|
return this.theUriInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Uri Info
|
||||||
|
* @param uriInfo the uri info
|
||||||
|
*/
|
||||||
|
public void setUriInfo(UriInfo uriInfo) {
|
||||||
|
this.theUriInfo = uriInfo;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the headers
|
* Get the headers
|
||||||
* @return the headers
|
* @return the headers
|
||||||
*/
|
*/
|
||||||
public HttpHeaders getHeaders() {
|
public HttpHeaders getHeaders() {
|
||||||
return theHeaders;
|
return this.theHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the headers
|
||||||
|
* @param headers the headers to set
|
||||||
|
*/
|
||||||
|
public void setHeaders(HttpHeaders headers) {
|
||||||
|
this.theHeaders = headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,6 +31,7 @@ import ca.uhn.fhir.rest.method.RequestDetails;
|
||||||
import ca.uhn.fhir.rest.server.BundleInclusionRule;
|
import ca.uhn.fhir.rest.server.BundleInclusionRule;
|
||||||
import ca.uhn.fhir.rest.server.Constants;
|
import ca.uhn.fhir.rest.server.Constants;
|
||||||
import ca.uhn.fhir.rest.server.IPagingProvider;
|
import ca.uhn.fhir.rest.server.IPagingProvider;
|
||||||
|
import ca.uhn.fhir.rest.server.IRestfulServer;
|
||||||
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
|
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
|
||||||
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
|
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
|
||||||
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
|
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
|
||||||
|
@ -43,7 +44,7 @@ import ca.uhn.fhir.util.UrlUtil;
|
||||||
*/
|
*/
|
||||||
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
|
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
|
||||||
@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON, "application/json+fhir", "application/xml+fhir"})
|
@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON, "application/json+fhir", "application/xml+fhir"})
|
||||||
public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends AbstractJaxRsProvider implements IJaxRsResourceProvider<R> {
|
public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends AbstractJaxRsProvider implements IRestfulServer<JaxRsRequest> {
|
||||||
|
|
||||||
private final MethodBindings bindings;
|
private final MethodBindings bindings;
|
||||||
|
|
||||||
|
@ -56,9 +57,9 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getBaseUri() {
|
public String getBaseUri() {
|
||||||
try {
|
try {
|
||||||
return new URL(getInfo().getBaseUri().toURL(), getResourceType().getSimpleName()).toExternalForm();
|
return new URL(getUriInfo().getBaseUri().toURL(), getResourceType().getSimpleName()).toExternalForm();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
// cannot happen
|
// cannot happen
|
||||||
return null;
|
return null;
|
||||||
|
@ -66,7 +67,6 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Override
|
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response create(final String resourceString)
|
public Response create(final String resourceString)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
@ -76,20 +76,17 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
@POST
|
@POST
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
@Path("/_search")
|
@Path("/_search")
|
||||||
@Override
|
|
||||||
public Response searchWithPost() throws Exception {
|
public Response searchWithPost() throws Exception {
|
||||||
return executeMethod(null, RequestTypeEnum.POST, RestOperationTypeEnum.SEARCH_TYPE, null);
|
return executeMethod(null, RequestTypeEnum.POST, RestOperationTypeEnum.SEARCH_TYPE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Override
|
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response search() throws Exception {
|
public Response search() throws Exception {
|
||||||
return executeMethod(null, RequestTypeEnum.GET, RestOperationTypeEnum.SEARCH_TYPE, null);
|
return executeMethod(null, RequestTypeEnum.GET, RestOperationTypeEnum.SEARCH_TYPE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Override
|
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response update(@PathParam("id") final String id, final String resourceString)
|
public Response update(@PathParam("id") final String id, final String resourceString)
|
||||||
|
@ -98,7 +95,6 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Override
|
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response delete(@PathParam("id") final String id) throws Exception {
|
public Response delete(@PathParam("id") final String id) throws Exception {
|
||||||
|
@ -107,7 +103,6 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
|
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Override
|
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response find(@PathParam("id") final String id) throws Exception {
|
public Response find(@PathParam("id") final String id) throws Exception {
|
||||||
|
@ -120,7 +115,6 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Override
|
|
||||||
@Path("/{id}/_history/{version}")
|
@Path("/{id}/_history/{version}")
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response findHistory(@PathParam("id") final String id, @PathParam("version") final String versionString)
|
public Response findHistory(@PathParam("id") final String id, @PathParam("version") final String versionString)
|
||||||
|
@ -128,21 +122,20 @@ public abstract class AbstractJaxRsResourceProvider<R extends IResource> extends
|
||||||
BaseMethodBinding<?> method = getBindings().getBinding(RestOperationTypeEnum.VREAD);
|
BaseMethodBinding<?> method = getBindings().getBinding(RestOperationTypeEnum.VREAD);
|
||||||
final RequestDetails theRequest = createRequestDetails(null, RequestTypeEnum.GET, RestOperationTypeEnum.VREAD);
|
final RequestDetails theRequest = createRequestDetails(null, RequestTypeEnum.GET, RestOperationTypeEnum.VREAD);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
throw new InvalidRequestException("Don't know how to handle request path: " + getInfo().getRequestUri().toASCIIString());
|
throw new InvalidRequestException("Don't know how to handle request path: " + getUriInfo().getRequestUri().toASCIIString());
|
||||||
}
|
}
|
||||||
theRequest.setId(new IdDt(getBaseUri(), id, UrlUtil.unescape(versionString)));
|
theRequest.setId(new IdDt(getBaseUri(), id, UrlUtil.unescape(versionString)));
|
||||||
return (Response) method.invokeServer(this, theRequest);
|
return (Response) method.invokeServer(this, theRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Override
|
|
||||||
@Path("/{id}/{compartment}")
|
@Path("/{id}/{compartment}")
|
||||||
@Interceptors(JaxRsExceptionInterceptor.class)
|
@Interceptors(JaxRsExceptionInterceptor.class)
|
||||||
public Response findCompartment(@PathParam("id") final String id, @PathParam("compartment") final String compartment) throws BaseServerResponseException, IOException {
|
public Response findCompartment(@PathParam("id") final String id, @PathParam("compartment") final String compartment) throws BaseServerResponseException, IOException {
|
||||||
BaseMethodBinding<?> method = getBindings().getBinding(RestOperationTypeEnum.SEARCH_TYPE, compartment);
|
BaseMethodBinding<?> method = getBindings().getBinding(RestOperationTypeEnum.SEARCH_TYPE, compartment);
|
||||||
final RequestDetails theRequest = createRequestDetails(null, RequestTypeEnum.GET, RestOperationTypeEnum.VREAD);
|
final RequestDetails theRequest = createRequestDetails(null, RequestTypeEnum.GET, RestOperationTypeEnum.VREAD);
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
throw new InvalidRequestException("Don't know how to handle request path: " + getInfo().getRequestUri().toASCIIString());
|
throw new InvalidRequestException("Don't know how to handle request path: " + getUriInfo().getRequestUri().toASCIIString());
|
||||||
}
|
}
|
||||||
theRequest.setCompartmentName(compartment);
|
theRequest.setCompartmentName(compartment);
|
||||||
theRequest.setId(new IdDt(getBaseUri(), id));
|
theRequest.setId(new IdDt(getBaseUri(), id));
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
package ca.uhn.fhir.jaxrs.server;
|
|
||||||
|
|
||||||
import ca.uhn.fhir.rest.server.IResourceProvider;
|
|
||||||
import ca.uhn.fhir.rest.server.IRestfulServerDefaults;
|
|
||||||
|
|
||||||
public interface IJaxRsConformanceProvider extends IResourceProvider, IRestfulServerDefaults {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
package ca.uhn.fhir.jaxrs.server;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
|
|
||||||
import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest;
|
|
||||||
import ca.uhn.fhir.rest.server.IResourceProvider;
|
|
||||||
import ca.uhn.fhir.rest.server.IRestfulServer;
|
|
||||||
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
|
|
||||||
|
|
||||||
public interface IJaxRsResourceProvider<T> extends IRestfulServer<JaxRsRequest>, IResourceProvider {
|
|
||||||
|
|
||||||
Response search()
|
|
||||||
throws Exception;
|
|
||||||
|
|
||||||
Response create(String resourceString)
|
|
||||||
throws Exception;
|
|
||||||
|
|
||||||
Response searchWithPost()
|
|
||||||
throws Exception;
|
|
||||||
|
|
||||||
Response find(String id) throws Exception;
|
|
||||||
|
|
||||||
Response update(String id, String resourceString)
|
|
||||||
throws Exception;
|
|
||||||
|
|
||||||
Response delete(String id)
|
|
||||||
throws Exception;
|
|
||||||
|
|
||||||
Response findHistory(String id, String version) throws BaseServerResponseException, IOException;
|
|
||||||
|
|
||||||
Response findCompartment(String id, String compartment) throws BaseServerResponseException, IOException;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -13,6 +13,7 @@ import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.ResponseBuilder;
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
||||||
|
|
||||||
import ca.uhn.fhir.parser.IParser;
|
import ca.uhn.fhir.parser.IParser;
|
||||||
|
@ -43,7 +44,8 @@ public class JaxRsResponse extends RestfulResponse<JaxRsRequest> {
|
||||||
@Override
|
@Override
|
||||||
public Response sendWriterResponse(int status, String contentType, String charset, Writer writer) {
|
public Response sendWriterResponse(int status, String contentType, String charset, Writer writer) {
|
||||||
Object entity = writer instanceof StringWriter ? writer.toString() : writer;
|
Object entity = writer instanceof StringWriter ? writer.toString() : writer;
|
||||||
return buildResponse(status)/*.header(HttpHeaders.CONTENT_TYPE, charset)*/.header(Constants.HEADER_CONTENT_TYPE, contentType).entity(entity).build();
|
String charContentType = contentType+";charset="+StringUtils.defaultIfBlank(charset, Constants.CHARSET_NAME_UTF8);
|
||||||
|
return buildResponse(status).header(Constants.HEADER_CONTENT_TYPE, charContentType).entity(entity).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
package ca.uhn.fhir.jaxrs.server;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import javax.ws.rs.HttpMethod;
|
||||||
|
import javax.ws.rs.core.MultivaluedHashMap;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
|
import org.glassfish.jersey.internal.MapPropertiesDelegate;
|
||||||
|
import org.glassfish.jersey.server.ContainerRequest;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.jaxrs.server.example.TestJaxRsPatientRestProvider;
|
||||||
|
import ca.uhn.fhir.jaxrs.server.example.TestDummyPatientProvider;
|
||||||
|
import ca.uhn.fhir.rest.server.Constants;
|
||||||
|
import ca.uhn.fhir.rest.server.IResourceProvider;
|
||||||
|
|
||||||
|
public class AbstractJaxRsConformanceProviderTest {
|
||||||
|
|
||||||
|
private static final String BASEURI = "http://basiuri";
|
||||||
|
private static final String REQUESTURI = BASEURI + "/metadata";
|
||||||
|
AbstractJaxRsConformanceProvider provider;
|
||||||
|
private ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> providers;
|
||||||
|
private ContainerRequest headers;
|
||||||
|
private MultivaluedHashMap<String, String> queryParameters;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
// headers
|
||||||
|
headers = new ContainerRequest(new URI(BASEURI), new URI(REQUESTURI), HttpMethod.GET, null,
|
||||||
|
new MapPropertiesDelegate());
|
||||||
|
// uri info
|
||||||
|
queryParameters = new MultivaluedHashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
providers = new ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider>();
|
||||||
|
provider = createConformanceProvider(providers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConformance() throws Exception {
|
||||||
|
providers.put(AbstractJaxRsConformanceProvider.class, provider);
|
||||||
|
providers.put(TestDummyPatientProvider.class, new TestDummyPatientProvider());
|
||||||
|
Response response = createConformanceProvider(providers).conformance();
|
||||||
|
System.out.println(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConformanceWithMethods() throws Exception {
|
||||||
|
providers.put(AbstractJaxRsConformanceProvider.class, provider);
|
||||||
|
providers.put(TestJaxRsPatientRestProvider.class, new TestJaxRsPatientRestProvider());
|
||||||
|
Response response = createConformanceProvider(providers).conformance();
|
||||||
|
assertEquals(Constants.STATUS_HTTP_200_OK, response.getStatus());
|
||||||
|
assertTrue(response.getEntity().toString().contains("\"type\":\"Patient\""));
|
||||||
|
assertTrue(response.getEntity().toString().contains("\"$last"));
|
||||||
|
System.out.println(response);
|
||||||
|
System.out.println(response.getEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConformanceInXml() throws Exception {
|
||||||
|
queryParameters.put(Constants.PARAM_FORMAT, Arrays.asList(Constants.CT_XML));
|
||||||
|
providers.put(AbstractJaxRsConformanceProvider.class, provider);
|
||||||
|
providers.put(TestJaxRsPatientRestProvider.class, new TestJaxRsPatientRestProvider());
|
||||||
|
Response response = createConformanceProvider(providers).conformance();
|
||||||
|
assertEquals(Constants.STATUS_HTTP_200_OK, response.getStatus());
|
||||||
|
System.out.println(response.getEntity());
|
||||||
|
assertTrue(response.getEntity().toString().contains(" <type value=\"Patient\"/>"));
|
||||||
|
assertTrue(response.getEntity().toString().contains("\"$last"));
|
||||||
|
System.out.println(response.getEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AbstractJaxRsConformanceProvider createConformanceProvider(final ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> providers)
|
||||||
|
throws Exception {
|
||||||
|
AbstractJaxRsConformanceProvider result = new AbstractJaxRsConformanceProvider(null, null, null) {
|
||||||
|
@Override
|
||||||
|
protected ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> getProviders() {
|
||||||
|
return providers;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// mocks
|
||||||
|
UriInfo uriInfo = mock(UriInfo.class);
|
||||||
|
when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
|
||||||
|
when(uriInfo.getBaseUri()).thenReturn(new URI(BASEURI));
|
||||||
|
result.setUriInfo(uriInfo);
|
||||||
|
result.setHeaders(headers);
|
||||||
|
result.setUpPostConstruct();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package ca.uhn.fhir.jaxrs.server;
|
package ca.uhn.fhir.jaxrs.server.example;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -16,8 +16,6 @@ import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import ca.uhn.fhir.context.FhirContext;
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
import ca.uhn.fhir.jaxrs.server.example.JaxRsPatientRestProvider;
|
|
||||||
import ca.uhn.fhir.jaxrs.server.example.RandomServerPortProvider;
|
|
||||||
import ca.uhn.fhir.model.api.BundleEntry;
|
import ca.uhn.fhir.model.api.BundleEntry;
|
||||||
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
|
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
|
||||||
import ca.uhn.fhir.model.dstu2.resource.Bundle;
|
import ca.uhn.fhir.model.dstu2.resource.Bundle;
|
||||||
|
@ -56,7 +54,7 @@ public class JaxRsPatientProviderTest {
|
||||||
jettyServer.setHandler(context);
|
jettyServer.setHandler(context);
|
||||||
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
|
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
|
||||||
jerseyServlet.setInitOrder(0);
|
jerseyServlet.setInitOrder(0);
|
||||||
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", JaxRsPatientRestProvider.class.getCanonicalName());
|
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", TestJaxRsPatientRestProvider.class.getCanonicalName() + "," +TestJaxRsConformanceRestProvider.class.getCanonicalName());
|
||||||
jettyServer.start();
|
jettyServer.start();
|
||||||
|
|
||||||
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
|
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
|
||||||
|
@ -75,7 +73,7 @@ public class JaxRsPatientProviderTest {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Search/Query - Type */
|
/** Search/Query - Type */
|
||||||
@Test
|
@Test
|
||||||
public void findUsingGenericClientBySearch() {
|
public void findUsingGenericClientBySearch() {
|
||||||
|
@ -236,7 +234,6 @@ public class JaxRsPatientProviderTest {
|
||||||
|
|
||||||
/** Conformance - Server */
|
/** Conformance - Server */
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void testConformance() {
|
public void testConformance() {
|
||||||
final Conformance conf = client.fetchConformance().ofType(Conformance.class).execute();
|
final Conformance conf = client.fetchConformance().ofType(Conformance.class).execute();
|
||||||
System.out.println(conf.getRest().get(0).getResource().get(0).getType());
|
System.out.println(conf.getRest().get(0).getResource().get(0).getType());
|
|
@ -0,0 +1,35 @@
|
||||||
|
package ca.uhn.fhir.jaxrs.server.example;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import javax.ejb.Stateless;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsConformanceProvider;
|
||||||
|
import ca.uhn.fhir.rest.server.Constants;
|
||||||
|
import ca.uhn.fhir.rest.server.IResourceProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fhir Physician Rest Service
|
||||||
|
* @author axmpm
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Path(TestJaxRsConformanceRestProvider.PATH)
|
||||||
|
@Stateless
|
||||||
|
@Produces({MediaType.APPLICATION_JSON, Constants.CT_FHIR_JSON, Constants.CT_FHIR_XML})
|
||||||
|
public class TestJaxRsConformanceRestProvider extends AbstractJaxRsConformanceProvider {
|
||||||
|
|
||||||
|
public TestJaxRsConformanceRestProvider() {
|
||||||
|
super("", "", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> getProviders() throws Exception {
|
||||||
|
ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> map = new ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider>();
|
||||||
|
map.put(TestJaxRsPatientRestProvider.class, new TestJaxRsPatientRestProvider());
|
||||||
|
map.put(TestJaxRsConformanceRestProvider.class, new TestJaxRsConformanceRestProvider());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,18 +53,18 @@ import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
|
||||||
* @author axmpm
|
* @author axmpm
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Path(JaxRsPatientRestProvider.PATH)
|
@Path(TestJaxRsPatientRestProvider.PATH)
|
||||||
@Stateless
|
@Stateless
|
||||||
@Produces({MediaType.APPLICATION_JSON, Constants.CT_FHIR_JSON, Constants.CT_FHIR_XML})
|
@Produces({MediaType.APPLICATION_JSON, Constants.CT_FHIR_JSON, Constants.CT_FHIR_XML})
|
||||||
public class JaxRsPatientRestProvider extends AbstractJaxRsResourceProvider<Patient> /*implements IJaxRsPatientProvider*/ {
|
public class TestJaxRsPatientRestProvider extends AbstractJaxRsResourceProvider<Patient> {
|
||||||
|
|
||||||
static final String PATH = "/Patient";
|
static final String PATH = "/Patient";
|
||||||
|
|
||||||
private static Long counter = 1L;
|
private static Long counter = 1L;
|
||||||
private static final ConcurrentHashMap<String, List<Patient>> patients = new ConcurrentHashMap<String, List<Patient>>();
|
private static final ConcurrentHashMap<String, List<Patient>> patients = new ConcurrentHashMap<String, List<Patient>>();
|
||||||
|
|
||||||
public JaxRsPatientRestProvider() throws Exception {
|
public TestJaxRsPatientRestProvider() throws Exception {
|
||||||
super(JaxRsPatientRestProvider.class);
|
super(TestJaxRsPatientRestProvider.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
|
@ -111,7 +111,7 @@ public class JaxRsRequestTest {
|
||||||
|
|
||||||
//mocks
|
//mocks
|
||||||
provider = spy(TestDummyPatientProvider.class);
|
provider = spy(TestDummyPatientProvider.class);
|
||||||
doReturn(uriInfo).when(provider).getInfo();
|
doReturn(uriInfo).when(provider).getUriInfo();
|
||||||
doReturn(BASEURI).when(provider).getBaseUri();
|
doReturn(BASEURI).when(provider).getBaseUri();
|
||||||
doReturn(headers).when(provider).getHeaders();
|
doReturn(headers).when(provider).getHeaders();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue