001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2012 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.tool.ianscu; 040 041import java.io.File; 042import java.io.IOException; 043import java.security.GeneralSecurityException; 044import java.util.HashMap; 045import java.util.List; 046import java.util.ResourceBundle; 047import java.util.concurrent.ExecutorService; 048import java.util.concurrent.Executors; 049import java.util.concurrent.ScheduledExecutorService; 050 051import org.apache.commons.cli.CommandLine; 052import org.apache.commons.cli.OptionBuilder; 053import org.apache.commons.cli.Options; 054import org.apache.commons.cli.ParseException; 055import org.dcm4che3.data.Tag; 056import org.dcm4che3.data.UID; 057import org.dcm4che3.data.Attributes; 058import org.dcm4che3.data.Sequence; 059import org.dcm4che3.data.VR; 060import org.dcm4che3.net.ApplicationEntity; 061import org.dcm4che3.net.Association; 062import org.dcm4che3.net.Connection; 063import org.dcm4che3.net.Device; 064import org.dcm4che3.net.DimseRSPHandler; 065import org.dcm4che3.net.IncompatibleConnectionException; 066import org.dcm4che3.net.pdu.AAssociateRQ; 067import org.dcm4che3.net.pdu.PresentationContext; 068import org.dcm4che3.tool.common.CLIUtils; 069import org.dcm4che3.tool.common.DicomFiles; 070 071/** 072 * @author Gunter Zeilinger <gunterze@gmail.com> 073 * 074 */ 075public class IanSCU { 076 077 private static ResourceBundle rb = 078 ResourceBundle.getBundle("org.dcm4che3.tool.ianscu.messages"); 079 080 private final Device device = new Device("ianscu"); 081 private final ApplicationEntity ae = new ApplicationEntity("IANSCU"); 082 private final Connection conn = new Connection(); 083 private final Connection remote = new Connection(); 084 private final AAssociateRQ rq = new AAssociateRQ(); 085 private final Attributes attrs = new Attributes(); 086 private String uidSuffix; 087 private String refPpsIUID; 088 private String refPpsCUID = UID.ModalityPerformedProcedureStepSOPClass; 089 private String availability = "ONLINE"; 090 private String retrieveAET; 091 private String retrieveURI; 092 private String retrieveURL; 093 private String retrieveUID; 094 095 private HashMap<String,Attributes> map = new HashMap<String,Attributes>(); 096 private Association as; 097 098 public IanSCU() { 099 device.addConnection(conn); 100 device.addApplicationEntity(ae); 101 ae.addConnection(conn); 102 } 103 104 public final void setUIDSuffix(String uidSuffix) { 105 this.uidSuffix = uidSuffix; 106 } 107 108 public void setTransferSyntaxes(String[] tss) { 109 rq.addPresentationContext( 110 new PresentationContext(1, UID.VerificationSOPClass, 111 UID.ImplicitVRLittleEndian)); 112 rq.addPresentationContext( 113 new PresentationContext(3, 114 UID.InstanceAvailabilityNotificationSOPClass, 115 tss)); 116 } 117 118 public void setRefPpsIUID(String refPpsIUID) { 119 this.refPpsIUID = refPpsIUID; 120 } 121 122 public void setRefPpsCUID(String refPpsCUID) { 123 this.refPpsCUID = refPpsCUID; 124 } 125 126 public void setAvailability(String availability) { 127 this.availability = availability; 128 } 129 130 public void setRetrieveAET(String retrieveAET) { 131 this.retrieveAET = retrieveAET; 132 } 133 134 public String getRetrieveAET() { 135 return retrieveAET != null ? retrieveAET : ae.getAETitle(); 136 } 137 138 public void setRetrieveURL(String retrieveURL) { 139 this.retrieveURL = retrieveURL; 140 } 141 142 public void setRetrieveURI(String retrieveURI) { 143 this.retrieveURI = retrieveURI; 144 } 145 146 public void setRetrieveUID(String retrieveUID) { 147 this.retrieveUID = retrieveUID; 148 } 149 150 @SuppressWarnings("unchecked") 151 public static void main(String[] args) { 152 try { 153 CommandLine cl = parseComandLine(args); 154 final IanSCU main = new IanSCU(); 155 configureIAN(main, cl); 156 CLIUtils.configureConnect(main.remote, main.rq, cl); 157 CLIUtils.configureBind(main.conn, main.ae, cl); 158 CLIUtils.configure(main.conn, cl); 159 main.remote.setTlsProtocols(main.conn.tlsProtocols()); 160 main.remote.setTlsCipherSuites(main.conn.getTlsCipherSuites()); 161 main.setTransferSyntaxes(CLIUtils.transferSyntaxesOf(cl)); 162 CLIUtils.addAttributes(main.attrs, cl.getOptionValues("s")); 163 main.setUIDSuffix(cl.getOptionValue("uid-suffix")); 164 List<String> argList = cl.getArgList(); 165 boolean echo = argList.isEmpty(); 166 if (!echo) { 167 System.out.println(rb.getString("scanning")); 168 DicomFiles.scan(argList, new DicomFiles.Callback() { 169 170 @Override 171 public boolean dicomFile(File f, Attributes fmi, long dsPos, 172 Attributes ds) { 173 if (UID.InstanceAvailabilityNotificationSOPClass.equals( 174 fmi.getString(Tag.MediaStorageSOPClassUID))) { 175 return main.addIAN( 176 fmi.getString(Tag.MediaStorageSOPInstanceUID), 177 ds); 178 } 179 return main.addInstance(ds); 180 } 181 }); 182 } 183 ExecutorService executorService = 184 Executors.newSingleThreadExecutor(); 185 ScheduledExecutorService scheduledExecutorService = 186 Executors.newSingleThreadScheduledExecutor(); 187 main.device.setExecutor(executorService); 188 main.device.setScheduledExecutor(scheduledExecutorService); 189 try { 190 main.open(); 191 if (echo) 192 main.echo(); 193 else 194 main.sendIans(); 195 } finally { 196 main.close(); 197 executorService.shutdown(); 198 scheduledExecutorService.shutdown(); 199 } 200 } catch (ParseException e) { 201 System.err.println("ianscu: " + e.getMessage()); 202 System.err.println(rb.getString("try")); 203 System.exit(2); 204 } catch (Exception e) { 205 System.err.println("ianscu: " + e.getMessage()); 206 e.printStackTrace(); 207 System.exit(2); 208 } 209 } 210 211 private static CommandLine parseComandLine(String[] args) 212 throws ParseException{ 213 Options opts = new Options(); 214 CLIUtils.addTransferSyntaxOptions(opts); 215 CLIUtils.addConnectOption(opts); 216 CLIUtils.addBindOption(opts, "IANSCU"); 217 CLIUtils.addAEOptions(opts); 218 CLIUtils.addResponseTimeoutOption(opts); 219 CLIUtils.addCommonOptions(opts); 220 addIANOptions(opts); 221 return CLIUtils.parseComandLine(args, opts, rb, IanSCU.class); 222 } 223 224 @SuppressWarnings("static-access") 225 private static void addIANOptions(Options opts) { 226 opts.addOption(OptionBuilder 227 .hasArg() 228 .withArgName("uid") 229 .withDescription(rb.getString("pps-iuid")) 230 .withLongOpt("pps-iuid") 231 .create()); 232 opts.addOption(OptionBuilder 233 .hasArg() 234 .withArgName("uid") 235 .withDescription(rb.getString("pps-cuid")) 236 .withLongOpt("pps-cuid") 237 .create()); 238 opts.addOption(OptionBuilder 239 .hasArg() 240 .withArgName("code-string") 241 .withDescription(rb.getString("availability")) 242 .withLongOpt("availability") 243 .create()); 244 opts.addOption(OptionBuilder 245 .hasArg() 246 .withArgName("aet") 247 .withDescription(rb.getString("retrieve-aet")) 248 .withLongOpt("retrieve-aet") 249 .create()); 250 opts.addOption(OptionBuilder 251 .hasArg() 252 .withArgName("uri") 253 .withDescription(rb.getString("retrieve-url")) 254 .withLongOpt("retrieve-url") 255 .create()); 256 opts.addOption(OptionBuilder 257 .hasArg() 258 .withArgName("uri") 259 .withDescription(rb.getString("retrieve-uri")) 260 .withLongOpt("retrieve-uri") 261 .create()); 262 opts.addOption(OptionBuilder 263 .hasArg() 264 .withArgName("uid") 265 .withDescription(rb.getString("retrieve-uid")) 266 .withLongOpt("retrieve-uid") 267 .create()); 268 opts.addOption(OptionBuilder 269 .hasArgs() 270 .withArgName("[seq/]attr=value") 271 .withValueSeparator('=') 272 .withDescription(rb.getString("set")) 273 .create("s")); 274 opts.addOption(OptionBuilder 275 .hasArg() 276 .withArgName("suffix") 277 .withDescription(rb.getString("uid-suffix")) 278 .withLongOpt("uid-suffix") 279 .create(null)); 280 } 281 282 private static void configureIAN(IanSCU main, CommandLine cl) 283 throws Exception { 284 main.setRefPpsIUID(cl.getOptionValue("pps-iuid")); 285 main.setRefPpsCUID(cl.getOptionValue("pps-cuid", 286 UID.ModalityPerformedProcedureStepSOPClass)); 287 main.setAvailability(cl.getOptionValue("availability", "ONLINE")); 288 main.setRetrieveAET(cl.getOptionValue("retrieve-aet")); 289 main.setRetrieveURI(cl.getOptionValue("retrieve-uri")); 290 main.setRetrieveURL(cl.getOptionValue("retrieve-url")); 291 main.setRetrieveUID(cl.getOptionValue("retrieve-uid")); 292 } 293 294 public void open() throws IOException, InterruptedException, 295 IncompatibleConnectionException, GeneralSecurityException { 296 as = ae.connect(conn, remote, rq); 297 } 298 299 public void close() throws IOException, InterruptedException { 300 if (as != null) { 301 as.release(); 302 } 303 } 304 305 public void echo() throws IOException, InterruptedException { 306 as.cecho().next(); 307 } 308 309 public void sendIans() throws IOException, InterruptedException { 310 for (Attributes ian : map.values()) 311 sendIan(ian); 312 } 313 314 private void sendIan(Attributes ian) throws IOException, InterruptedException { 315 as.ncreate(UID.InstanceAvailabilityNotificationSOPClass, null, ian, null, 316 new DimseRSPHandler(as.nextMessageID())); 317 } 318 319 public boolean addInstance(Attributes inst) { 320 CLIUtils.updateAttributes(inst, attrs, uidSuffix); 321 String suid = inst.getString(Tag.StudyInstanceUID); 322 if (suid == null) 323 return false; 324 325 Attributes ian = map.get(suid); 326 if (ian == null) 327 map.put(suid, ian = createIAN(inst)); 328 updateIAN(ian, inst); 329 return true; 330 } 331 332 public boolean addIAN(String iuid, Attributes ian) { 333 map.put(iuid, ian); 334 return true; 335 } 336 337 private Attributes createIAN(Attributes inst) { 338 Attributes ian = new Attributes(3); 339 Sequence refPpsSeq = 340 ian.newSequence(Tag.ReferencedPerformedProcedureStepSequence, 1); 341 if (refPpsIUID != null) { 342 Attributes refPps = new Attributes(3); 343 refPps.setString(Tag.ReferencedSOPClassUID, VR.UI, refPpsCUID); 344 refPps.setString(Tag.ReferencedSOPInstanceUID, VR.UI, refPpsIUID); 345 refPps.setNull(Tag.PerformedWorkitemCodeSequence,VR.SQ); 346 refPpsSeq.add(refPps); 347 } 348 ian.newSequence(Tag.ReferencedSeriesSequence, 1); 349 ian.setString(Tag.StudyInstanceUID, VR.UI, 350 inst.getString(Tag.StudyInstanceUID)); 351 return ian ; 352 } 353 354 private void updateIAN(Attributes mpps, Attributes inst) { 355 Sequence refSeriesSeq = mpps.getSequence(Tag.ReferencedSeriesSequence); 356 Attributes refSeries = getRefSeries(refSeriesSeq, inst); 357 Sequence refSOPSeq = refSeries.getSequence(Tag.ReferencedSOPSequence); 358 Attributes refSOP = new Attributes(6); 359 refSOP.setString(Tag.RetrieveAETitle, VR.AE, getRetrieveAET()); 360 refSOP.setString(Tag.InstanceAvailability, VR.CS, availability); 361 refSOP.setString(Tag.ReferencedSOPClassUID, VR.UI, 362 inst.getString(Tag.SOPClassUID)); 363 refSOP.setString(Tag.ReferencedSOPInstanceUID, VR.UI, 364 inst.getString(Tag.SOPInstanceUID)); 365 if (retrieveURL != null) 366 refSOP.setString(Tag.RetrieveURL, VR.UR, retrieveURL); 367 if (retrieveURI != null) 368 refSOP.setString(Tag.RetrieveURI, VR.UR, retrieveURI); 369 if (retrieveUID != null) 370 refSOP.setString(Tag.RetrieveLocationUID, VR.UI, retrieveUID); 371 refSOPSeq.add(refSOP); 372 } 373 374 private Attributes getRefSeries(Sequence refSeriesSeq, Attributes inst) { 375 String suid = inst.getString(Tag.SeriesInstanceUID); 376 for (Attributes refSeries : refSeriesSeq) { 377 if (suid.equals(refSeries.getString(Tag.SeriesInstanceUID))) 378 return refSeries; 379 } 380 Attributes refSeries = new Attributes(2); 381 refSeries.newSequence(Tag.ReferencedSOPSequence, 10); 382 refSeries.setString(Tag.SeriesInstanceUID, VR.CS, suid); 383 refSeriesSeq.add(refSeries); 384 return refSeries ; 385 } 386}