Skip to content

Commit

Permalink
wado-rs util: use UID as file name dcm4che#391
Browse files Browse the repository at this point in the history
  • Loading branch information
vrindanayak authored and Patrick Peer committed Oct 17, 2019
1 parent b9d3fad commit 138f746
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 36 deletions.
30 changes: 21 additions & 9 deletions dcm4che-tool/dcm4che-tool-wadors/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
usage: wadors [options]
usage: wadors [options] [URLs...]

Wado RS client simulator. It supports retrieving Study, Series, Instance
and Metadata. One may choose to specify multiple urls as arguments. Each
Expand All @@ -14,24 +14,36 @@
Supported types for object retrieval are
dicom, octetstream, pdf, jpeg, jp2, jpx,
xjls, xdicomrle, mpeg and mp4. To retrieve
metadata, supported types are xml or json.
The appropriate value of Accept header will
then be sent in request header. For eg. if it
is jpeg then Accept header shall contain
multipart/related;type=image/jpeg
metadata, supported types are xml or json. If
no value is specified, then wildcard shall be
sent in the request. The appropriate value of
Accept shall be sent in request header or as
a query parameter. For eg. if it is jpeg then
Accept value shall be
multipart/related;type=image/jpeg.
-h,--help display this help and exit
--header If specified, Accept value shall be sent as
header, else it shall be appended to the URL
as a query parameter.
--out-dir <directory> Specify directory where the received MIME
multipart messages will be unpacked into
different parts.
-u,--user <user:password> Specify the user name and password to use for
server authentication.
-V,--version output version information and exit
-
Example: wadors

Examples:
=> wadors
http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID1
}
Send WADO RS request to Wado RS Receiver to retrieve studies with Study
Instance UID StudyIUID1

=> wadors
http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID1
}
http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID2
}/series/{SeriesIUID21}
=> Send WADO RS request to Wado RS Receiver to retrieve studies with Study
Send WADO RS request to Wado RS Receiver to retrieve studies with Study
Instance UID StudyIUID1 and to retrieve series of study with Study
Instance UID StudyIUID2 and series instance UID as SeriesIUID21.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
public class WadoRS {
private static final Logger LOG = LoggerFactory.getLogger(WadoRS.class);
private static final ResourceBundle rb = ResourceBundle.getBundle("org.dcm4che3.tool.wadors.messages");
private static final String wildcard = "wildcard";
private Input input = Input.METADATA_JSON;
private String user;
private Accept accept;
private boolean header;
private static Path outDir;

public WadoRS() {}
Expand All @@ -91,19 +93,23 @@ public static void main(String[] args) {
}
}

public final void setOutputDirectory(String dir) throws IOException {
private void setOutputDirectory(String dir) throws IOException {
outDir = Files.createDirectories(Paths.get(dir));
}

public final void setUser(String user) {
private void setUser(String user) {
this.user = user;
}

public final void setAcceptType(String accept) {
private void setAcceptType(String accept) {
this.accept = Accept.valueOf(accept);
}

public final void setInput(Input input) {
private void setHeader(boolean val) {
this.header = val;
}

private void setInput(Input input) {
this.input = input;
}

Expand All @@ -112,9 +118,14 @@ private static CommandLine parseComandLine(String[] args) throws ParseException
CLIUtils.addCommonOptions(opts);
opts.addOption(Option.builder("a")
.longOpt("accept")
.hasArg(true)
.hasArg()
.desc(rb.getString("accept"))
.build());
opts.addOption(Option.builder()
.longOpt("header")
.hasArg(false)
.desc(rb.getString("header"))
.build());
opts.addOption(Option.builder()
.longOpt("out-dir")
.hasArg()
Expand All @@ -133,33 +144,52 @@ private static CommandLine parseComandLine(String[] args) throws ParseException
private static void init(CommandLine cl, WadoRS wadoRS) throws Exception {
if (cl.getArgList().isEmpty())
throw new MissingArgumentException("Specify at least one url as an argument");
if (!cl.hasOption("a"))
throw new MissingOptionException("Specify accept header");
wadoRS.setAcceptType(cl.getOptionValue("a"));
wadoRS.setAcceptType(cl.hasOption("a") ? cl.getOptionValue("a") : wildcard);
wadoRS.setUser(cl.getOptionValue("u"));
if (cl.hasOption("out-dir"))
wadoRS.setOutputDirectory(cl.getOptionValue("out-dir"));
wadoRS.setHeader(cl.hasOption("header"));
}

private void wado(String url) throws Exception {
final String uid = uidFrom(url);
if (!header)
url = appendAcceptToURL(url);
URL newUrl = new URL(url);
final HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", accept.headerVal);
if (header)
connection.setRequestProperty("Accept", accept.headerVal);
logOutgoing(connection);
if (user != null) {
String basicAuth = basicAuth(user);
LOG.info("> Authorization: " + basicAuth);
connection.setRequestProperty("Authorization", basicAuth);
}
logIncoming(connection);
unpack(connection);
unpack(connection, uid);
connection.disconnect();
}

private String appendAcceptToURL(String url) {
return url
+ (url.indexOf('?') != -1 ? "&" : "?")
+ "accept="
+ accept.headerVal.replace("+", "%2B");
}

private String uidFrom(String url) {
return url.contains("metadata")
? url.substring(url.substring(0, url.lastIndexOf('/')).lastIndexOf('/') + 1, url.lastIndexOf('/'))
: url.contains("?")
? url.substring(url.substring(0, url.indexOf('?')).lastIndexOf('/') + 1, url.indexOf('?'))
: url.substring(url.lastIndexOf('/')+1);
}

enum Accept {
wildcard("*"),
dicom("multipart/related;type=application/dicom"),
octetstream("multipart/related;type=application/octet-stream"),
pdf("multipart/related;type=application/pdf"),
Expand All @@ -180,12 +210,12 @@ enum Accept {
}
}

private static void logOutgoing(HttpURLConnection connection) {
private void logOutgoing(HttpURLConnection connection) {
LOG.info("> " + connection.getRequestMethod() + " " + connection.getURL());
LOG.info("> Accept: " + connection.getRequestProperty("Accept"));
LOG.info("> Accept: " + accept.headerVal);
}

private static void logIncoming(HttpURLConnection connection) throws Exception {
private void logIncoming(HttpURLConnection connection) throws Exception {
LOG.info("< Content-Length: " + connection.getContentLength());
LOG.info("< HTTP/1.1 Response: " + String.valueOf(connection.getResponseCode()) + " " + connection.getResponseMessage());
LOG.info("< Transfer-Encoding: " + connection.getContentEncoding());
Expand All @@ -195,14 +225,10 @@ private static void logIncoming(HttpURLConnection connection) throws Exception {
LOG.info("< Date: " + connection.getHeaderField("Date"));
}

private void unpack(HttpURLConnection connection) throws Exception {
String url = connection.getURL().toString();
final String uid = url.endsWith("metadata")
? url.substring(url.substring(0, url.lastIndexOf("/")).lastIndexOf("/") + 1, url.lastIndexOf('/'))
: url.substring(url.lastIndexOf('/')+1);

private void unpack(HttpURLConnection connection, final String uid) throws Exception {
try (InputStream is = connection.getInputStream()) {
if (accept == Accept.json) {
if (connection.getContentType().endsWith("json")) {
LOG.info("Extract metadata as json");
input.writeBodyPart(is, 1, uid);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
usage=wadors [options]
usage=wadors [options] [URLs...]
try=Try `wadors --help' for more information.
description=\n\
Wado RS client simulator. It supports retrieving Study, Series, Instance and Metadata. One may choose to specify \
Expand All @@ -7,14 +7,18 @@ or the directory selected by user as <uid>-001.dicom, <uid>-002.dicom and so on.
based on the url(s) specified. For eg. if study is retrieved the Study IUID will be used, if the url is for series \
retrieval then Series IUID shall be used. The extension of individual parts is determined by accept header. \n\-\n\
Options:
example=-\n\
Example: wadors http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID1} \
example=\n\
Examples:\n\
=> wadors http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID1} \n\
Send WADO RS request to Wado RS Receiver to retrieve studies with Study Instance UID StudyIUID1 \n\n\
=> wadors http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID1} \
http[s]://<host>:<port>/dcm4chee-arc/aets/{AETitle}/rs/studies/{StudyIUID2}/series/{SeriesIUID21} \n\
=> Send WADO RS request to Wado RS Receiver to retrieve studies with Study Instance UID StudyIUID1 and to retrieve \
Send WADO RS request to Wado RS Receiver to retrieve studies with Study Instance UID StudyIUID1 and to retrieve \
series of study with Study Instance UID StudyIUID2 and series instance UID as SeriesIUID21. \n\
accept=Specify the value for Accept header. Supported types for object retrieval are dicom, octetstream, pdf, jpeg, \
jp2, jpx, xjls, xdicomrle, mpeg and mp4. To retrieve metadata, supported types are xml or json. \
The appropriate value of Accept header will then be sent in request header. For eg. if it is jpeg then Accept header \
shall contain multipart/related;type=image/jpeg
jp2, jpx, xjls, xdicomrle, mpeg and mp4. To retrieve metadata, supported types are xml or json. If no value is \
specified, then wildcard shall be sent in the request. The appropriate value of Accept shall be sent in request header \
or as a query parameter. For eg. if it is jpeg then Accept value shall be multipart/related;type=image/jpeg.
header=If specified, Accept value shall be sent as header, else it shall be appended to the URL as a query parameter.
out-dir=Specify directory where the received MIME multipart messages will be unpacked into different parts.
user=Specify the user name and password to use for server authentication.

0 comments on commit 138f746

Please sign in to comment.