@Override protected DataWriter createDataWriter(InstanceLocator inst, String tsuid) throws IOException { Attributes attrs; DicomInputStream in = new DicomInputStream(inst.getFile()); try { if (withoutBulkData) { in.setIncludeBulkData(false); attrs = in.readDataset(-1, Tag.PixelData); } else { in.setIncludeBulkDataLocator(true); attrs = in.readDataset(-1, -1); } } finally { SafeClose.close(in); } attrs.addAll((Attributes) inst.getObject()); adjustPatientID(attrs); adjustAccessionNumber(attrs); ArchiveApplicationEntity ae = (ArchiveApplicationEntity) as.getApplicationEntity(); try { Templates tpl = ae.getAttributeCoercionTemplates( inst.cuid, Dimse.C_STORE_RQ, Role.SCU, as.getRemoteAET()); if (tpl != null) attrs.update(SAXTransformer.transform(attrs, tpl, false, false), null); } catch (Exception e) { throw new IOException(e); } return new DataWriterAdapter(attrs); }
private static String addStudyPOIsAndGetPatientID( List<ParticipantObjectIdentification> studyPOIs, RetrieveDocumentSetResponseType rsp) { Attributes attrs = null; String studyIUID, classUID; HashMap<String, HashMap<String, List<String>>> studySopClassMap = new HashMap<String, HashMap<String, List<String>>>(); HashMap<String, List<String>> sopClassInstanceMap; List<String> instances; for (DocumentResponse doc : rsp.getDocumentResponse()) { BufferedInputStream is = null; DicomInputStream dis = null; try { DataHandler dh = doc.getDocument(); is = new BufferedInputStream(dh.getInputStream(), BUF_SIZE); is.mark(BUF_SIZE); dis = new DicomInputStream(new BufferedInputStream(is)); attrs = dis.readDataset(-1, Tag.SeriesInstanceUID); is.reset(); doc.setDocument(new DataHandler(new InputStreamDataSource(is, dh.getContentType()))); studyIUID = attrs.getString(Tag.StudyInstanceUID); classUID = attrs.getString(Tag.SOPClassUID); sopClassInstanceMap = studySopClassMap.get(studyIUID); if (sopClassInstanceMap == null) { sopClassInstanceMap = new HashMap<String, List<String>>(); studySopClassMap.put(studyIUID, sopClassInstanceMap); instances = null; } else { instances = sopClassInstanceMap.get(classUID); } if (instances == null) { instances = new ArrayList<String>(); sopClassInstanceMap.put(classUID, instances); } instances.add(attrs.getString(Tag.SOPInstanceUID)); } catch (IOException x) { log.warn("Failed to read DICOM attachment! instanceUID:" + doc.getDocumentUniqueId(), x); } } for (Entry<String, HashMap<String, List<String>>> e : studySopClassMap.entrySet()) { ParticipantObjectDescription pod = new ParticipantObjectDescription(); studyPOIs.add(createStudyPOI(e.getKey(), pod)); for (Entry<String, List<String>> e1 : e.getValue().entrySet()) { SOPClass sc = new SOPClass(); sc.setUID(e1.getKey()); sc.setNumberOfInstances(e1.getValue().size()); for (String iuid : e1.getValue()) { Instance inst = new Instance(); inst.setUID(iuid); sc.getInstance().add(inst); } pod.getSOPClass().add(sc); } } return attrs == null ? null : attrs.getString(Tag.PatientID); }
@Override public void write(OutputStream out) throws IOException, WebApplicationException { log.info( "{}@{} << {}: Content-Type={}, iuid={}", new Object[] { request.getRemoteUser(), request.getRemoteHost(), System.identityHashCode(request), mediaType, fileRef.sopInstanceUID }); DicomInputStream dis = new DicomInputStream(fileRef.getFile()); dis.setURI(bulkDataURI); try { dis.setIncludeBulkData(IncludeBulkData.URI); Attributes dataset = dis.readDataset(-1, -1); dataset.addAll(attrs); Object pixelData = dataset.getValue(Tag.PixelAspectRatio); if (pixelData instanceof Fragments) { Fragments frags = (Fragments) pixelData; Object frag0 = frags.get(0); BulkData frag1 = (BulkData) frags.get(1); dataset.setValue( Tag.PixelData, VR.OB, new BulkData( frag1.uri, frag1.transferSyntax, frag0 == Value.NULL ? frag1.offset - 16 : ((BulkData) frag0).offset - 8, -1)); } SAXTransformer.getSAXWriter(new StreamResult(out)).write(dataset); } catch (IOException e) { throw e; } catch (Exception e) { throw new WebApplicationException(e); } finally { SafeClose.close(dis); } }