Skip to content

Commit

Permalink
dcm4che#310 : Improve Exception Handling on server and client side
Browse files Browse the repository at this point in the history
  • Loading branch information
vrindanayak committed Aug 30, 2016
1 parent 6be365f commit d6368a1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 117 deletions.
182 changes: 104 additions & 78 deletions dcm4chee-arc-iocm-rs/src/main/java/org/dcm4chee/arc/iocm/rs/IocmRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParsingException;
import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
Expand Down Expand Up @@ -218,35 +219,45 @@ public void deleteStudy(@PathParam("StudyUID") String studyUID) throws Exception
@Consumes("application/json")
public String createPatient(InputStream in) throws Exception {
logRequest();
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
Attributes attrs = reader.readDataset(null);
if (attrs.containsValue(Tag.PatientID))
throw new WebApplicationException(getResponse("Patient ID in message body", Response.Status.BAD_REQUEST));
idService.newPatientID(attrs);
PatientMgtContext ctx = patientService.createPatientMgtContextWEB(request, getApplicationEntity());
ctx.setAttributes(attrs);
ctx.setAttributeUpdatePolicy(Attributes.UpdatePolicy.REPLACE);
patientService.updatePatient(ctx);
return IDWithIssuer.pidOf(attrs).toString();
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
Attributes attrs = reader.readDataset(null);
if (attrs.containsValue(Tag.PatientID))
throw new WebApplicationException(getResponse("Patient ID in message body", Response.Status.BAD_REQUEST));
idService.newPatientID(attrs);
PatientMgtContext ctx = patientService.createPatientMgtContextWEB(request, getApplicationEntity());
ctx.setAttributes(attrs);
ctx.setAttributeUpdatePolicy(Attributes.UpdatePolicy.REPLACE);
patientService.updatePatient(ctx);
return IDWithIssuer.pidOf(attrs).toString();
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}

@PUT
@Path("/patients/{PatientID}")
@Consumes("application/json")
public void updatePatient(@PathParam("PatientID") IDWithIssuer patientID, InputStream in) throws Exception {
logRequest();
PatientMgtContext ctx = patientService.createPatientMgtContextWEB(request, getApplicationEntity());
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
ctx.setAttributes(reader.readDataset(null));
ctx.setAttributeUpdatePolicy(Attributes.UpdatePolicy.REPLACE);
IDWithIssuer bodyPatientID = ctx.getPatientID();
if (bodyPatientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));
if (patientID.equals(bodyPatientID)) {
patientService.updatePatient(ctx);
} else {
ctx.setPreviousAttributes(patientID.exportPatientIDWithIssuer(null));
patientService.changePatientID(ctx);
try {
PatientMgtContext ctx = patientService.createPatientMgtContextWEB(request, getApplicationEntity());
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
ctx.setAttributes(reader.readDataset(null));
ctx.setAttributeUpdatePolicy(Attributes.UpdatePolicy.REPLACE);
IDWithIssuer bodyPatientID = ctx.getPatientID();
if (bodyPatientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));
if (patientID.equals(bodyPatientID)) {
patientService.updatePatient(ctx);
} else {
ctx.setPreviousAttributes(patientID.exportPatientIDWithIssuer(null));
patientService.changePatientID(ctx);
}
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}

Expand All @@ -256,32 +267,37 @@ public void updatePatient(@PathParam("PatientID") IDWithIssuer patientID, InputS
@Produces("application/json")
public StreamingOutput updateStudy(InputStream in) throws Exception {
logRequest();
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
final Attributes attrs = reader.readDataset(null);
IDWithIssuer patientID = IDWithIssuer.pidOf(attrs);
if (patientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
final Attributes attrs = reader.readDataset(null);
IDWithIssuer patientID = IDWithIssuer.pidOf(attrs);
if (patientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));
Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

if (!attrs.containsValue(Tag.StudyInstanceUID))
attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
if (!attrs.containsValue(Tag.StudyInstanceUID))
attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());

StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
studyService.updateStudy(ctx);
return new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
try (JsonGenerator gen = Json.createGenerator(out)) {
new JSONWriter(gen).write(attrs);
StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
studyService.updateStudy(ctx);
return new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
try (JsonGenerator gen = Json.createGenerator(out)) {
new JSONWriter(gen).write(attrs);
}
}
}
};
};
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}

@POST
Expand All @@ -290,24 +306,29 @@ public void write(OutputStream out) throws IOException {
public String updateStudy(@PathParam("PatientID") IDWithIssuer patientID,
InputStream in) throws Exception {
logRequest();
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
Attributes attrs = reader.readDataset(null);
String studyIUID = attrs.getString(Tag.StudyInstanceUID);
if (studyIUID != null)
throw new WebApplicationException(getResponse("Study Instance UID in message body", Response.Status.BAD_REQUEST));
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
Attributes attrs = reader.readDataset(null);
String studyIUID = attrs.getString(Tag.StudyInstanceUID);
if (studyIUID != null)
throw new WebApplicationException(getResponse("Study Instance UID in message body", Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));
Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());

StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
studyService.updateStudy(ctx);
return studyIUID;
StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
studyService.updateStudy(ctx);
return studyIUID;
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}

@PUT
Expand All @@ -317,26 +338,31 @@ public void updateStudy(@PathParam("PatientID") IDWithIssuer patientID,
@PathParam("StudyUID") String studyUID,
InputStream in) throws Exception {
logRequest();
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));

StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setAttributes(reader.readDataset(null));
String studyIUIDBody = ctx.getStudyInstanceUID();
if (studyIUIDBody == null)
throw new WebApplicationException(getResponse("missing Study Instance UID in message body", Response.Status.BAD_REQUEST));
if (!studyIUIDBody.equals(studyUID))
throw new WebApplicationException(getResponse("Study Instance UID[" + studyIUIDBody +
"] in message body does not match Study Instance UID[" + studyUID + "] in path",
Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

ctx.setPatient(patient);
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));

studyService.updateStudy(ctx);
StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity());
ctx.setAttributes(reader.readDataset(null));
String studyIUIDBody = ctx.getStudyInstanceUID();
if (studyIUIDBody == null)
throw new WebApplicationException(getResponse("missing Study Instance UID in message body", Response.Status.BAD_REQUEST));
if (!studyIUIDBody.equals(studyUID))
throw new WebApplicationException(getResponse("Study Instance UID[" + studyIUIDBody +
"] in message body does not match Study Instance UID[" + studyUID + "] in path",
Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

ctx.setPatient(patient);

studyService.updateStudy(ctx);
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import javax.inject.Inject;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonParsingException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
Expand Down Expand Up @@ -111,46 +112,51 @@ public class MwlRS {
@Produces("application/json")
public StreamingOutput updateSPS(InputStream in) throws Exception {
LOG.info("Process POST {} from {}@{}", this, request.getRemoteUser(), request.getRemoteHost());
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
final Attributes attrs = reader.readDataset(null);
IDWithIssuer patientID = IDWithIssuer.pidOf(attrs);
if (patientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));

Attributes spsItem = attrs.getNestedDataset(Tag.ScheduledProcedureStepSequence);
if (spsItem == null)
throw new WebApplicationException(getResponse(
"Missing or empty (0040,0100) Scheduled Procedure Step Sequence", Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

if (!attrs.containsValue(Tag.AccessionNumber))
idService.newAccessionNumber(attrs);
if (!attrs.containsValue(Tag.RequestedProcedureID))
idService.newRequestedProcedureID(attrs);
if (!spsItem.containsValue(Tag.ScheduledProcedureStepID))
idService.newScheduledProcedureStepID(spsItem);
if (!attrs.containsValue(Tag.StudyInstanceUID))
attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
if (!spsItem.containsValue(Tag.ScheduledProcedureStepStartDate) && !spsItem.containsValue(Tag.ScheduledProcedureStepStartTime))
spsItem.setDate(Tag.ScheduledProcedureStepStartDateAndTime, new Date());
if (!spsItem.containsValue(Tag.ScheduledProcedureStepStatus))
spsItem.setString(Tag.ScheduledProcedureStepStatus, VR.CS, SPSStatus.SCHEDULED.toString());
ProcedureContext ctx = procedureService.createProcedureContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
procedureService.updateProcedure(ctx);
return new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
try (JsonGenerator gen = Json.createGenerator(out)) {
new JSONWriter(gen).write(attrs);
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
final Attributes attrs = reader.readDataset(null);
IDWithIssuer patientID = IDWithIssuer.pidOf(attrs);
if (patientID == null)
throw new WebApplicationException(getResponse("missing Patient ID in message body", Response.Status.BAD_REQUEST));

Attributes spsItem = attrs.getNestedDataset(Tag.ScheduledProcedureStepSequence);
if (spsItem == null)
throw new WebApplicationException(getResponse(
"Missing or empty (0040,0100) Scheduled Procedure Step Sequence", Response.Status.BAD_REQUEST));

Patient patient = patientService.findPatient(patientID);
if (patient == null)
throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists",
Response.Status.NOT_FOUND));

if (!attrs.containsValue(Tag.AccessionNumber))
idService.newAccessionNumber(attrs);
if (!attrs.containsValue(Tag.RequestedProcedureID))
idService.newRequestedProcedureID(attrs);
if (!spsItem.containsValue(Tag.ScheduledProcedureStepID))
idService.newScheduledProcedureStepID(spsItem);
if (!attrs.containsValue(Tag.StudyInstanceUID))
attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
if (!spsItem.containsValue(Tag.ScheduledProcedureStepStartDate) && !spsItem.containsValue(Tag.ScheduledProcedureStepStartTime))
spsItem.setDate(Tag.ScheduledProcedureStepStartDateAndTime, new Date());
if (!spsItem.containsValue(Tag.ScheduledProcedureStepStatus))
spsItem.setString(Tag.ScheduledProcedureStepStatus, VR.CS, SPSStatus.SCHEDULED.toString());
ProcedureContext ctx = procedureService.createProcedureContextWEB(request, getApplicationEntity());
ctx.setPatient(patient);
ctx.setAttributes(attrs);
procedureService.updateProcedure(ctx);
return new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException {
try (JsonGenerator gen = Json.createGenerator(out)) {
new JSONWriter(gen).write(attrs);
}
}
}
};
};
} catch (JsonParsingException e) {
throw new WebApplicationException(
getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR));
}
}

@DELETE
Expand Down

0 comments on commit d6368a1

Please sign in to comment.