Encoding a Binary resource without a content type set should not result

in a NullPointerException.
This commit is contained in:
James Agnew 2014-12-03 13:46:46 -05:00
parent 028c349d10
commit 3ca9fbbebb
7 changed files with 169 additions and 31 deletions

View File

@ -618,7 +618,9 @@ public class XmlParser extends BaseParser implements IParser {
if (theResource instanceof Binary) {
Binary bin = (Binary) theResource;
theEventWriter.writeAttribute("contentType", bin.getContentType());
if (bin.getContentType() != null) {
theEventWriter.writeAttribute("contentType", bin.getContentType());
}
theEventWriter.writeCharacters(bin.getContentAsBase64());
} else {
encodeCompositeElementToStreamWriter(resDef, theResource, theResource, theEventWriter, resDef, theIncludedResource);

View File

@ -93,6 +93,17 @@ public class XmlParserTest {
ourCtx.newXmlParser().parseResource(Profile.class, content);
}
@Test
public void testEncodeBinaryWithNoContentType() {
Binary b = new Binary();
b.setContent(new byte[] {1,2,3,4});
String output = ourCtx.newXmlParser().encodeResourceToString(b);
ourLog.info(output);
assertEquals("<Binary xmlns=\"http://hl7.org/fhir\">AQIDBA==</Binary>", output);
}
@Test
public void testEncodeNonContained() {

View File

@ -12,7 +12,15 @@ public class Example02_SimpleRestfulServer extends RestfulServer {
@Override
protected void initialize() throws ServletException {
// Set the resource providers used by this server
setResourceProviders(new Example02_PatientResourceProvider());
/* This just means to use Content-Types which are not technically
* FHIR compliant if a browser is detected (so that they display
* nicely for testing) */
setUseBrowserFriendlyContentTypes(true);
}

View File

@ -0,0 +1,78 @@
package ca.uhn.fhir.example.ex3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
/**
* This is the most basic resource provider, showing only a single
* read method on a resource provider
*/
public class Example03_PatientResourceProvider implements IResourceProvider {
private Map<Long, Patient> myPatients = new HashMap<Long, Patient>();
private long myNextId = 1;
/** Constructor */
public Example03_PatientResourceProvider() {
Patient pat1 = new Patient();
pat1.addIdentifier().setSystem("http://acme.com/MRNs").setValue("7000135");
pat1.addName().addFamily("Simpson").addGiven("Homer").addGiven("J");
myPatients.put(myNextId++, pat1);
}
/** All Resource Providers must implement this method */
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
/** Simple implementation of the "read" method */
@Read()
public Patient read(@IdParam IdDt theId) {
Patient retVal = myPatients.get(theId.getIdPartAsLong());
if (retVal == null) {
throw new ResourceNotFoundException(theId);
}
return retVal;
}
/** Simple implementation of the "create" method */
@Create
public MethodOutcome create(@ResourceParam Patient thePatient) {
// Give the resource the next sequential ID
long id = myNextId++;
thePatient.setId(new IdDt(id));
// Store the resource in memory
myPatients.put(id, thePatient);
// Inform the server of the ID for the newly stored resource
return new MethodOutcome(thePatient.getId());
}
/** FHIR "Search" operation for Patient resources */
@Search
public List<Patient> search() {
List<Patient> retVal = new ArrayList<Patient>();
retVal.addAll(myPatients.values());
return retVal;
}
}

View File

@ -0,0 +1,27 @@
package ca.uhn.fhir.example.ex3;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import ca.uhn.fhir.rest.server.RestfulServer;
@WebServlet("/example03/*")
public class Example03_SimpleRestfulServer extends RestfulServer {
private static final long serialVersionUID = 1L;
@Override
protected void initialize() throws ServletException {
// Set the resource providers used by this server
setResourceProviders(new Example03_PatientResourceProvider());
/* This just means to use Content-Types which are not technically
* FHIR compliant if a browser is detected (so that they display
* nicely for testing) */
setUseBrowserFriendlyContentTypes(true);
}
}

View File

@ -1,10 +1,13 @@
package ca.uhn.example;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hamcrest.core.StringContains;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
@ -24,51 +27,57 @@ public class ExampleTest {
if (ourServer != null) {
ourServer.stop();
}
System.clearProperty("ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS");
}
/**
* Tests here have some weird windows inconsistency relating to the path for finding the WAR file. Since this test isn't really important to work multiplatform, we can skip it
*/
public static boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
public static boolean isWindows()
{
return System.getProperty("os.name").startsWith("Windows");
}
@Test
public void test01Search() throws Exception {
if (isWindows()) {
/*
* Tests here have some weird windows inconsistency relating to the path for finding the WAR file.
* Since this test isn't really important to work multiplatform, we can skip it
*/
return;
}
Bundle results = ourClient.search().forResource(Patient.class).execute();
assertEquals(1, results.size());
}
@Test
public void test02Read() throws Exception {
if (isWindows()) {
return;
}
Patient results = ourClient.read(Patient.class, "1");
assertThat(results.getNameFirstRep().getGivenAsSingleString(), StringContains.containsString("PatientOne"));
}
@BeforeClass
public static void beforeClass() throws Exception {
if (isWindows()) {
return;
}
beforeClass();
Bundle results = ourClient.search().forResource(Patient.class).execute();
assertEquals(1, results.size());
}
/**
* Not annotated with @BeforeClass so that we can skip if we're not running tests here
*/
public static void beforeClass() throws Exception {
if (ourPort != null) {
return;
}
ourPort = RandomServerPortProvider.findFreePort();
ourServer = new Server(ourPort);
String base = "http://localhost:" + ourPort+"/fhir";
System.setProperty("ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS", "example , Restful Server Example , " + base);
String base = "http://localhost:" + ourPort + "/fhir";
System.setProperty("ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS", "example , Restful Server Example , " + base);
WebAppContext root = new WebAppContext();
root.setAllowDuplicateFragmentNames(true);
root.setWar("file:../restful-server-example/target/restful-server-example.war");
root.setContextPath("/");
root.setAttribute(WebAppContext.BASETEMPDIR, "target/tempextrtact");
@ -83,7 +92,6 @@ public class ExampleTest {
ourCtx = new FhirContext();
ourClient = ourCtx.newRestfulGenericClient(base);
}
}

View File

@ -156,6 +156,10 @@
were applied to other client instances for the same client interface as well. (Issue
did not affect generic/fluent clients)
</action>
<action type="fix">
Encoding a Binary resource without a content type set should not result in a NullPointerException. Thanks
to Alexander Kley for reporting!
</action>
</release>
<release version="0.7" date="2014-Oct-23">
<action type="add" issue="30">