Commit 1182ff65 authored by R.W.Majeed's avatar R.W.Majeed

changed output to javax.activation.DataSource

parent 64353b67
RESTeasy doesn't output javax.xml.transform.Source,
check why, e.g. https://github.com/resteasy/Resteasy/blob/master/jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/plugins/providers/SourceProvider.java
Allow delete of complete patients via FHIR
e.g. DELETE [base]Patient
......
......@@ -9,10 +9,8 @@ import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
......@@ -56,14 +54,12 @@ public class Binary implements ExternalInterface{
@POST
@Path("$validate")
@Produces(MediaType.APPLICATION_XML)
public Response validate(Source doc){
log.info("Validation requested");
return Response.serverError().entity("Not implemented").build();
}
@POST
@Produces({FhirConstants.MEDIA_TYPE_XML_FHIR,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
public Response create(Source doc){
ValidationResult vr = null;
SimplifiedOperationOutcome outcome = new SimplifiedOperationOutcome();
......@@ -144,7 +140,6 @@ public class Binary implements ExternalInterface{
}
@GET
@Produces({FhirConstants.MEDIA_TYPE_XML_FHIR,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
public Response search(@QueryParam("_id") String docId, @QueryParam("_lastUpdated") String lastUpdated, @QueryParam("_tag") String tag, @QueryParam("_count") Integer count){
log.info("search via GET");
try {
......
......@@ -5,7 +5,6 @@ import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
......@@ -15,12 +14,12 @@ import javax.ws.rs.core.Response;
public class ConformanceStatement {
private static final Logger log = Logger.getLogger(ConformanceStatement.class.getName());
private Response read(){
return Response.ok(getClass().getResourceAsStream("/fhir/conformance.xml"),MediaType.TEXT_XML_TYPE).build();
private Response read(){
return Response.ok(getClass().getResourceAsStream("/fhir/conformance.xml"),MediaType.TEXT_XML).build();
}
@OPTIONS
@Produces({FhirConstants.MEDIA_TYPE_XML_FHIR,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
public Response options(){
log.info("FHIR Conformance requested via OPTIONS [base]");
return read();
......@@ -28,7 +27,6 @@ public class ConformanceStatement {
@GET
@Path("metadata")
@Produces({FhirConstants.MEDIA_TYPE_XML_FHIR,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
public Response metadata(){
log.info("FHIR Conformance requested via GET [base]/metadata");
return read();
......
package org.aktin.cda.etl.fhir;
import javax.ws.rs.core.MediaType;
public class FhirConstants {
public static final String MEDIA_TYPE_XML_FHIR = "application/xml+fhir";
public static final String[] MEDIA_TYPES_XML = {MEDIA_TYPE_XML_FHIR, MediaType.APPLICATION_XML, MediaType.TEXT_XML};
}
package org.aktin.cda.etl.fhir;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class FhirDomSource implements DataSource {
private Document doc;
public FhirDomSource(Document doc){
this.doc = doc;
}
@Override
public String getContentType() {
return FhirConstants.MEDIA_TYPE_XML_FHIR;
}
@Override
public InputStream getInputStream() throws IOException {
// TODO is there way to use XMLStreamReader to construct an on demand InputStream?
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
try {
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
} catch (TransformerException | TransformerFactoryConfigurationError e) {
throw new IOException("Unable transform XML to byte array", e);
}
return new ByteArrayInputStream(outputStream.toByteArray());
}
@Override
public String getName() {
return doc.getDocumentElement().getTagName();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
}
......@@ -3,14 +3,13 @@ package org.aktin.cda.etl.fhir;
import java.util.ArrayList;
import java.util.List;
import javax.activation.DataSource;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
......@@ -114,7 +113,7 @@ public class SimplifiedOperationOutcome {
* @return XML source
* @throws XMLStreamException any XML error
*/
public Source generateXml(XMLOutputFactory factory, DocumentBuilder documentBuilder) throws XMLStreamException{
public DataSource generateXml(XMLOutputFactory factory, DocumentBuilder documentBuilder) throws XMLStreamException{
Document doc = documentBuilder.newDocument();
DOMResult res = new DOMResult(doc);
XMLStreamWriter writer = factory.createXMLStreamWriter(res);
......@@ -125,10 +124,14 @@ public class SimplifiedOperationOutcome {
// writer.writeDefaultNamespace(FHIR_NAMESPACE);
for( Issue issue : issues ){
writer.writeStartElement("issue");
// severity
writer.writeStartElement("severity");
writer.writeAttribute("value", issue.severity.name());
writer.writeEndElement();
// code
writer.writeStartElement("code");
writer.writeAttribute("value", issue.code.value);
writer.writeEndElement();
if( issue.details != null ){
writer.writeStartElement("details");
......@@ -140,8 +143,7 @@ public class SimplifiedOperationOutcome {
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
DOMSource result = new DOMSource(doc);
return result;
return new FhirDomSource(doc);
}
public static SimplifiedOperationOutcome create(Severity severity, String message){
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment