Clean up build so that JAX-RS uses the same response pipeline as the

non-JAX-RS codebase for create/update/delete responses, re #374
This commit is contained in:
James Agnew 2016-06-01 11:28:35 -04:00
parent 8a933cd2c2
commit c318d1a040
3 changed files with 221 additions and 212 deletions

View File

@ -25,9 +25,11 @@ 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.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -52,6 +54,7 @@ import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest.Builder;
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.api.SummaryEnum;
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.method.ParseAction;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
@ -63,199 +66,220 @@ import ca.uhn.fhir.rest.server.RestulfulServerConfiguration;
import ca.uhn.fhir.util.ReflectionUtil; import ca.uhn.fhir.util.ReflectionUtil;
/** /**
* This is the conformance provider for the jax rs servers. It requires all providers to be registered * This is the conformance provider for the jax rs servers. It requires all providers to be registered during startup because the conformance profile is generated during the postconstruct phase.
* during startup because the conformance profile is generated during the postconstruct phase.
* *
* @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
*/ */
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProvider implements IResourceProvider { public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProvider implements IResourceProvider {
/** the logger */ /** the logger */
private static final org.slf4j.Logger ourLog = LoggerFactory.getLogger(AbstractJaxRsConformanceProvider.class); private static final org.slf4j.Logger ourLog = LoggerFactory.getLogger(AbstractJaxRsConformanceProvider.class);
/** the server bindings */ /** the server bindings */
private ResourceBinding myServerBinding = new ResourceBinding(); private ResourceBinding myServerBinding = new ResourceBinding();
/** the resource bindings */ /** the resource bindings */
private ConcurrentHashMap<String, ResourceBinding> myResourceNameToBinding = new ConcurrentHashMap<String, ResourceBinding>(); private ConcurrentHashMap<String, ResourceBinding> myResourceNameToBinding = new ConcurrentHashMap<String, ResourceBinding>();
/** the server configuration */ /** the server configuration */
private RestulfulServerConfiguration serverConfiguration = new RestulfulServerConfiguration(); private RestulfulServerConfiguration serverConfiguration = new RestulfulServerConfiguration();
/** the conformance. It is created once during startup */ /** the conformance. It is created once during startup */
private Conformance myDstu3Conformance; private Conformance myDstu3Conformance;
private ca.uhn.fhir.model.dstu2.resource.Conformance myDstu2Conformance; private ca.uhn.fhir.model.dstu2.resource.Conformance myDstu2Conformance;
/**
* Constructor allowing the description, servername and server to be set
* @param implementationDescription the implementation description. If null, "" is used
* @param serverName the server name. If null, "" is used
* @param serverVersion the server version. If null, "" is used
*/
protected AbstractJaxRsConformanceProvider(String implementationDescription, String serverName, String serverVersion) {
serverConfiguration.setFhirContext(getFhirContext());
serverConfiguration.setImplementationDescription(StringUtils.defaultIfEmpty(implementationDescription, ""));
serverConfiguration.setServerName(StringUtils.defaultIfEmpty(serverName, ""));
serverConfiguration.setServerVersion(StringUtils.defaultIfEmpty(serverVersion, ""));
}
/**
* Constructor allowing the description, servername and server to be set
* @param ctx the {@link FhirContext} instance.
* @param implementationDescription the implementation description. If null, "" is used
* @param serverName the server name. If null, "" is used
* @param serverVersion the server version. If null, "" is used
*/
protected AbstractJaxRsConformanceProvider(FhirContext ctx, String implementationDescription, String serverName, String serverVersion) {
super(ctx);
serverConfiguration.setFhirContext(ctx);
serverConfiguration.setImplementationDescription(StringUtils.defaultIfEmpty(implementationDescription, ""));
serverConfiguration.setServerName(StringUtils.defaultIfEmpty(serverName, ""));
serverConfiguration.setServerVersion(StringUtils.defaultIfEmpty(serverVersion, ""));
}
/** /**
* This method will set the conformance during the postconstruct phase. The * Constructor allowing the description, servername and server to be set
* method {@link AbstractJaxRsConformanceProvider#getProviders()} is used to *
* get all the resource providers include in the conformance * @param implementationDescription
* the implementation description. If null, "" is used
* @param serverName
* the server name. If null, "" is used
* @param serverVersion
* the server version. If null, "" is used
*/ */
@PostConstruct protected AbstractJaxRsConformanceProvider(String implementationDescription, String serverName, String serverVersion) {
protected void setUpPostConstruct() { serverConfiguration.setFhirContext(getFhirContext());
for (Entry<Class<? extends IResourceProvider>, IResourceProvider> provider : getProviders().entrySet()) { serverConfiguration.setImplementationDescription(StringUtils.defaultIfEmpty(implementationDescription, ""));
serverConfiguration.setServerName(StringUtils.defaultIfEmpty(serverName, ""));
serverConfiguration.setServerVersion(StringUtils.defaultIfEmpty(serverVersion, ""));
}
/**
* Constructor allowing the description, servername and server to be set
*
* @param ctx
* the {@link FhirContext} instance.
* @param implementationDescription
* the implementation description. If null, "" is used
* @param serverName
* the server name. If null, "" is used
* @param serverVersion
* the server version. If null, "" is used
*/
protected AbstractJaxRsConformanceProvider(FhirContext ctx, String implementationDescription, String serverName, String serverVersion) {
super(ctx);
serverConfiguration.setFhirContext(ctx);
serverConfiguration.setImplementationDescription(StringUtils.defaultIfEmpty(implementationDescription, ""));
serverConfiguration.setServerName(StringUtils.defaultIfEmpty(serverName, ""));
serverConfiguration.setServerVersion(StringUtils.defaultIfEmpty(serverVersion, ""));
}
/**
* This method will set the conformance during the postconstruct phase. The method {@link AbstractJaxRsConformanceProvider#getProviders()} is used to get all the resource providers include in the
* conformance
*/
@PostConstruct
protected void setUpPostConstruct() {
for (Entry<Class<? extends IResourceProvider>, IResourceProvider> provider : getProviders().entrySet()) {
addProvider(provider.getValue(), provider.getKey()); 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());
} }
serverConfiguration.setServerBindings(serverBindings); serverConfiguration.setServerBindings(serverBindings);
serverConfiguration.setResourceBindings(new LinkedList<ResourceBinding>(myResourceNameToBinding.values())); serverConfiguration.setResourceBindings(new LinkedList<ResourceBinding>(myResourceNameToBinding.values()));
HardcodedServerAddressStrategy hardcodedServerAddressStrategy = new HardcodedServerAddressStrategy(); HardcodedServerAddressStrategy hardcodedServerAddressStrategy = new HardcodedServerAddressStrategy();
hardcodedServerAddressStrategy.setValue(getBaseForServer()); hardcodedServerAddressStrategy.setValue(getBaseForServer());
serverConfiguration.setServerAddressStrategy(hardcodedServerAddressStrategy); serverConfiguration.setServerAddressStrategy(hardcodedServerAddressStrategy);
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
ServerConformanceProvider serverConformanceProvider = new ServerConformanceProvider(serverConfiguration);
serverConformanceProvider.initializeOperations();
myDstu3Conformance = serverConformanceProvider.getServerConformance(null);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider serverConformanceProvider = new ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider(serverConfiguration);
serverConformanceProvider.initializeOperations();
myDstu2Conformance = serverConformanceProvider.getServerConformance(null);
}
}
/**
* This method must return all the resource providers which need to be included in the conformance
* @return a map of the resource providers and their corresponding classes. This class needs to be given
* explicitly because retrieving the interface using {@link Object#getClass()} may not give the correct
* interface in a jee environment.
*/
protected abstract ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> getProviders();
/**
* This method will retrieve the conformance using the http OPTIONS method
* @return the response containing the conformance
*/
@OPTIONS
@Path("/metadata")
public Response conformanceUsingOptions() throws IOException {
return conformance();
}
/**
* This method will retrieve the conformance using the http GET method
* @return the response containing the conformance
*/
@GET
@Path("/metadata")
public Response conformance() throws IOException {
Builder request = getRequest(RequestTypeEnum.OPTIONS, RestOperationTypeEnum.METADATA);
IRestfulResponse response = request.build().getResponse();
response.addHeader(Constants.HEADER_CORS_ALLOW_ORIGIN, "*");
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
return (Response) response.returnResponse(ParseAction.create(myDstu3Conformance), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName()); ServerConformanceProvider serverConformanceProvider = new ServerConformanceProvider(serverConfiguration);
serverConformanceProvider.initializeOperations();
myDstu3Conformance = serverConformanceProvider.getServerConformance(null);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) { } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
return (Response) response.returnResponse(ParseAction.create(myDstu2Conformance), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName()); ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider serverConformanceProvider = new ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider(serverConfiguration);
} serverConformanceProvider.initializeOperations();
myDstu2Conformance = serverConformanceProvider.getServerConformance(null);
}
}
/**
* This method must return all the resource providers which need to be included in the conformance
*
* @return a map of the resource providers and their corresponding classes. This class needs to be given explicitly because retrieving the interface using {@link Object#getClass()} may not give the
* correct interface in a jee environment.
*/
protected abstract ConcurrentHashMap<Class<? extends IResourceProvider>, IResourceProvider> getProviders();
/**
* This method will retrieve the conformance using the http OPTIONS method
*
* @return the response containing the conformance
*/
@OPTIONS
@Path("/metadata")
public Response conformanceUsingOptions() throws IOException {
return conformance();
}
/**
* This method will retrieve the conformance using the http GET method
*
* @return the response containing the conformance
*/
@GET
@Path("/metadata")
public Response conformance() throws IOException {
Builder request = getRequest(RequestTypeEnum.OPTIONS, RestOperationTypeEnum.METADATA);
IRestfulResponse response = request.build().getResponse();
response.addHeader(Constants.HEADER_CORS_ALLOW_ORIGIN, "*");
IBaseResource conformance = null;
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
conformance = myDstu3Conformance;
// return (Response) response.returnResponse(ParseAction.create(myDstu3Conformance), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName());
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
conformance = myDstu2Conformance;
// return (Response) response.returnResponse(ParseAction.create(myDstu2Conformance), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName());
}
if (conformance != null) {
Set<SummaryEnum> summaryMode = Collections.emptySet();
return (Response) response.streamResponseAsResource(conformance, false, summaryMode, Constants.STATUS_HTTP_200_OK, true, false);
}
return (Response) response.returnResponse(null, Constants.STATUS_HTTP_500_INTERNAL_ERROR, true, null, getResourceType().getSimpleName()); return (Response) response.returnResponse(null, Constants.STATUS_HTTP_500_INTERNAL_ERROR, true, null, getResourceType().getSimpleName());
} }
/** /**
* This method will add a provider to the conformance. This method is almost an exact copy of {@link ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods } * This method will add a provider to the conformance. This method is almost an exact copy of {@link ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods }
* @param theProvider an instance of the provider interface *
* @param theProviderInterface the class describing the providers interface * @param theProvider
* @return the numbers of basemethodbindings added * an instance of the provider interface
* @see ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods * @param theProviderInterface
*/ * the class describing the providers interface
public int addProvider(IResourceProvider theProvider, Class<? extends IResourceProvider> theProviderInterface) throws ConfigurationException { * @return the numbers of basemethodbindings added
int count = 0; * @see ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods
*/
public int addProvider(IResourceProvider theProvider, Class<? extends IResourceProvider> theProviderInterface) throws ConfigurationException {
int count = 0;
for (Method m : ReflectionUtil.getDeclaredMethods(theProviderInterface)) { 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;
} }
count++; count++;
// if (foundMethodBinding instanceof ConformanceMethodBinding) { // if (foundMethodBinding instanceof ConformanceMethodBinding) {
// myServerConformanceMethod = foundMethodBinding; // myServerConformanceMethod = foundMethodBinding;
// continue; // continue;
// } // }
if (!Modifier.isPublic(m.getModifiers())) { if (!Modifier.isPublic(m.getModifiers())) {
throw new ConfigurationException("Method '" + m.getName() + "' is not public, FHIR RESTful methods must be public"); throw new ConfigurationException("Method '" + m.getName() + "' is not public, FHIR RESTful methods must be public");
} else { } else {
if (Modifier.isStatic(m.getModifiers())) { if (Modifier.isStatic(m.getModifiers())) {
throw new ConfigurationException("Method '" + m.getName() + "' is static, FHIR RESTful methods must not be static"); throw new ConfigurationException("Method '" + m.getName() + "' is static, FHIR RESTful methods must not be static");
} else { } else {
ourLog.debug("Scanning public method: {}#{}", theProvider.getClass(), m.getName()); ourLog.debug("Scanning public method: {}#{}", theProvider.getClass(), m.getName());
String resourceName = foundMethodBinding.getResourceName(); String resourceName = foundMethodBinding.getResourceName();
ResourceBinding resourceBinding; ResourceBinding resourceBinding;
if (resourceName == null) { if (resourceName == null) {
resourceBinding = myServerBinding; resourceBinding = myServerBinding;
} else { } else {
RuntimeResourceDefinition definition = getFhirContext().getResourceDefinition(resourceName); RuntimeResourceDefinition definition = getFhirContext().getResourceDefinition(resourceName);
if (myResourceNameToBinding.containsKey(definition.getName())) { if (myResourceNameToBinding.containsKey(definition.getName())) {
resourceBinding = myResourceNameToBinding.get(definition.getName()); resourceBinding = myResourceNameToBinding.get(definition.getName());
} else { } else {
resourceBinding = new ResourceBinding(); resourceBinding = new ResourceBinding();
resourceBinding.setResourceName(resourceName); resourceBinding.setResourceName(resourceName);
myResourceNameToBinding.put(resourceName, resourceBinding); myResourceNameToBinding.put(resourceName, resourceBinding);
} }
} }
List<Class<?>> allowableParams = foundMethodBinding.getAllowableParamAnnotations(); List<Class<?>> allowableParams = foundMethodBinding.getAllowableParamAnnotations();
if (allowableParams != null) { if (allowableParams != null) {
for (Annotation[] nextParamAnnotations : m.getParameterAnnotations()) { for (Annotation[] nextParamAnnotations : m.getParameterAnnotations()) {
for (Annotation annotation : nextParamAnnotations) { for (Annotation annotation : nextParamAnnotations) {
Package pack = annotation.annotationType().getPackage(); Package pack = annotation.annotationType().getPackage();
if (pack.equals(IdParam.class.getPackage())) { if (pack.equals(IdParam.class.getPackage())) {
if (!allowableParams.contains(annotation.annotationType())) { if (!allowableParams.contains(annotation.annotationType())) {
throw new ConfigurationException("Method[" + m.toString() + "] is not allowed to have a parameter annotated with " + annotation); throw new ConfigurationException("Method[" + m.toString() + "] is not allowed to have a parameter annotated with " + annotation);
} }
} }
} }
} }
} }
resourceBinding.addMethod(foundMethodBinding); resourceBinding.addMethod(foundMethodBinding);
ourLog.debug(" * Method: {}#{} is a handler", theProvider.getClass(), m.getName()); ourLog.debug(" * Method: {}#{} is a handler", theProvider.getClass(), m.getName());
} }
} }
} }
return count; return count;
} }
@SuppressWarnings("unchecked")
@Override
public Class<IBaseResource> getResourceType() {
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
return Class.class.cast(Conformance.class);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
return Class.class.cast(ca.uhn.fhir.model.dstu2.resource.Conformance.class);
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public Class<IBaseResource> getResourceType() {
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
return Class.class.cast(Conformance.class);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
return Class.class.cast(ca.uhn.fhir.model.dstu2.resource.Conformance.class);
}
return null;
}
} }

View File

@ -100,15 +100,15 @@ public class JaxRsResponseDstu3Test {
@Test @Test
public void testReturnResponse() throws IOException { public void testReturnResponse() throws IOException {
IdType theId = new IdType(15L); IdType theId = new IdType(15L);
ParseAction<?> outcome = ParseAction.create(createPatient());
int operationStatus = 200; int operationStatus = 200;
boolean allowPrefer = true; boolean allowPrefer = true;
String resourceName = "Patient"; String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(theId); MethodOutcome methodOutcome = new MethodOutcome(theId);
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName); boolean addContentLocationHeader = true;
boolean respondGzip = true;
Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), createPatient(), theSummaryMode, 200, addContentLocationHeader, respondGzip, this.request);
assertEquals(200, result.getStatus()); assertEquals(200, result.getStatus());
assertEquals(Constants.CT_JSON+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); assertEquals("application/json+fhir; charset=UTF-8", result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
System.out.println(result.getEntity().toString());
assertTrue(result.getEntity().toString().contains("resourceType\":\"Patient")); assertTrue(result.getEntity().toString().contains("resourceType\":\"Patient"));
assertTrue(result.getEntity().toString().contains("15")); assertTrue(result.getEntity().toString().contains("15"));
@ -117,30 +117,32 @@ public class JaxRsResponseDstu3Test {
@Test @Test
public void testReturnResponseAsXml() throws IOException { public void testReturnResponseAsXml() throws IOException {
IdType theId = new IdType(15L); IdType theId = new IdType(15L);
ParseAction<?> outcome = ParseAction.create(createPatient());
int operationStatus = 200; int operationStatus = 200;
boolean allowPrefer = true; boolean allowPrefer = true;
String resourceName = "Patient"; String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(theId); MethodOutcome methodOutcome = new MethodOutcome(theId);
response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML}); response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML});
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName); boolean addContentLocationHeader = true;
boolean respondGzip = true;
Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), createPatient(), theSummaryMode, 200, addContentLocationHeader, respondGzip, this.request);
assertEquals(200, result.getStatus()); assertEquals(200, result.getStatus());
assertEquals(Constants.CT_XML+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); assertEquals("application/xml+fhir; charset=UTF-8", result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
assertTrue(result.getEntity().toString().contains("<Patient")); assertTrue(result.getEntity().toString().contains("<Patient"));
assertTrue(result.getEntity().toString().contains("15")); assertTrue(result.getEntity().toString().contains("15"));
} }
@Test @Test
public void testNoOutcomeXml() throws IOException { public void testNoOutcomeXml() throws IOException {
ParseAction<?> outcome = ParseAction.create((IBaseResource) null);
int operationStatus = Constants.STATUS_HTTP_204_NO_CONTENT; int operationStatus = Constants.STATUS_HTTP_204_NO_CONTENT;
boolean allowPrefer = true; boolean allowPrefer = true;
String resourceName = "Patient"; String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(null); MethodOutcome methodOutcome = new MethodOutcome(null);
response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML}); response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML});
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName); boolean addContentLocationHeader = true;
boolean respondGzip = true;
Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), null, theSummaryMode, 204, addContentLocationHeader, respondGzip, this.request);
assertEquals(204, result.getStatus()); assertEquals(204, result.getStatus());
assertEquals(Constants.CT_XML+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); assertEquals(null, result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
} }
private Bundle getSinglePatientResource() { private Bundle getSinglePatientResource() {

View File

@ -11,7 +11,6 @@ import java.util.Set;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.hl7.fhir.instance.model.api.IBaseBinary; import org.hl7.fhir.instance.model.api.IBaseBinary;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -19,9 +18,7 @@ import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Binary; import ca.uhn.fhir.model.dstu2.resource.Binary;
import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.SummaryEnum; import ca.uhn.fhir.rest.api.SummaryEnum;
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.RestfulServerUtils; import ca.uhn.fhir.rest.server.RestfulServerUtils;
@ -42,7 +39,6 @@ public class JaxRsResponseTest {
@Test @Test
public void testGetResponseWriterNoZipNoBrowser() throws IOException { public void testGetResponseWriterNoZipNoBrowser() throws IOException {
boolean theRequestIsBrowser = false;
boolean respondGzip = false; boolean respondGzip = false;
Set<SummaryEnum> theSummaryMode = Collections.<SummaryEnum>emptySet(); Set<SummaryEnum> theSummaryMode = Collections.<SummaryEnum>emptySet();
Response result = (Response) RestfulServerUtils.streamResponseAsBundle(request.getServer(), bundle, theSummaryMode, respondGzip, request); Response result = (Response) RestfulServerUtils.streamResponseAsBundle(request.getServer(), bundle, theSummaryMode, respondGzip, request);
@ -54,7 +50,6 @@ public class JaxRsResponseTest {
@Test @Test
public void testSendAttachmentResponse() throws IOException { public void testSendAttachmentResponse() throws IOException {
boolean theRequestIsBrowser = true;
boolean respondGzip = true; boolean respondGzip = true;
IBaseBinary binary = new Binary(); IBaseBinary binary = new Binary();
String contentType = "foo"; String contentType = "foo";
@ -70,7 +65,6 @@ public class JaxRsResponseTest {
@Test @Test
public void testSendAttachmentResponseNoContent() throws IOException { public void testSendAttachmentResponseNoContent() throws IOException {
boolean theRequestIsBrowser = true;
boolean respondGzip = true; boolean respondGzip = true;
IBaseBinary binary = new Binary(); IBaseBinary binary = new Binary();
binary.setContent(new byte[]{}); binary.setContent(new byte[]{});
@ -83,7 +77,6 @@ public class JaxRsResponseTest {
@Test @Test
public void testSendAttachmentResponseEmptyContent() throws IOException { public void testSendAttachmentResponseEmptyContent() throws IOException {
boolean theRequestIsBrowser = true;
boolean respondGzip = true; boolean respondGzip = true;
IBaseBinary binary = new Binary(); IBaseBinary binary = new Binary();
boolean theAddContentLocationHeader = false; boolean theAddContentLocationHeader = false;
@ -96,15 +89,12 @@ public class JaxRsResponseTest {
@Test @Test
public void testReturnResponse() throws IOException { public void testReturnResponse() throws IOException {
IdDt theId = new IdDt(15L); boolean addContentLocationHeader = true;
ParseAction<?> outcome = ParseAction.create(createPatient()); boolean respondGzip = true;
int operationStatus = 200; // Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName);
boolean allowPrefer = true; Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), createPatient(), theSummaryMode, 200, addContentLocationHeader, respondGzip, this.request);
String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(theId);
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName);
assertEquals(200, result.getStatus()); assertEquals(200, result.getStatus());
assertEquals(Constants.CT_JSON+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); assertEquals("application/json+fhir; charset=UTF-8", result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
System.out.println(result.getEntity().toString()); System.out.println(result.getEntity().toString());
assertTrue(result.getEntity().toString().contains("resourceType\":\"Patient")); assertTrue(result.getEntity().toString().contains("resourceType\":\"Patient"));
assertTrue(result.getEntity().toString().contains("15")); assertTrue(result.getEntity().toString().contains("15"));
@ -113,31 +103,24 @@ public class JaxRsResponseTest {
@Test @Test
public void testReturnResponseAsXml() throws IOException { public void testReturnResponseAsXml() throws IOException {
IdDt theId = new IdDt(15L);
ParseAction<?> outcome = ParseAction.create(createPatient());
int operationStatus = 200;
boolean allowPrefer = true;
String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(theId);
response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML}); response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML});
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName); boolean addContentLocationHeader = true;
boolean respondGzip = true;
Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), createPatient(), theSummaryMode, 200, addContentLocationHeader, respondGzip, this.request);
assertEquals(200, result.getStatus()); assertEquals(200, result.getStatus());
assertEquals(Constants.CT_XML+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); assertEquals("application/xml+fhir; charset=UTF-8", result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
assertTrue(result.getEntity().toString().contains("<Patient")); assertTrue(result.getEntity().toString().contains("<Patient"));
assertTrue(result.getEntity().toString().contains("15")); assertTrue(result.getEntity().toString().contains("15"));
} }
@Test @Test
public void testNoOutcomeXml() throws IOException { public void testNoOutcomeXml() throws IOException {
ParseAction<?> outcome = ParseAction.create((IBaseResource) null);
int operationStatus = Constants.STATUS_HTTP_204_NO_CONTENT;
boolean allowPrefer = true;
String resourceName = "Patient";
MethodOutcome methodOutcome = new MethodOutcome(null);
response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML}); response.getRequestDetails().getParameters().put(Constants.PARAM_FORMAT, new String[]{Constants.CT_XML});
Response result = response.returnResponse(outcome, operationStatus, allowPrefer, methodOutcome, resourceName); boolean addContentLocationHeader = true;
assertEquals(204, result.getStatus()); boolean respondGzip = true;
assertEquals(Constants.CT_XML+Constants.CHARSET_UTF8_CTSUFFIX, result.getHeaderString(Constants.HEADER_CONTENT_TYPE)); Response result = (Response) RestfulServerUtils.streamResponseAsResource(request.getServer(), createPatient(), theSummaryMode, 200, addContentLocationHeader, respondGzip, this.request);
assertEquals(200, result.getStatus());
assertEquals("application/xml+fhir; charset=UTF-8", result.getHeaderString(Constants.HEADER_CONTENT_TYPE));
} }
private Bundle getSinglePatientResource() { private Bundle getSinglePatientResource() {