Skip to content

Commit

Permalink
Fix #2814 : Configuration RS services : Provide handling of Configura…
Browse files Browse the repository at this point in the history
…tionException to return user readable messages
  • Loading branch information
vrindanayak committed Oct 7, 2020
1 parent 602c116 commit 350a26d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
package org.dcm4chee.arc.conf.rs;

import org.dcm4che3.conf.api.*;
import org.dcm4che3.conf.api.hl7.HL7ApplicationAlreadyExistsException;
import org.dcm4che3.conf.api.hl7.HL7Configuration;
import org.dcm4che3.conf.json.ConfigurationDelegate;
import org.dcm4che3.conf.json.JsonConfiguration;
Expand Down Expand Up @@ -136,6 +135,8 @@ public Response getDevice(@PathParam("DeviceName") String deviceName) {
}).build();
} catch (ConfigurationNotFoundException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -158,6 +159,8 @@ public Response listDevices() {
gen.writeEnd();
gen.flush();
}).build();
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -180,6 +183,8 @@ public Response listAETs() {
gen.writeEnd();
gen.flush();
}).build();
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -202,6 +207,8 @@ public Response listWebApps() {
gen.writeEnd();
gen.flush();
}).build();
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -234,6 +241,8 @@ public Response listHL7Apps() {
gen.writeEnd();
gen.flush();
}).build();
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -247,6 +256,8 @@ public Response listRegisteredAETS() {
logRequest();
try {
return writeJsonArray(conf.listRegisteredAETitles());
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -260,8 +271,9 @@ public Response listRegisteredHL7Apps() {
logRequest();
try {
return writeJsonArray(
conf.getDicomConfigurationExtension(HL7Configuration.class)
.listRegisteredHL7ApplicationNames());
conf.getDicomConfigurationExtension(HL7Configuration.class).listRegisteredHL7ApplicationNames());
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -275,6 +287,8 @@ public Response listRegisteredWebAppNames() {
logRequest();
try {
return writeJsonArray(conf.listRegisteredWebAppNames());
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -297,7 +311,8 @@ private EnumSet<DicomConfiguration.Option> options() {
DicomConfiguration.Option.PRESERVE_VENDOR_DATA,
DicomConfiguration.Option.PRESERVE_CERTIFICATE,
arcDev.isAuditSoftwareConfigurationVerbose()
? DicomConfiguration.Option.CONFIGURATION_CHANGES_VERBOSE : DicomConfiguration.Option.CONFIGURATION_CHANGES);
? DicomConfiguration.Option.CONFIGURATION_CHANGES_VERBOSE
: DicomConfiguration.Option.CONFIGURATION_CHANGES);
if (register == null || Boolean.parseBoolean(register))
options.add(DicomConfiguration.Option.REGISTER);
return options;
Expand All @@ -312,11 +327,12 @@ public void createDevice(@PathParam("DeviceName") String deviceName, Reader cont
try {
ConfigurationChanges diffs = conf.persist(device, options());
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (IllegalStateException e) {
} catch (IllegalStateException | ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException | WebAppAlreadyExistsException e) {
throw new WebApplicationException(
errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -333,11 +349,12 @@ public void updateDevice(@PathParam("DeviceName") String deviceName, Reader cont
ConfigurationChanges diffs = conf.merge(device, options());
if (!diffs.isEmpty())
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (IllegalStateException e) {
} catch (IllegalStateException | ConfigurationNotFoundException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (AETitleAlreadyExistsException | HL7ApplicationAlreadyExistsException | WebAppAlreadyExistsException e) {
throw new WebApplicationException(
errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -354,6 +371,8 @@ public void registerAET(@PathParam("aet") String aet) {
throw new WebApplicationException(
errResponse("Application Entity Title " + aet + " already registered.",
Response.Status.CONFLICT));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -371,6 +390,8 @@ public void unregisterAET(@PathParam("aet") String aet) {
errResponse("Application Entity Title " + aet + " not registered.",
Response.Status.NOT_FOUND));
conf.unregisterAETitle(aet);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -388,6 +409,8 @@ public void registerHL7App(@PathParam("appName") String appName) {
throw new WebApplicationException(
errResponse("HL7 Application " + appName + " already registered.",
Response.Status.CONFLICT));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -406,6 +429,8 @@ public void unregisterHL7App(@PathParam("appName") String appName) {
errResponse("HL7 Application " + appName + " not registered.",
Response.Status.NOT_FOUND));
hl7Conf.unregisterHL7Application(appName);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -422,6 +447,8 @@ public void registerWebApp(@PathParam("webAppName") String webAppName) {
throw new WebApplicationException(
errResponse("Web Application " + webAppName + " already registered.",
Response.Status.CONFLICT));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -439,6 +466,8 @@ public void unregisterWebApp(@PathParam("webAppName") String webAppName) {
errResponse("Web Application " + webAppName + " not registered.",
Response.Status.NOT_FOUND));
conf.unregisterWebAppName(webAppName);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -455,6 +484,8 @@ public void deleteDevice(@PathParam("DeviceName") String deviceName) {
} catch (IllegalStateException | ConfigurationNotFoundException e) {
throw new WebApplicationException(
errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand All @@ -477,6 +508,8 @@ public Response getVendorData(@PathParam("deviceName") String deviceName) {
}
} catch (ConfigurationNotFoundException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -498,6 +531,8 @@ public Response updateVendorData(@PathParam("deviceName") String deviceName, Fil
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -514,6 +549,8 @@ public Response deleteVendorData(@PathParam("deviceName") String deviceName) {
softwareConfigurationEvent.fire(new SoftwareConfiguration(request, deviceName, diffs));
} catch (ConfigurationNotFoundException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -528,8 +565,11 @@ private Device toDevice(String deviceName, Reader content) {
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));
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.NOT_FOUND));
} catch (IllegalArgumentException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (ConfigurationException e) {
throw new WebApplicationException(errResponse(e.getMessage(), Response.Status.CONFLICT));
} catch (Exception e) {
throw new WebApplicationException(
errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public Response getAttributeFilter(@PathParam("Entity") String entityName) {
.writeAttributeFilter(writer, entity, filter);
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (IllegalArgumentException e) {
return errResponse(e.getMessage(), Response.Status.BAD_REQUEST);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public class QueryAttributeSets {
@Produces("application/json")
public Response listAttributeSets(@PathParam("type") String type) {
logRequest();
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
try {
ArchiveDeviceExtension arcDev = device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class);
final AttributeSet.Type attrSetType = AttributeSet.Type.valueOf(type);
return Response.ok((StreamingOutput) out -> {
JsonGenerator gen = Json.createGenerator(out);
Expand All @@ -99,6 +99,8 @@ public Response listAttributeSets(@PathParam("type") String type) {
gen.writeEnd();
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (IllegalArgumentException e) {
return errResponse("Attribute Set of type : " + type + " not found.", Response.Status.NOT_FOUND);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public Response devicename() {
gen.writeEnd();
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -120,6 +122,10 @@ private void logRequest() {
request.getRemoteHost());
}

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

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
LOG.warn("Response {} caused by {}", status, errorMsg);
return Response.status(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public Response query() {
gen.writeEnd();
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -115,6 +117,10 @@ private void logRequest() {
request.getRemoteHost());
}

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

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
LOG.warn("Response {} caused by {}", status, errorMsg);
return Response.status(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public Response query() {
gen.writeEnd();
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -114,6 +116,10 @@ private void logRequest() {
request.getRemoteHost());
}

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

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
LOG.warn("Response {} caused by {}", status, errorMsg);
return Response.status(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public Response query() {
gen.writeEnd();
gen.flush();
}).build();
} catch (IllegalStateException e) {
return errResponse(e.getMessage(), Response.Status.NOT_FOUND);
} catch (Exception e) {
return errResponseAsTextPlain(exceptionAsString(e), Response.Status.INTERNAL_SERVER_ERROR);
}
Expand All @@ -120,13 +122,17 @@ private void logRequest() {
}

private RejectionNote[] sortedRejectionNotes() {
return device.getDeviceExtension(ArchiveDeviceExtension.class)
return device.getDeviceExtensionNotNull(ArchiveDeviceExtension.class)
.getRejectionNotes().stream()
.sorted(Comparator.comparing(RejectionNote::getRejectionNoteLabel))
.filter(rjNote -> rjNote.isRevokeRejection() == Boolean.parseBoolean(revokeRejection))
.toArray(RejectionNote[]::new);
}

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

private Response errResponseAsTextPlain(String errorMsg, Response.Status status) {
LOG.warn("Response {} caused by {}", status, errorMsg);
return Response.status(status)
Expand Down

0 comments on commit 350a26d

Please sign in to comment.