Skip to content

Commit

Permalink
dcm4che#458 : Sychronize another archive instances with local changes…
Browse files Browse the repository at this point in the history
… by IOCM-RS
  • Loading branch information
vrindanayak committed Nov 24, 2016
1 parent 12a4ce0 commit d1cf0fb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 19 deletions.
5 changes: 5 additions & 0 deletions dcm4chee-arc-iocm-rs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
<artifactId>dcm4chee-arc-validation</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>org.dcm4che.dcm4chee-arc</groupId>
<artifactId>dcm4chee-arc-rs-client</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
import org.dcm4che3.json.JSONWriter;
import org.dcm4che3.net.ApplicationEntity;
import org.dcm4che3.net.Device;
import org.dcm4che3.util.TeeInputStream;
import org.dcm4che3.util.UIDUtils;
import org.dcm4chee.arc.conf.ArchiveAEExtension;
import org.dcm4chee.arc.conf.ArchiveDeviceExtension;
import org.dcm4chee.arc.conf.RejectionNote;
import org.dcm4chee.arc.conf.*;
import org.dcm4chee.arc.delete.DeletionService;
import org.dcm4chee.arc.delete.StudyNotEmptyException;
import org.dcm4chee.arc.delete.StudyNotFoundException;
Expand All @@ -59,6 +58,7 @@
import org.dcm4chee.arc.patient.PatientService;
import org.dcm4chee.arc.query.QueryService;
import org.dcm4chee.arc.retrieve.InstanceLocations;
import org.dcm4chee.arc.rs.client.RSClient;
import org.dcm4chee.arc.store.StoreContext;
import org.dcm4chee.arc.store.StoreService;
import org.dcm4chee.arc.store.StoreSession;
Expand All @@ -76,19 +76,14 @@
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.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;

/**
Expand Down Expand Up @@ -124,6 +119,9 @@ public class IocmRS {
@Inject
private IDService idService;

@Inject
private RSClient rsClient;

@PathParam("AETitle")
private String aet;

Expand All @@ -137,7 +135,7 @@ public void rejectStudy(
@PathParam("StudyUID") String studyUID,
@PathParam("CodeValue") String codeValue,
@PathParam("CodingSchemeDesignator") String designator) throws Exception {
reject("rejectStudy", studyUID, null, null, codeValue, designator);
reject(RSOperation.RejectStudy, studyUID, null, null, codeValue, designator);
}

@POST
Expand All @@ -147,7 +145,7 @@ public void rejectSeries(
@PathParam("SeriesUID") String seriesUID,
@PathParam("CodeValue") String codeValue,
@PathParam("CodingSchemeDesignator") String designator) throws Exception {
reject("rejectSeries", studyUID, seriesUID, null, codeValue, designator);
reject(RSOperation.RejectSeries, studyUID, seriesUID, null, codeValue, designator);
}

@POST
Expand All @@ -158,7 +156,7 @@ public void rejectInstance(
@PathParam("ObjectUID") String objectUID,
@PathParam("CodeValue") String codeValue,
@PathParam("CodingSchemeDesignator") String designator) throws Exception {
reject("rejectInstance", studyUID, seriesUID, objectUID, codeValue, designator);
reject(RSOperation.RejectInstance, studyUID, seriesUID, objectUID, codeValue, designator);
}

@POST
Expand Down Expand Up @@ -198,6 +196,7 @@ public void deletePatient(@PathParam("PatientID") IDWithIssuer patientID) throws
ctx.setEventActionCode(AuditMessages.EventActionCode.Delete);
ctx.setPatient(patient);
deletionService.deletePatient(ctx);
forwardRS("DELETE", RSOperation.DeletePatient, null, new byte[0], patientID.toString());
}

@DELETE
Expand All @@ -220,7 +219,10 @@ public void deleteStudy(@PathParam("StudyUID") String studyUID) throws Exception
public String createPatient(InputStream in) throws Exception {
logRequest();
try {
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
TeeInputStream tIn = new TeeInputStream(in, bOut);

JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(tIn, "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));
Expand All @@ -229,6 +231,7 @@ 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());
return IDWithIssuer.pidOf(attrs).toString();
} catch (JsonParsingException e) {
throw new WebApplicationException(
Expand All @@ -243,17 +246,23 @@ public void updatePatient(@PathParam("PatientID") IDWithIssuer patientID, InputS
logRequest();
try {
PatientMgtContext ctx = patientService.createPatientMgtContextWEB(request, getApplicationEntity());
JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(in, "UTF-8")));

ByteArrayOutputStream bOut = new ByteArrayOutputStream();
TeeInputStream tIn = new TeeInputStream(in, bOut);

JSONReader reader = new JSONReader(Json.createParser(new InputStreamReader(tIn, "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);
forwardRS("PUT", RSOperation.CreatePatient, null, bOut.toByteArray(), null);
} else {
ctx.setPreviousAttributes(patientID.exportPatientIDWithIssuer(null));
patientService.changePatientID(ctx);
forwardRS("PUT", RSOperation.UpdatePatient, null, bOut.toByteArray(), bodyPatientID.toString());
}
} catch (JsonParsingException e) {
throw new WebApplicationException(
Expand Down Expand Up @@ -438,7 +447,7 @@ private boolean authenticatedUser(HttpServletRequest request, String[] acceptedU
return false;
}

private void reject(String method, String studyUID, String seriesUID, String objectUID,
private void reject(RSOperation rsOp, String studyUID, String seriesUID, String objectUID,
String codeValue, String designator) throws IOException {
logRequest();
ApplicationEntity ae = getApplicationEntity();
Expand All @@ -459,9 +468,40 @@ private void reject(String method, String studyUID, String seriesUID, String obj
ctx.setSopInstanceUID(attrs.getString(Tag.SOPInstanceUID));
ctx.setReceiveTransferSyntax(UID.ExplicitVRLittleEndian);
storeService.store(ctx, attrs);
forwardRS("POST", rsOp, arcAE, new byte[0], null);
}

private String composeURL(String targetBaseURI, String relativeURI) {
boolean endSlash = targetBaseURI.substring(targetBaseURI.lastIndexOf("rs")+2).contains("/");
relativeURI = endSlash ? relativeURI : "/" + relativeURI;
return targetBaseURI + relativeURI;
}

private String composeRelativeURI(RSOperation rsOp, String pID) {
String reqURI = request.getRequestURI();
String relativeURI = reqURI.substring(reqURI.indexOf("rs")+3);
switch (rsOp) {
case CreatePatient:
return pID == null ? relativeURI : relativeURI + pID;
case UpdatePatient:
case DeletePatient:
case RejectStudy:
case RejectSeries:
case RejectInstance:
return relativeURI;
}
return null;
}

private void forwardRS(String method, RSOperation rsOp, ArchiveAEExtension arcAE, byte[] content, String pID) {
if (arcAE == null)
arcAE = getApplicationEntity().getAEExtension(ArchiveAEExtension.class);
List<RSForwardRule> rules = arcAE.findRSForwardRules(rsOp);
for (RSForwardRule rule : rules) {
String relativeURI = composeRelativeURI(rsOp, pID);
rsClient.scheduleRequest(method, composeURL(rule.getBaseURI(), relativeURI), content);
}
}

private StreamingOutput copyOrMoveInstances(String studyUID, InputStream in, Code code) throws Exception {
logRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@
import javax.jms.JMSException;
import javax.jms.JMSRuntimeException;
import javax.jms.ObjectMessage;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

/**
* @author Gunter Zeilinger <gunterze@gmail.com>
* @author Vrinda Nayak <vrinda.nayak@j4care.com>
* @since Nov 2016
*/
@ApplicationScoped
Expand All @@ -75,7 +81,37 @@ public void scheduleRequest(String method, String uri, byte[] content) {

@Override
public Outcome request(String method, String uri, byte[] content) throws Exception {
//TODO
return new Outcome(QueueMessage.Status.WARNING, "Not yet implemented");
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(uri);
Response response = null;
Outcome outcome;
switch (method) {
case "DELETE":
response = target.request().delete();
break;
case "POST":
response = target.request().post(Entity.json(content));
break;
case "PUT":
response = target.request().put(Entity.json(content));
break;
}
outcome = buildOutcome(Response.Status.fromStatusCode(response.getStatus()), response.getStatusInfo());
response.close();
return outcome;
}

private Outcome buildOutcome(Response.Status status, Response.StatusType st) {
switch (status) {
case NO_CONTENT:
return new Outcome(QueueMessage.Status.COMPLETED, "Completed : " + st);
case REQUEST_TIMEOUT:
case SERVICE_UNAVAILABLE:
return new Outcome(QueueMessage.Status.SCHEDULED, "Retry : " + st);
case NOT_FOUND:
case BAD_REQUEST:
return new Outcome(QueueMessage.Status.FAILED, st.toString());
}
return new Outcome(QueueMessage.Status.WARNING, "Not yet implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

package org.dcm4chee.arc.rs.client.impl;

import com.google.inject.Inject;
import org.dcm4chee.arc.qmgt.Outcome;
import org.dcm4chee.arc.qmgt.QueueManager;
import org.dcm4chee.arc.rs.client.RSClient;
Expand All @@ -51,13 +50,15 @@
import javax.ejb.MessageDriven;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
* @author Gunter Zeilinger <gunterze@gmail.com>
* @author Vrinda Nayak <vrinda.nayak@j4care.com>
* @since Nov 2016
*/
@MessageDriven(activationConfig = {
Expand All @@ -73,7 +74,7 @@ public class RSClientMDB implements MessageListener {
@Inject
private RSClient rsClient;

@javax.inject.Inject
@Inject
private QueueManager queueManager;

@Override
Expand Down

0 comments on commit d1cf0fb

Please sign in to comment.