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) 2011
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.net.service;
040
041import java.io.IOException;
042
043import org.dcm4che3.data.Tag;
044import org.dcm4che3.data.Attributes;
045import org.dcm4che3.data.VR;
046import org.dcm4che3.data.ValidationResult;
047import org.dcm4che3.net.Status;
048import org.dcm4che3.util.StringUtils;
049
050/**
051 * @author Gunter Zeilinger <gunterze@gmail.com>
052 *
053 */
054public class DicomServiceException extends IOException {
055
056    private static final long serialVersionUID = -8680017798403768406L;
057
058    private final Attributes rsp;
059    private Attributes data;
060
061    public DicomServiceException(int status) {
062        rsp = new Attributes();
063        setStatus(status);
064    }
065
066    public DicomServiceException(int status, String message) {
067        super(message);
068        rsp = new Attributes();
069        setStatus(status);
070        setErrorComment(getMessage());
071    }
072
073    public DicomServiceException(int status, Throwable cause) {
074        super(cause);
075        rsp = new Attributes();
076        setStatus(status);
077        setErrorComment(getMessage());
078    }
079
080    public DicomServiceException(int status, String message, Throwable cause) {
081        super(message, cause);
082        rsp = new Attributes();
083        setStatus(status);
084        setErrorComment(getMessage());
085    }
086
087
088    public static Throwable initialCauseOf(Throwable e) {
089        if (e == null)
090            return null;
091
092        Throwable cause;
093        while ((cause = e.getCause()) != null)
094            e = cause;
095        return e;
096    }
097
098    @Override
099    public String toString() {
100
101        return super.toString() + "( status = "+getStatus()+" )";
102    }
103
104    private void setStatus(int status) {
105        rsp.setInt(Tag.Status, VR.US, status);
106    }
107
108    public int getStatus() {
109        return rsp.getInt(Tag.Status, 0);
110    }
111
112    public DicomServiceException setUID(int tag, String value) {
113        rsp.setString(tag, VR.UI, value);
114        return this;
115    }
116
117    public DicomServiceException setErrorComment(String val) {
118        if (val != null)
119            rsp.setString(Tag.ErrorComment, VR.LO, StringUtils.truncate(val, 64));
120        return this;
121    }
122
123    public DicomServiceException setErrorID(int val) {
124        rsp.setInt(Tag.ErrorID, VR.US, val);
125        return this;
126    }
127
128    public DicomServiceException setEventTypeID(int val) {
129        rsp.setInt(Tag.EventTypeID, VR.US, val);
130        return this;
131    }
132
133    public DicomServiceException setActionTypeID(int val) {
134        rsp.setInt(Tag.ActionTypeID, VR.US, val);
135        return this;
136    }
137
138    public DicomServiceException setOffendingElements(int... tags) {
139        rsp.setInt(Tag.OffendingElement, VR.AT, tags);
140        return this;
141    }
142
143    public DicomServiceException setAttributeIdentifierList(int... tags) {
144        rsp.setInt(Tag.AttributeIdentifierList, VR.AT, tags);
145        return this;
146    }
147
148    public Attributes mkRSP(int cmdField, int msgId) {
149        rsp.setInt(Tag.CommandField, VR.US, cmdField);
150        rsp.setInt(Tag.MessageIDBeingRespondedTo, VR.US, msgId);
151        return rsp;
152    }
153
154    public final Attributes getDataset() {
155        return data;
156    }
157
158    public final DicomServiceException setDataset(Attributes data) {
159        this.data = data;
160        return this;
161    }
162
163    public static DicomServiceException valueOf(ValidationResult result,
164            Attributes attrs) {
165        if (result.hasNotAllowedAttributes())
166            return new DicomServiceException(Status.NoSuchAttribute)
167                .setAttributeIdentifierList(result.tagsOfNotAllowedAttributes());
168        if (result.hasMissingAttributes())
169            return new DicomServiceException(Status.MissingAttribute)
170                .setAttributeIdentifierList(result.tagsOfMissingAttributes());
171        if (result.hasMissingAttributeValues())
172            return new DicomServiceException(Status.MissingAttributeValue)
173                .setDataset(new Attributes(attrs, result.tagsOfMissingAttributeValues()));
174        if (result.hasInvalidAttributeValues())
175            return new DicomServiceException(Status.InvalidAttributeValue)
176                .setDataset(new Attributes(attrs, result.tagsOfInvalidAttributeValues()));
177        return null;
178    }
179}