forked from nroduit/weasis-dicom-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGet.java
190 lines (170 loc) · 7.7 KB
/
CGet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
* Copyright (c) 2009-2019 Weasis Team and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Nicolas Roduit - initial API and implementation
*******************************************************************************/
package org.weasis.dicom.op;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Map.Entry;
import java.util.Properties;
import org.dcm4che3.net.Connection;
import org.dcm4che3.net.QueryOption;
import org.dcm4che3.net.Status;
import org.dcm4che3.tool.common.CLIUtils;
import org.dcm4che3.tool.getscu.GetSCU;
import org.dcm4che3.tool.getscu.GetSCU.InformationModel;
import org.dcm4che3.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weasis.core.util.FileUtil;
import org.weasis.core.util.StringUtil;
import org.weasis.dicom.param.AdvancedParams;
import org.weasis.dicom.param.DeviceOpService;
import org.weasis.dicom.param.DicomNode;
import org.weasis.dicom.param.DicomParam;
import org.weasis.dicom.param.DicomProgress;
import org.weasis.dicom.param.DicomState;
import org.weasis.dicom.util.ServiceUtil;
public class CGet {
private static final Logger LOGGER = LoggerFactory.getLogger(CGet.class);
private CGet() {
}
/**
* @param callingNode
* the calling DICOM node configuration
* @param calledNode
* the called DICOM node configuration
* @param progress
* the progress handler
* @param keys
* the matching and returning keys. DicomParam with no value is a returning key.
* @return The DicomSate instance which contains the DICOM response, the DICOM status, the error message and the
* progression.
*/
public static DicomState process(DicomNode callingNode, DicomNode calledNode, DicomProgress progress,
File outputDir, DicomParam... keys) {
return process(null, callingNode, calledNode, progress, outputDir, keys);
}
/**
* @param params
* the optional advanced parameters (proxy, authentication, connection and TLS)
* @param callingNode
* the calling DICOM node configuration
* @param calledNode
* the called DICOM node configuration
* @param progress
* the progress handler
* @param keys
* the matching and returning keys. DicomParam with no value is a returning key.
* @return The DicomSate instance which contains the DICOM response, the DICOM status, the error message and the
* progression.
*/
public static DicomState process(AdvancedParams params, DicomNode callingNode, DicomNode calledNode,
DicomProgress progress, File outputDir, DicomParam... keys) {
return process(params, callingNode, calledNode, progress, outputDir, null, keys);
}
/**
* @param params
* the optional advanced parameters (proxy, authentication, connection and TLS)
* @param callingNode
* the calling DICOM node configuration
* @param calledNode
* the called DICOM node configuration
* @param progress
* the progress handler
* @param keys
* the matching and returning keys. DicomParam with no value is a returning key.
* @return The DicomSate instance which contains the DICOM response, the DICOM status, the error message and the
* progression.
*/
public static DicomState process(AdvancedParams params, DicomNode callingNode, DicomNode calledNode,
DicomProgress progress, File outputDir, URL sopClassURL, DicomParam... keys) {
if (callingNode == null || calledNode == null || outputDir == null) {
throw new IllegalArgumentException("callingNode, calledNode or outputDir cannot be null!");
}
GetSCU getSCU = null;
AdvancedParams options = params == null ? new AdvancedParams() : params;
try {
getSCU = new GetSCU(progress);
Connection remote = getSCU.getRemoteConnection();
Connection conn = getSCU.getConnection();
options.configureConnect(getSCU.getAAssociateRQ(), remote, calledNode);
options.configureBind(getSCU.getApplicationEntity(), conn, callingNode);
DeviceOpService service = new DeviceOpService(getSCU.getDevice());
// configure
options.configure(conn);
options.configureTLS(conn, remote);
getSCU.setPriority(options.getPriority());
getSCU.setStorageDirectory(outputDir);
getSCU.setInformationModel(getInformationModel(options), options.getTsuidOrder(),
options.getQueryOptions().contains(QueryOption.RELATIONAL));
configureRelatedSOPClass(getSCU, sopClassURL);
for (DicomParam p : keys) {
getSCU.addKey(p.getTag(), p.getValues());
}
service.start();
try {
DicomState dcmState = getSCU.getState();
long t1 = System.currentTimeMillis();
getSCU.open();
long t2 = System.currentTimeMillis();
getSCU.retrieve();
ServiceUtil.forceGettingAttributes(dcmState, getSCU);
long t3 = System.currentTimeMillis();
String timeMsg =
MessageFormat.format("DICOM C-GET connected in {2}ms from {0} to {1}. Get files in {3}ms.",
getSCU.getAAssociateRQ().getCallingAET(), getSCU.getAAssociateRQ().getCalledAET(), t2 - t1,
t3 - t2);
return DicomState.buildMessage(dcmState, timeMsg, null);
} catch (Exception e) {
LOGGER.error("getscu", e);
ServiceUtil.forceGettingAttributes(getSCU.getState(), getSCU);
return DicomState.buildMessage(getSCU.getState(), null, e);
} finally {
FileUtil.safeClose(getSCU);
service.stop();
}
} catch (Exception e) {
LOGGER.error("getscu", e);
return new DicomState(Status.UnableToProcess,
"DICOM Get failed" + StringUtil.COLON_AND_SPACE + e.getMessage(), null);
}
}
private static void configureRelatedSOPClass(GetSCU getSCU, URL url) throws IOException {
Properties p = new Properties();
try {
if (url == null) {
p.load(getSCU.getClass().getResourceAsStream("store-tcs.properties"));
} else {
p.load(url.openStream());
}
for (Entry<Object, Object> entry : p.entrySet()) {
configureStorageSOPClass(getSCU, (String) entry.getKey(), (String) entry.getValue());
}
} catch (Exception e) {
LOGGER.error("Read sop classes", e);
}
}
private static void configureStorageSOPClass(GetSCU getSCU, String cuid, String tsuids) {
String[] ts = StringUtils.split(tsuids, ';');
for (int i = 0; i < ts.length; i++) {
ts[i] = CLIUtils.toUID(ts[i]);
}
getSCU.addOfferedStorageSOPClass(CLIUtils.toUID(cuid), ts);
}
private static InformationModel getInformationModel(AdvancedParams options) {
Object model = options.getInformationModel();
if (model instanceof InformationModel) {
return (InformationModel) model;
}
return InformationModel.StudyRoot;
}
}