Skip to content

Commit

Permalink
Log query params for all RESTful services (wherever currently missing)
Browse files Browse the repository at this point in the history
…dcm4che#1950

RESTful services : Additionally log exception as stack trace in application logs dcm4che#1947
  • Loading branch information
vrindanayak committed Apr 24, 2019
1 parent 81d0b25 commit eb08a30
Show file tree
Hide file tree
Showing 49 changed files with 863 additions and 732 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Response getDevice(@PathParam("DeviceName") String deviceName) {
w.flush();
}).build();
} catch (ConfigurationNotFoundException e) {
return errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.NOT_FOUND);
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ public void createDevice(@PathParam("DeviceName") String deviceName, Reader cont
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException | WebAppAlreadyExistsException e) {
throw new WebApplicationException(
errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.CONFLICT));
errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -321,7 +321,7 @@ public void updateDevice(@PathParam("DeviceName") String deviceName, Reader cont
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException | WebAppAlreadyExistsException e) {
throw new WebApplicationException(
errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.CONFLICT));
errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -335,9 +335,9 @@ public void registerAET(@PathParam("aet") String aet) {
logRequest();
try {
if (!conf.registerAETitle(aet))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("Application Entity Title " + aet + " already registered."),
Response.Status.CONFLICT));
throw new WebApplicationException(
errResponse("Application Entity Title " + aet + " already registered.",
Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -351,9 +351,9 @@ public void unregisterAET(@PathParam("aet") String aet) {
try {
List<String> aets = Arrays.asList(conf.listRegisteredAETitles());
if (!aets.contains(aet))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("Application Entity Title " + aet + " not registered."),
Response.Status.NOT_FOUND));
throw new WebApplicationException(
errResponse("Application Entity Title " + aet + " not registered.",
Response.Status.NOT_FOUND));
conf.unregisterAETitle(aet);
} catch (Exception e) {
throw new WebApplicationException(
Expand All @@ -369,8 +369,8 @@ public void registerHL7App(@PathParam("appName") String appName) {
try {
HL7Configuration hl7Conf = conf.getDicomConfigurationExtension(HL7Configuration.class);
if (!hl7Conf.registerHL7Application(appName))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("HL7 Application " + appName + " already registered."),
throw new WebApplicationException(
errResponse("HL7 Application " + appName + " already registered.",
Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
Expand All @@ -386,8 +386,8 @@ public void unregisterHL7App(@PathParam("appName") String appName) {
HL7Configuration hl7Conf = conf.getDicomConfigurationExtension(HL7Configuration.class);
List<String> hl7apps = Arrays.asList(hl7Conf.listRegisteredHL7ApplicationNames());
if (!hl7apps.contains(appName))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("HL7 Application " + appName + " not registered."),
throw new WebApplicationException(
errResponse("HL7 Application " + appName + " not registered.",
Response.Status.NOT_FOUND));
hl7Conf.unregisterHL7Application(appName);
} catch (Exception e) {
Expand All @@ -403,8 +403,8 @@ public void registerWebApp(@PathParam("webAppName") String webAppName) {
logRequest();
try {
if (!conf.registerWebAppName(webAppName))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("Web Application " + webAppName + " already registered."),
throw new WebApplicationException(
errResponse("Web Application " + webAppName + " already registered.",
Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
Expand All @@ -419,8 +419,8 @@ public void unregisterWebApp(@PathParam("webAppName") String webAppName) {
try {
List<String> webApps = Arrays.asList(conf.listRegisteredWebAppNames());
if (!webApps.contains(webAppName))
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("Web Application " + webAppName + " not registered."),
throw new WebApplicationException(
errResponse("Web Application " + webAppName + " not registered.",
Response.Status.NOT_FOUND));
conf.unregisterWebAppName(webAppName);
} catch (Exception e) {
Expand All @@ -438,7 +438,7 @@ public void deleteDevice(@PathParam("DeviceName") String deviceName) {
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
throw new WebApplicationException(
errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.NOT_FOUND));
errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -460,7 +460,7 @@ public Response getVendorData(@PathParam("deviceName") String deviceName) {
status = Response.Status.OK;
}
} catch (ConfigurationNotFoundException e) {
return errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.NOT_FOUND);
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -481,7 +481,7 @@ public Response updateVendorData(@PathParam("deviceName") String deviceName, Fil
if (!diffs.isEmpty())
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
return errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.NOT_FOUND);
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -497,7 +497,7 @@ public Response deleteVendorData(@PathParam("deviceName") String deviceName) {
if (!diffs.isEmpty())
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
return errResponseAsTextPlain(errorMessage(e.getMessage()), Response.Status.NOT_FOUND);
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -509,19 +509,19 @@ private Device toDevice(String deviceName, Reader content) {
try {
device = jsonConf.loadDeviceFrom(Json.createParser(content), configDelegate);
} catch (JsonParsingException e) {
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage(e.getMessage() + " at location : " + e.getLocation()), Response.Status.BAD_REQUEST));
throw new WebApplicationException(
errResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.BAD_REQUEST));
} catch (ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage(e.getMessage()), Response.Status.NOT_FOUND));
throw new WebApplicationException(
errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
}

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

return device;
Expand Down Expand Up @@ -667,8 +667,8 @@ private void logRequest() {
request.getRemoteHost());
}

private String errorMessage(String msg) {
return "{\"errorMessage\":\"" + msg + "\"}";
private Response errResponse(String msg, Response.Status status) {
return errResponseAsTextPlain("{\"errorMessage\":\"" + msg + "\"}", status);
}

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
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;
Expand All @@ -82,20 +81,19 @@ public class QueryAETs {
@GET
@NoCache
@Produces("application/json")
public StreamingOutput query() {
public Response query() {
logRequest();
try {
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
for (ApplicationEntity ae : sortedApplicationEntities())
writeTo(ae, gen);
gen.writeEnd();
gen.flush();
};
}).build();
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,24 @@ public class QueryAttributeFilter {
@NoCache
@Path("/{Entity}")
@Produces("application/json")
public StreamingOutput getAttributeFilter(@PathParam("Entity") String entityName) {
public Response getAttributeFilter(@PathParam("Entity") String entityName) {
logRequest();
final Entity entity;
try {
entity = Entity.valueOf(entityName);
final AttributeFilter filter = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class)
.getAttributeFilter(entity);
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
JsonWriter writer = new JsonWriter(gen);
jsonConf.getJsonConfigurationExtension(JsonArchiveConfiguration.class)
.writeAttributeFilter(writer, entity, filter);
gen.flush();
};
}).build();
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage(e.getMessage()), Response.Status.BAD_REQUEST));
return errResponse(e.getMessage(), Response.Status.BAD_REQUEST);
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -116,8 +114,8 @@ private void logRequest() {
request.getRemoteHost());
}

private String errorMessage(String msg) {
return "{\"errorMessage\":\"" + msg + "\"}";
private Response errResponse(String msg, Response.Status status) {
return errResponseAsTextPlain("{\"errorMessage\":\"" + msg + "\"}", status);
}

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public class QueryAttributeSets {
@NoCache
@Path("/{type}")
@Produces("application/json")
public StreamingOutput listAttributeSets(@PathParam("type") String type) {
public Response listAttributeSets(@PathParam("type") String type) {
logRequest();
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
try {
final AttributeSet.Type attrSetType = AttributeSet.Type.valueOf(type);
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
for (AttributeSet attrSet : sortedAttributeSets(arcDev.getAttributeSet(attrSetType))) {
Expand All @@ -98,13 +98,11 @@ public StreamingOutput listAttributeSets(@PathParam("type") String type) {
}
gen.writeEnd();
gen.flush();
};
}).build();
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponseAsTextPlain(
errorMessage("Attribute Set of type : " + type + " not found."), Response.Status.NOT_FOUND));
return errResponse("Attribute Set of type : " + type + " not found.", Response.Status.NOT_FOUND);
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -124,8 +122,8 @@ private AttributeSet[] sortedAttributeSets(Map<String, AttributeSet> attrSets) {
.toArray(AttributeSet[]::new);
}

private String errorMessage(String msg) {
return "{\"errorMessage\":\"" + msg + "\"}";
private Response errResponse(String msg, Response.Status status) {
return errResponseAsTextPlain("{\"errorMessage\":\"" + msg + "\"}", status);
}

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
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;
Expand All @@ -79,11 +78,11 @@ public class QueryDeviceName {
@GET
@NoCache
@Produces("application/json")
public StreamingOutput devicename() {
public Response devicename() {
logRequest();
try {
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
JsonWriter writer = new JsonWriter(gen);
gen.writeStartObject();
Expand All @@ -95,10 +94,9 @@ public StreamingOutput devicename() {
}
gen.writeEnd();
gen.flush();
};
}).build();
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
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;
Expand All @@ -84,10 +83,10 @@ public class QueryExporters {
@GET
@NoCache
@Produces("application/json")
public StreamingOutput query() {
public Response query() {
logRequest();
try {
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class).getExporterDescriptors().stream()
Expand All @@ -101,10 +100,9 @@ public StreamingOutput query() {
});
gen.writeEnd();
gen.flush();
};
}).build();
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
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;
Expand All @@ -83,10 +82,10 @@ public class QueryPDQServices {
@GET
@NoCache
@Produces("application/json")
public StreamingOutput query() {
public Response query() {
logRequest();
try {
return out -> {
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartArray();
device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class).getPDQServiceDescriptors().stream()
Expand All @@ -100,10 +99,9 @@ public StreamingOutput query() {
});
gen.writeEnd();
gen.flush();
};
}).build();
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Loading

0 comments on commit eb08a30

Please sign in to comment.