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) 2013
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.camel;
040
041import java.io.IOException;
042
043import org.apache.camel.AsyncCallback;
044import org.apache.camel.Exchange;
045import org.apache.camel.Processor;
046import org.apache.camel.impl.DefaultConsumer;
047import org.apache.camel.util.AsyncProcessorHelper;
048import org.dcm4che3.data.Tag;
049import org.dcm4che3.data.Attributes;
050import org.dcm4che3.net.Association;
051import org.dcm4che3.net.Dimse;
052import org.dcm4che3.net.DimseRQHandler;
053import org.dcm4che3.net.PDVInputStream;
054import org.dcm4che3.net.Status;
055import org.dcm4che3.net.pdu.PresentationContext;
056import org.dcm4che3.net.service.DicomServiceException;
057
058/**
059 * @author Gunter Zeilinger <gunterze@gmail.com>
060 *
061 */
062public class DicomConsumer extends DefaultConsumer implements DimseRQHandler{
063
064    public DicomConsumer(DicomEndpoint endpoint, Processor processor) {
065        super(endpoint, processor);
066    }
067
068    @Override
069    public DicomEndpoint getEndpoint() {
070        return (DicomEndpoint) super.getEndpoint();
071    }
072
073    public String[] getSopClasses() {
074        return getEndpoint().getSopClasses();
075    }
076
077    @Override
078    protected void doStart() throws Exception {
079        super.doStart();
080        getEndpoint().getComponent().registerDicomConsumer(this);
081    }
082
083    @Override
084    protected void doStop() throws Exception {
085        getEndpoint().getComponent().unregisterDicomConsumer(this);
086        super.doStop();
087    }
088
089    @Override
090    public void onClose(Association as) {
091        // TODO Auto-generated method stub
092        
093    }
094
095    @Override
096    public void onDimseRQ(Association as, PresentationContext pc, Dimse dimse,
097            Attributes cmd, PDVInputStream data) throws IOException {
098        final int msgid = cmd.getInt(Tag.MessageID, 0);
099        Exchange exchange = getEndpoint().createExchange(dimse, cmd, data);
100        AsyncCallback callback = new EndpointDimseRQHandlerAsyncCallback(
101                as, pc, dimse, msgid, exchange);
102        AsyncProcessorHelper.process(getAsyncProcessor(), exchange, callback);
103    }
104
105    private final class EndpointDimseRQHandlerAsyncCallback
106        implements AsyncCallback {
107    
108        private final Association as;
109        private final PresentationContext pc;
110        private final Dimse dimse;
111        private final int msgId;
112        private final Exchange exchange;
113    
114        public EndpointDimseRQHandlerAsyncCallback(Association as,
115                PresentationContext pc, Dimse dimse, int msgId,
116                Exchange exchange) {
117            this.as = as;
118            this.pc = pc;
119            this.dimse = dimse;
120            this.msgId = msgId;
121            this.exchange = exchange;
122        }
123    
124        @Override
125        public void done(boolean doneSync) {
126            Attributes cmd;
127            Attributes data;
128            if (exchange.getException() != null) {
129                DicomServiceException dse;
130                Exception ex = exchange.getException();
131                if (ex instanceof DicomServiceException) {
132                    dse = (DicomServiceException) ex;
133                } else {
134                    dse = new DicomServiceException(Status.ProcessingFailure, ex);
135                }
136                cmd = dse.mkRSP(dimse.commandFieldOfRSP(), msgId);
137                data = dse.getDataset();
138            } else {
139                DicomMessage out = exchange.getOut(DicomMessage.class);
140                cmd = out.getCommand();
141                data = out.getBody(Attributes.class);
142            }
143            as.tryWriteDimseRSP(pc, cmd, data);
144        }
145    
146    }
147}