From 19488918e0840925835f8e2f42b5a4a439218d47 Mon Sep 17 00:00:00 2001 From: vrindanayak Date: Fri, 25 Nov 2016 15:30:22 +0100 Subject: [PATCH] Fix #458 dcm4che/dcm4chee-arc-light : Sychronize another archive instances with local changes by IOCM-RS --- .../java/org/dcm4chee/arc/iocm/rs/IocmRS.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/dcm4chee-arc-iocm-rs/src/main/java/org/dcm4chee/arc/iocm/rs/IocmRS.java b/dcm4chee-arc-iocm-rs/src/main/java/org/dcm4chee/arc/iocm/rs/IocmRS.java index 47d026c799..6bb7563f59 100644 --- a/dcm4chee-arc-iocm-rs/src/main/java/org/dcm4chee/arc/iocm/rs/IocmRS.java +++ b/dcm4chee-arc-iocm-rs/src/main/java/org/dcm4chee/arc/iocm/rs/IocmRS.java @@ -220,10 +220,7 @@ public void deleteStudy(@PathParam("StudyUID") String studyUID) throws Exception public String createPatient(InputStream in) throws Exception { logRequest(); try { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - TeeInputStream tIn = new TeeInputStream(in, bOut); - - JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(tIn, "UTF-8"))); + 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)); @@ -232,11 +229,31 @@ public String createPatient(InputStream in) throws Exception { ctx.setAttributes(attrs); ctx.setAttributeUpdatePolicy(Attributes.UpdatePolicy.REPLACE); patientService.updatePatient(ctx); - forwardRS("PUT", RSOperation.CreatePatient, null, bOut.toByteArray(), IDWithIssuer.pidOf(attrs).toString()); + forwardRS("PUT", RSOperation.CreatePatient, null, + getByteArrayOutStream(attrs).toByteArray(), IDWithIssuer.pidOf(attrs).toString()); return IDWithIssuer.pidOf(attrs).toString(); } catch (JsonParsingException e) { throw new WebApplicationException( getResponse(e.getMessage() + " at location : " + e.getLocation(), Response.Status.INTERNAL_SERVER_ERROR)); + } catch (IOException e) { + throw new WebApplicationException(getResponse(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); + } + } + + private ByteArrayOutputStream getByteArrayOutStream(Attributes attrs) { + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); + try { + (new StreamingOutput() { + @Override + public void write(OutputStream out) throws IOException { + JsonGenerator gen = Json.createGenerator(out); + new JSONWriter(gen).write(attrs); + gen.flush(); + } + }).write(bOut); + return bOut; + } catch (IOException e) { + throw new WebApplicationException(getResponse(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR)); } } @@ -278,8 +295,8 @@ public void updatePatient(@PathParam("PatientID") IDWithIssuer patientID, InputS public StreamingOutput updateStudy(InputStream in) throws Exception { logRequest(); try { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - TeeInputStream tIn = new TeeInputStream(in, bOut); + ByteArrayOutputStream bOutStream = new ByteArrayOutputStream(); + TeeInputStream tIn = new TeeInputStream(in, bOutStream); JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(tIn, "UTF-8"))); final Attributes attrs = reader.readDataset(null); @@ -292,14 +309,16 @@ public StreamingOutput updateStudy(InputStream in) throws Exception { throw new WebApplicationException(getResponse("Patient[id=" + patientID + "] does not exists", Response.Status.NOT_FOUND)); - if (!attrs.containsValue(Tag.StudyInstanceUID)) + boolean studyIUIDPresent = attrs.containsValue(Tag.StudyInstanceUID); + if (!studyIUIDPresent) attrs.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID()); StudyMgtContext ctx = studyService.createStudyMgtContextWEB(request, getApplicationEntity()); ctx.setPatient(patient); ctx.setAttributes(attrs); studyService.updateStudy(ctx); - forwardRS("POST", RSOperation.UpdateStudy, null, bOut.toByteArray(), null); + byte[] bOut = studyIUIDPresent ? bOutStream.toByteArray() : getByteArrayOutStream(attrs).toByteArray(); + forwardRS("POST", RSOperation.UpdateStudy, null, bOut, null); return new StreamingOutput() { @Override public void write(OutputStream out) throws IOException {