Skip to content

Commit

Permalink
Handle Runtime Exception for all available RESTful services by sendin…
Browse files Browse the repository at this point in the history
…g the stack trace in response to the UI
  • Loading branch information
vrindanayak committed Nov 21, 2018
1 parent 8a1f221 commit fb69d80
Show file tree
Hide file tree
Showing 45 changed files with 1,825 additions and 988 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private StreamingOutput writeJsonArray(String[] values) {
}

private EnumSet<DicomConfiguration.Option> options() {
ArchiveDeviceExtension arcDev = device.getDeviceExtension(ArchiveDeviceExtension.class);
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
EnumSet<DicomConfiguration.Option> options = EnumSet.of(
DicomConfiguration.Option.PRESERVE_VENDOR_DATA,
DicomConfiguration.Option.PRESERVE_CERTIFICATE,
Expand All @@ -283,20 +283,10 @@ private EnumSet<DicomConfiguration.Option> options() {
@Consumes("application/json")
public void createDevice(@PathParam("DeviceName") String deviceName, Reader content) {
logRequest();
Device device = toDevice(deviceName, content);
try {
Device device = jsonConf.loadDeviceFrom(Json.createParser(content), configDelegate);
if (!device.getDeviceName().equals(deviceName))
throw new IllegalArgumentException(
"Device name in content[" + device.getDeviceName() + "] does not match Device name in URL");
ConfigurationChanges diffs = conf.persist(device, options());
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (JsonParsingException e) {
throw new WebApplicationException(
errResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.BAD_REQUEST));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
Expand All @@ -309,21 +299,11 @@ public void createDevice(@PathParam("DeviceName") String deviceName, Reader cont
@Consumes("application/json")
public void updateDevice(@PathParam("DeviceName") String deviceName, Reader content) {
logRequest();
Device device = toDevice(deviceName, content);
try {
Device device = jsonConf.loadDeviceFrom(Json.createParser(content), configDelegate);
if (!device.getDeviceName().equals(deviceName))
throw new IllegalArgumentException(
"Device name in content[" + device.getDeviceName() + "] does not match Device name in URL");
ConfigurationChanges diffs = conf.merge(device, options());
if (!diffs.isEmpty())
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (JsonParsingException e) {
throw new WebApplicationException(
errResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.BAD_REQUEST));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
Expand Down Expand Up @@ -386,7 +366,7 @@ public void unregisterHL7App(@PathParam("appName") String appName) {
throw new WebApplicationException(errResponse(
"HL7 Application " + appName + " not registered.", Response.Status.NOT_FOUND));
hl7Conf.unregisterHL7Application(appName);
} catch (Exception e) {
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}
Expand Down Expand Up @@ -424,7 +404,8 @@ public Response getVendorData(@PathParam("deviceName") String deviceName) {
} catch (Exception e) {
return errResponseAsTextPlain(e);
}
return Response.ok(content).status(status).type("application/zip").header("Content-Disposition", "attachment; filename=vendordata.zip").build();
return Response.ok(content).status(status).type("application/zip")
.header("Content-Disposition", "attachment; filename=vendordata.zip").build();
}

@PUT
Expand Down Expand Up @@ -460,6 +441,28 @@ public Response deleteVendorData(@PathParam("deviceName") String deviceName) {
return Response.ok().status(Response.Status.NO_CONTENT).build();
}

private Device toDevice(String deviceName, Reader content) {
Device device;
try {
device = jsonConf.loadDeviceFrom(Json.createParser(content), configDelegate);
} catch (JsonParsingException e) {
throw new WebApplicationException(
errResponse(e.getMessage() + " at location : " + e.getLocation(),
Response.Status.BAD_REQUEST));
} catch (ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}

if (!device.getDeviceName().equals(deviceName))
throw new WebApplicationException(errResponse(
"Device name in content[" + device.getDeviceName() + "] does not match Device name in URL",
Response.Status.BAD_REQUEST));

return device;
}

private static DeviceInfo toDeviceInfo(UriInfo info) {
DeviceInfo deviceInfo = new DeviceInfo();
for (Map.Entry<String, List<String>> entry : info.getQueryParameters().entrySet()) {
Expand Down Expand Up @@ -605,9 +608,15 @@ private Response errResponse(Object errorMessage, Response.Status status) {
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(exceptionAsString).type("text/plain").build();
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Comparator;

/**
Expand All @@ -80,14 +84,18 @@ public class QueryAETs {
@Produces("application/json")
public StreamingOutput query() {
LOG.info("Process GET {} from {}@{}", request.getRequestURI(), request.getRemoteUser(), request.getRemoteHost());
return out -> {
try {
return out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
for (ApplicationEntity ae : sortedApplicationEntities())
writeTo(ae, gen);
gen.writeEnd();
gen.flush();
};
};
} catch (Exception e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}

private void writeTo(ApplicationEntity ae, JsonGenerator gen) {
Expand Down Expand Up @@ -117,4 +125,17 @@ private ApplicationEntity[] sortedApplicationEntities() {
.sorted(Comparator.comparing(ApplicationEntity::getAETitle))
.toArray(ApplicationEntity[]::new);
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
* @author Gunter Zeilinger <gunterze@gmail.com>
Expand Down Expand Up @@ -98,10 +100,25 @@ public StreamingOutput getAttributeFilter(@PathParam("Entity") String entityName
};
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (Exception e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}

private static Response errResponse(String errorMessage, Response.Status status) {
return Response.status(status).entity("{\"errorMessage\":\"" + errorMessage + "\"}").build();
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

/**
Expand Down Expand Up @@ -103,6 +105,8 @@ public StreamingOutput listAttributeSets(@PathParam("type") String type) {
throw new WebApplicationException(
Response.status(Response.Status.NOT_FOUND)
.entity("Attribute Set of type : " + type + " not found.").build());
} catch (Exception e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}

Expand All @@ -112,4 +116,17 @@ private AttributeSet[] sortedAttributeSets(Map<String, AttributeSet> attrSets) {
.sorted(Comparator.comparing(AttributeSet::getID))
.toArray(AttributeSet[]::new);
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
* @author Gunter Zeilinger <gunterze@gmail.com>
Expand All @@ -77,19 +81,36 @@ public class QueryDeviceName {
@Produces("application/json")
public StreamingOutput devicename() {
LOG.info("Process GET {} from {}@{}", request.getRequestURI(), request.getRemoteUser(), request.getRemoteHost());
ArchiveDeviceExtension arcDev = device.getDeviceExtension(ArchiveDeviceExtension.class);
return out -> {
JsonGenerator gen = Json.createGenerator(out);
JsonWriter writer = new JsonWriter(gen);
gen.writeStartObject();
writer.writeNotNullOrDef("dicomDeviceName", device.getDeviceName(), null);
if (arcDev != null) {
writer.writeNotNullOrDef("xRoad", arcDev.hasXRoadProperties(), false);
writer.writeNotNullOrDef("impaxReport", arcDev.hasImpaxReportProperties(), false);
writer.writeNotNullOrDef("UIConfigurationDeviceName", arcDev.getUiConfigurationDeviceName(), null);
}
gen.writeEnd();
gen.flush();
};
try {
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
return out -> {
JsonGenerator gen = Json.createGenerator(out);
JsonWriter writer = new JsonWriter(gen);
gen.writeStartObject();
writer.writeNotNullOrDef("dicomDeviceName", device.getDeviceName(), null);
if (arcDev != null) {
writer.writeNotNullOrDef("xRoad", arcDev.hasXRoadProperties(), false);
writer.writeNotNullOrDef("impaxReport", arcDev.hasImpaxReportProperties(), false);
writer.writeNotNullOrDef("UIConfigurationDeviceName", arcDev.getUiConfigurationDeviceName(), null);
}
gen.writeEnd();
gen.flush();
};
} catch (Exception e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Comparator;

/**
Expand All @@ -82,20 +86,37 @@ public class QueryExporters {
@Produces("application/json")
public StreamingOutput query() {
LOG.info("Process GET {} from {}@{}", request.getRequestURI(), request.getRemoteUser(), request.getRemoteHost());
return out -> {
try {
return out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
device.getDeviceExtension(ArchiveDeviceExtension.class).getExporterDescriptors().stream()
device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class).getExporterDescriptors().stream()
.sorted(Comparator.comparing(ExporterDescriptor::getExporterID))
.forEach(exporter -> {
JsonWriter writer = new JsonWriter(gen);
gen.writeStartObject();
writer.writeNotNullOrDef("id", exporter.getExporterID(), null);
writer.writeNotNullOrDef("description", exporter.getDescription(), null);
gen.writeEnd();
});
JsonWriter writer = new JsonWriter(gen);
gen.writeStartObject();
writer.writeNotNullOrDef("id", exporter.getExporterID(), null);
writer.writeNotNullOrDef("description", exporter.getDescription(), null);
gen.writeEnd();
});
gen.writeEnd();
gen.flush();
};
};
} catch (Exception e) {
throw new WebApplicationException(errResponseAsTextPlain(e));
}
}

private Response errResponseAsTextPlain(Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(exceptionAsString(e))
.type("text/plain")
.build();
}

private String exceptionAsString(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}

0 comments on commit fb69d80

Please sign in to comment.