From 0872d2d8a64fd701776b272f49222428d8def07f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 3 Nov 2015 14:38:34 +0100 Subject: initial commit --- .../at/gv/egovernment/moa/spss/MOAException.java | 190 +++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/MOAException.java (limited to 'moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/MOAException.java') diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/MOAException.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/MOAException.java new file mode 100644 index 0000000..803f3fd --- /dev/null +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/MOAException.java @@ -0,0 +1,190 @@ +/* + * Copyright 2003 Federal Chancellery Austria + * MOA-SPSS has been developed in a cooperation between BRZ, the Federal + * Chancellery Austria - ICT staff unit, and Graz University of Technology. + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://www.osor.eu/eupl/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text + * file for details on the various modules and licenses. + * The "NOTICE" text file is part of the distribution. Any derivative works + * that you distribute must include a readable copy of the "NOTICE" text file. + */ + + +package at.gv.egovernment.moa.spss; +import java.io.PrintStream; +import java.io.PrintWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import at.gv.egovernment.moa.util.Constants; + + +import at.gv.egovernment.moa.spss.util.MessageProvider; + +/** + * Base class of MOA specific exceptions. + * + * This class has the ability to wrap other exceptions which may be seen + * as the root cause for this exception. A similar mechanism is in place + * since JDK1.4 (see the getClause() method) but will not be used + * because of required compatibility with JDK1.3. + * + * @author Patrick Peck + * @version $Id$ + */ +public abstract class MOAException extends Exception { + /** + * + */ + private static final long serialVersionUID = 7115301799538771949L; +/** The message ID. */ + private String messageId; + /** The wrapped Throwable. */ + private Throwable wrapped; + + /** + * Create a MOAException. + * + * @param messageId The message ID of the message contained in the created + * MOAException. + * @param parameters The parameters needed to fill in the message arguments. + */ + public MOAException(String messageId, Object[] parameters) { + super(MessageProvider.getInstance().getMessage(messageId, parameters)); + this.messageId = messageId; + } + + /** + * Create a MOAException. + * + * @param messageId The message ID of the message contained in the created + * MOAException. + * @param parameters The parameters needed to fill in the message arguments. + * @param wrapped The exception wrapped by the created + * MOAException. + */ + public MOAException(String messageId, Object[] parameters, Throwable wrapped) { + // TODO: remove wrapped again from super constructor + super(MessageProvider.getInstance().getMessage(messageId, parameters), wrapped); + this.messageId = messageId; + this.wrapped = wrapped; + } + + /** + * Returns the message ID of this exception. + * + * @return The message ID as provided in the constructor. + */ + public String getMessageId() { + return messageId; + } + + /** + * Returns the exception wrapped by this MOAException. + * + * @return The exception wrapped by this exception. Possibly + * null, if none was provided at construction time. + */ + public Throwable getWrapped() { + return wrapped; + } + + /** + * Convert this MOAException to an ErrorResponse + * element from the MOA namespace. + * + * @return An ErrorResponse element, containing the subelements + * ErrorCode and Info required by the MOA schema. + */ + public Element toErrorResponse() { + DocumentBuilder builder; + DOMImplementation impl; + Document doc; + Element errorResponse; + Element errorCode; + Element info; + + // create a new document + try { + builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + impl = builder.getDOMImplementation(); + } catch (ParserConfigurationException e) { + return null; + } + + // build the ErrorResponse element + doc = impl.createDocument(Constants.MOA_NS_URI, "ErrorResponse", null); + errorResponse = doc.getDocumentElement(); + + // add MOA namespace declaration + errorResponse.setAttributeNS( + Constants.XMLNS_NS_URI, + "xmlns", + Constants.MOA_NS_URI); + + // build the child elements + errorCode = doc.createElementNS(Constants.MOA_NS_URI, "ErrorCode"); + errorCode.appendChild(doc.createTextNode(messageId)); + info = doc.createElementNS(Constants.MOA_NS_URI, "Info"); + info.appendChild(doc.createTextNode(getMessage())); + errorResponse.appendChild(errorCode); + errorResponse.appendChild(info); + return errorResponse; + } + + /** + * Print a stack trace of this exception to System.err. + * + * @see java.lang.Throwable#printStackTrace() + */ + public void printStackTrace() { + printStackTrace(System.err); + } + + /** + * Print a stack trace of this exception, including the wrapped exception. + * + * @param s The stream to write the stack trace to. + * @see java.lang.Throwable#printStackTrace(java.io.PrintStream) + */ + public void printStackTrace(PrintStream s) { + super.printStackTrace(s); + if (getWrapped() != null) { + s.print("Caused by: "); + getWrapped().printStackTrace(s); + } + } + + /** + * Print a stack trace of this exception, including the wrapped exception. + * + * @param s The stream to write the stacktrace to. + * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter) + */ + public void printStackTrace(PrintWriter s) { + super.printStackTrace(s); + if (getWrapped() != null) { + s.print("Caused by: "); + getWrapped().printStackTrace(s); + } + } + +} -- cgit v1.2.3