Search in sources :

Example 6 with AttributeEditorContext

use of org.weasis.dicom.param.AttributeEditorContext in project karnak by OsiriX-Foundation.

the class FilterEditorTest method when_no_class_uid_found_should_modify_context.

@Test
void when_no_class_uid_found_should_modify_context() {
    // Init data
    // SopClassUIDEntities
    Set<SOPClassUIDEntity> sopClassUIDEntities = new HashSet<>();
    SOPClassUIDEntity sopClassUIDEntityFirst = new SOPClassUIDEntity();
    SOPClassUIDEntity sopClassUIDEntitySecond = new SOPClassUIDEntity();
    sopClassUIDEntityFirst.setUid("TEST FIRST");
    sopClassUIDEntitySecond.setUid("TEST SECOND");
    sopClassUIDEntities.add(sopClassUIDEntityFirst);
    sopClassUIDEntities.add(sopClassUIDEntitySecond);
    // Attributes
    Attributes attributes = new Attributes();
    attributes.setString(Tag.SOPClassUID, VR.SH, "TEST NOT FOUND");
    // AttributeEditorContext
    DicomNode source = new DicomNode("source");
    DicomNode destination = new DicomNode("destination");
    AttributeEditorContext attributeEditorContext = new AttributeEditorContext("tsuid", source, destination);
    // Create filterEditor
    FilterEditor filterEditor = new FilterEditor(sopClassUIDEntities);
    // Call service
    filterEditor.apply(attributes, attributeEditorContext);
    // Test results
    assertEquals(Abort.FILE_EXCEPTION, attributeEditorContext.getAbort());
}
Also used : AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) SOPClassUIDEntity(org.karnak.backend.data.entity.SOPClassUIDEntity) Attributes(org.dcm4che3.data.Attributes) DicomNode(org.weasis.dicom.param.DicomNode) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with AttributeEditorContext

use of org.weasis.dicom.param.AttributeEditorContext in project karnak by OsiriX-Foundation.

the class ForwardService method transferOther.

public void transferOther(ForwardDicomNode fwdNode, WebForwardDestination destination, Attributes copy, Params p) throws IOException {
    Attributes attributesToSend = new Attributes();
    Attributes attributesOriginal = new Attributes();
    try {
        List<AttributeEditor> editors = destination.getDicomEditors();
        DicomStowRS stow = destination.getStowrsSingleFile();
        var syntax = new AdaptTransferSyntax(p.getTsuid(), destination.getOutputTransferSyntax(p.getTsuid()));
        if (syntax.getRequested().equals(p.getTsuid()) && editors.isEmpty()) {
            attributesToSend = copy;
            attributesOriginal.addAll(copy);
            stow.uploadDicom(copy, syntax.getRequested());
        } else {
            AttributeEditorContext context = new AttributeEditorContext(p.getTsuid(), fwdNode, null);
            Attributes attributes = new Attributes(copy);
            attributesToSend = attributes;
            attributesOriginal.addAll(attributes);
            editors.forEach(e -> e.apply(attributes, context));
            if (context.getAbort() == Abort.FILE_EXCEPTION) {
                throw new AbortException(context.getAbort(), context.getAbortMessage());
            } else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
                throw new AbortException(context.getAbort(), "DICOM associtation abort. " + context.getAbortMessage());
            }
            BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
            if (desc == null) {
                stow.uploadDicom(attributes, syntax.getOriginal());
            } else {
                Editable<PlanarImage> editable = transformImage(attributes, context);
                stow.uploadPayload(ImageAdapter.preparePlayload(attributes, syntax, desc, editable));
            }
            progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
            monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
        }
    } catch (HttpException httpException) {
        if (httpException.getStatusCode() != 409) {
            progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
            monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, httpException.getMessage());
            throw new AbortException(Abort.FILE_EXCEPTION, "DICOMWeb forward", httpException);
        } else {
            progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
            monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
            LOGGER.debug("File already present in destination");
        }
    } catch (AbortException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
            throw e;
        }
    } catch (IOException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        throw e;
    } catch (Exception e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        LOGGER.error(ERROR_WHEN_FORWARDING, e);
    }
}
Also used : Attributes(org.dcm4che3.data.Attributes) IOException(java.io.IOException) HttpException(org.weasis.dicom.web.HttpException) AbortException(org.karnak.backend.exception.AbortException) IOException(java.io.IOException) AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) AttributeEditor(org.weasis.dicom.param.AttributeEditor) HttpException(org.weasis.dicom.web.HttpException) AdaptTransferSyntax(org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax) BytesWithImageDescriptor(org.dcm4che3.img.stream.BytesWithImageDescriptor) DicomStowRS(org.weasis.dicom.web.DicomStowRS) PlanarImage(org.weasis.opencv.data.PlanarImage) AbortException(org.karnak.backend.exception.AbortException)

Example 8 with AttributeEditorContext

use of org.weasis.dicom.param.AttributeEditorContext in project karnak by OsiriX-Foundation.

the class ForwardService method transfer.

public List<File> transfer(ForwardDicomNode sourceNode, DicomForwardDestination destination, Attributes copy, Params p) throws IOException {
    StoreFromStreamSCU streamSCU = destination.getStreamSCU();
    DicomInputStream in = null;
    List<File> files;
    Attributes attributesOriginal = new Attributes();
    Attributes attributesToSend = new Attributes();
    try {
        if (!streamSCU.isReadyForDataTransfer()) {
            throw new IllegalStateException("Association not ready for transfer.");
        }
        DataWriter dataWriter;
        String cuid = p.getCuid();
        String iuid = p.getIuid();
        String tsuid = p.getTsuid();
        var syntax = new AdaptTransferSyntax(tsuid, streamSCU.selectTransferSyntax(cuid, destination.getOutputTransferSyntax(tsuid)));
        List<AttributeEditor> editors = destination.getDicomEditors();
        if (copy == null && editors.isEmpty() && syntax.getRequested().equals(tsuid)) {
            dataWriter = new InputStreamDataWriter(p.getData());
            attributesToSend = new DicomInputStream(p.getData()).readDataset();
            attributesOriginal.addAll(attributesToSend);
        } else {
            AttributeEditorContext context = new AttributeEditorContext(tsuid, sourceNode, streamSCU.getRemoteDicomNode());
            in = new DicomInputStream(p.getData(), tsuid);
            in.setIncludeBulkData(IncludeBulkData.URI);
            Attributes attributes = in.readDataset();
            attributesOriginal.addAll(attributes);
            attributesToSend = attributes;
            if (copy != null) {
                copy.addAll(attributes);
            }
            if (!editors.isEmpty()) {
                editors.forEach(e -> e.apply(attributes, context));
                iuid = attributes.getString(Tag.SOPInstanceUID);
                cuid = attributes.getString(Tag.SOPClassUID);
            }
            if (context.getAbort() == Abort.FILE_EXCEPTION) {
                if (p.getData() instanceof PDVInputStream) {
                    ((PDVInputStream) p.getData()).skipAll();
                }
                throw new AbortException(context.getAbort(), context.getAbortMessage());
            } else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
                if (p.getAs() != null) {
                    p.getAs().abort();
                }
                throw new AbortException(context.getAbort(), "DICOM association abort: " + context.getAbortMessage());
            }
            BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
            Editable<PlanarImage> editable = transformImage(attributes, context);
            dataWriter = ImageAdapter.buildDataWriter(attributes, syntax, editable, desc);
        }
        streamSCU.cstore(cuid, iuid, p.getPriority(), dataWriter, syntax.getSuitable());
        progressNotify(destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
    } catch (AbortException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
            throw e;
        }
    } catch (IOException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        throw e;
    } catch (Exception e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        LOGGER.error(ERROR_WHEN_FORWARDING, e);
    } finally {
        streamSCU.triggerCloseExecutor();
        files = cleanOrGetBulkDataFiles(in, copy == null);
    }
    return files;
}
Also used : StoreFromStreamSCU(org.weasis.dicom.util.StoreFromStreamSCU) Attributes(org.dcm4che3.data.Attributes) IOException(java.io.IOException) DicomInputStream(org.dcm4che3.io.DicomInputStream) HttpException(org.weasis.dicom.web.HttpException) AbortException(org.karnak.backend.exception.AbortException) IOException(java.io.IOException) InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) AttributeEditor(org.weasis.dicom.param.AttributeEditor) AdaptTransferSyntax(org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax) BytesWithImageDescriptor(org.dcm4che3.img.stream.BytesWithImageDescriptor) File(java.io.File) PlanarImage(org.weasis.opencv.data.PlanarImage) DataWriter(org.dcm4che3.net.DataWriter) InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) PDVInputStream(org.dcm4che3.net.PDVInputStream) AbortException(org.karnak.backend.exception.AbortException)

Example 9 with AttributeEditorContext

use of org.weasis.dicom.param.AttributeEditorContext in project karnak by OsiriX-Foundation.

the class ForwardService method transfer.

public List<File> transfer(ForwardDicomNode fwdNode, WebForwardDestination destination, Attributes copy, Params p) throws IOException {
    DicomInputStream in = null;
    List<File> files;
    Attributes attributesToSend = new Attributes();
    Attributes attributesOriginal = new Attributes();
    try {
        List<AttributeEditor> editors = destination.getDicomEditors();
        DicomStowRS stow = destination.getStowrsSingleFile();
        var syntax = new AdaptTransferSyntax(p.getTsuid(), destination.getOutputTransferSyntax(p.getTsuid()));
        if (syntax.getRequested().equals(p.getTsuid()) && copy == null && editors.isEmpty()) {
            Attributes fmi = Attributes.createFileMetaInformation(p.getIuid(), p.getCuid(), syntax.getRequested());
            try (InputStream stream = p.getData()) {
                attributesToSend = new DicomInputStream(p.getData()).readDataset();
                attributesOriginal.addAll(attributesToSend);
                stow.uploadDicom(stream, fmi);
            } catch (HttpException httpException) {
                if (httpException.getStatusCode() != 409) {
                    throw new AbortException(Abort.FILE_EXCEPTION, httpException.getMessage());
                } else {
                    LOGGER.debug("File already present in destination");
                }
            }
        } else {
            AttributeEditorContext context = new AttributeEditorContext(p.getTsuid(), fwdNode, null);
            in = new DicomInputStream(p.getData(), p.getTsuid());
            in.setIncludeBulkData(IncludeBulkData.URI);
            Attributes attributes = in.readDataset();
            attributesToSend = attributes;
            attributesOriginal.addAll(attributes);
            if (copy != null) {
                copy.addAll(attributes);
            }
            if (!editors.isEmpty()) {
                editors.forEach(e -> e.apply(attributes, context));
            }
            if (context.getAbort() == Abort.FILE_EXCEPTION) {
                if (p.getData() instanceof PDVInputStream) {
                    ((PDVInputStream) p.getData()).skipAll();
                }
                throw new AbortException(context.getAbort(), context.getAbortMessage());
            } else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
                if (p.getAs() != null) {
                    p.getAs().abort();
                }
                throw new AbortException(context.getAbort(), "STOW-RS abort: " + context.getAbortMessage());
            }
            BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
            if (desc == null) {
                stow.uploadDicom(attributes, syntax.getOriginal());
            } else {
                Editable<PlanarImage> editable = transformImage(attributes, context);
                stow.uploadPayload(ImageAdapter.preparePlayload(attributes, syntax, desc, editable));
            }
        }
        progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
    } catch (AbortException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
            throw e;
        }
    } catch (IOException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        throw e;
    } catch (Exception e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
        monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        LOGGER.error(ERROR_WHEN_FORWARDING, e);
    } finally {
        files = cleanOrGetBulkDataFiles(in, copy == null);
    }
    return files;
}
Also used : PDVInputStream(org.dcm4che3.net.PDVInputStream) DicomInputStream(org.dcm4che3.io.DicomInputStream) InputStream(java.io.InputStream) Attributes(org.dcm4che3.data.Attributes) IOException(java.io.IOException) DicomInputStream(org.dcm4che3.io.DicomInputStream) HttpException(org.weasis.dicom.web.HttpException) AbortException(org.karnak.backend.exception.AbortException) IOException(java.io.IOException) AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) AttributeEditor(org.weasis.dicom.param.AttributeEditor) HttpException(org.weasis.dicom.web.HttpException) AdaptTransferSyntax(org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax) BytesWithImageDescriptor(org.dcm4che3.img.stream.BytesWithImageDescriptor) File(java.io.File) DicomStowRS(org.weasis.dicom.web.DicomStowRS) PlanarImage(org.weasis.opencv.data.PlanarImage) AbortException(org.karnak.backend.exception.AbortException) PDVInputStream(org.dcm4che3.net.PDVInputStream)

Example 10 with AttributeEditorContext

use of org.weasis.dicom.param.AttributeEditorContext in project karnak by OsiriX-Foundation.

the class ForwardService method transformImage.

private static Editable<PlanarImage> transformImage(Attributes attributes, AttributeEditorContext context) {
    MaskArea m = context.getMaskArea();
    boolean defacing = LangUtil.getEmptytoFalse(context.getProperties().getProperty(Defacer.APPLY_DEFACING));
    if (m != null || defacing) {
        return img -> {
            PlanarImage image = img;
            if (defacing) {
                image = Defacer.apply(attributes, image);
            }
            if (m != null) {
                image = MaskArea.drawShape(image.toMat(), m);
            }
            return image;
        };
    }
    return null;
}
Also used : AttributeEditor(org.weasis.dicom.param.AttributeEditor) HttpException(org.weasis.dicom.web.HttpException) DataWriter(org.dcm4che3.net.DataWriter) AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) UID(org.dcm4che3.data.UID) Attributes(org.dcm4che3.data.Attributes) BytesWithImageDescriptor(org.dcm4che3.img.stream.BytesWithImageDescriptor) ForwardDestination(org.karnak.backend.dicom.ForwardDestination) AbortException(org.karnak.backend.exception.AbortException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) ImageAdapter(org.dcm4che3.img.stream.ImageAdapter) Service(org.springframework.stereotype.Service) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) WebForwardDestination(org.karnak.backend.dicom.WebForwardDestination) InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) StoreFromStreamSCU(org.weasis.dicom.util.StoreFromStreamSCU) TransferStatusEntity(org.karnak.backend.data.entity.TransferStatusEntity) FileUtil(org.weasis.core.util.FileUtil) DicomOutputData(org.dcm4che3.img.DicomOutputData) Editable(org.dcm4che3.img.util.Editable) PDVInputStream(org.dcm4che3.net.PDVInputStream) LangUtil(org.weasis.core.util.LangUtil) PlanarImage(org.weasis.opencv.data.PlanarImage) Logger(org.slf4j.Logger) Params(org.karnak.backend.dicom.Params) IncludeBulkData(org.dcm4che3.io.DicomInputStream.IncludeBulkData) DataWriterAdapter(org.dcm4che3.net.DataWriterAdapter) DicomStowRS(org.weasis.dicom.web.DicomStowRS) Set(java.util.Set) AdaptTransferSyntax(org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax) IOException(java.io.IOException) Status(org.dcm4che3.net.Status) ProgressStatus(org.weasis.dicom.util.ServiceUtil.ProgressStatus) File(java.io.File) TransferMonitoringEvent(org.karnak.backend.model.event.TransferMonitoringEvent) Association(org.dcm4che3.net.Association) List(java.util.List) ForwardDicomNode(org.karnak.backend.dicom.ForwardDicomNode) Tag(org.dcm4che3.data.Tag) ServiceUtil(org.weasis.dicom.util.ServiceUtil) DicomInputStream(org.dcm4che3.io.DicomInputStream) DicomForwardDestination(org.karnak.backend.dicom.DicomForwardDestination) Abort(org.weasis.dicom.param.AttributeEditorContext.Abort) Defacer(org.karnak.backend.dicom.Defacer) MaskArea(org.dcm4che3.img.op.MaskArea) InputStream(java.io.InputStream) MaskArea(org.dcm4che3.img.op.MaskArea) PlanarImage(org.weasis.opencv.data.PlanarImage)

Aggregations

Attributes (org.dcm4che3.data.Attributes)14 AttributeEditorContext (org.weasis.dicom.param.AttributeEditorContext)14 IOException (java.io.IOException)6 BytesWithImageDescriptor (org.dcm4che3.img.stream.BytesWithImageDescriptor)6 AdaptTransferSyntax (org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax)6 AttributeEditor (org.weasis.dicom.param.AttributeEditor)6 Test (org.junit.jupiter.api.Test)5 AbortException (org.karnak.backend.exception.AbortException)5 DicomNode (org.weasis.dicom.param.DicomNode)5 HttpException (org.weasis.dicom.web.HttpException)5 PlanarImage (org.weasis.opencv.data.PlanarImage)5 ArrayList (java.util.ArrayList)4 DestinationEntity (org.karnak.backend.data.entity.DestinationEntity)4 ProfileEntity (org.karnak.backend.data.entity.ProfileEntity)4 ProjectEntity (org.karnak.backend.data.entity.ProjectEntity)4 SecretEntity (org.karnak.backend.data.entity.SecretEntity)4 File (java.io.File)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3