/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.ws.resource.basefaults;
import java.text.ParseException;
import java.util.Date;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.util.xml.XsdUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
/**
*
* BaseFault is Muse's default implementation of the WS-BaseFaults BaseFault type.
* This can be used as a standalone class or (more commonly) as a base class for
* more concrete fault types. WS-BF XML parsing and serialization is also handled
* by this class.
*
* @author Dan Jemiolo (danj)
*
*/
public abstract class BaseFault extends SoapFault
{
private static final long serialVersionUID = 7582724758621882686L;
private Element _errorCode = null;
private String _errorCodeDialect = null;
private QName _name = null;
//
// Where the fault was generated
//
private EndpointReference _origin = null;
//
// When the fault was generated
//
private Date _timestamp = new Date();
/**
*
* Creates a new BaseFault by parsing the given XML according to the
* WS-BF spec. This constructor only checks for elements and values
* defined in WS-BF and does not fail if extensions are added to the
* fault type.
* <br><br>
* The timestamp for this fault is the one specified in the XML,
* regardless of when this object is created.
*
* @param xml
* The XML representation of a WS-BF BaseFault.
*
*/
public BaseFault(Element xml)
{
//
// set the exception message to be the WS-BF Description
//
super(xml);
Element detail = getDetail();
_name = XmlUtils.getElementQName(detail);
//
// set the timestamp - this is a required field
//
String timeText = XmlUtils.getElementText(detail, WsbfConstants.TIMESTAMP_QNAME);
try
{
if (timeText != null)
_timestamp = XsdUtils.getLocalTime(timeText);
}
catch (ParseException error)
{
//
// nothing we can do - throwing on a fault creation won't help us.
// this fault will just use the system's current timestamp.
//
}
//
// set the origin EPR (optional)
//
Element eprXML = XmlUtils.getElement(detail, WsbfConstants.ORIGINATOR_QNAME);
try
{
if (eprXML != null)
setOriginReference(new EndpointReference(eprXML, false));
}
catch (Throwable error)
{
//
// NOTE: This error occurs if the BaseFault XML had an invalid
// EPR definition for wsbf:OriginReference. Because this
// fault may be coming from an external service, we do
// not want to throw - the only thing worse than having a
// fault with incomplete data is having a runtime exception
// thrown when you really want to respond to the fault.
//
}
//
// set the error code (optional)
//
Element errorXML = XmlUtils.getElement(detail, WsbfConstants.ERROR_CODE_QNAME);
if (errorXML != null)
{
Element error = XmlUtils.getFirstElement(errorXML);
String dialect = errorXML.getAttribute(WsbfConstants.ERROR_CODE_DIALECT);
setErrorCode(error, dialect);
}
}
public BaseFault(QName name, String message)
{
super(message);
_name = name;
}
public BaseFault(QName name, String message, Throwable cause)
{
super(message, cause);
_name = name;
}
public BaseFault(QName name, Throwable cause)
{
super(cause);
_name = name;
}
/**
*
* Creates the WSBF BaseFault XML on-the-fly, for inclusion in a larger
* SOAPFault XML fragment. The wsrf-bf:Description is not included because
* the same text is found in the SOAPFault's Reason element.
*
*/
public Element getDetail()
{
Element baseFault = XmlUtils.createElement(getName());
XmlUtils.setElement(baseFault, WsbfConstants.TIMESTAMP_QNAME, getTimestamp());
EndpointReference originEPR = getOriginReference();
if (originEPR != null)
{
Element originXML = originEPR.toXML();
baseFault.appendChild(originXML);
}
Element errorCode = getErrorCode();
if (errorCode != null)
{
Element errorXML = XmlUtils.createElement(WsbfConstants.ERROR_CODE_QNAME);
errorXML.setAttribute(WsbfConstants.ERROR_CODE_DIALECT, getErrorCodeDialect());
XmlUtils.setElement(errorXML, WsbfConstants.ERROR_CODE_QNAME, errorCode, false);
}
return baseFault;
}
public String getDescription()
{
return getMessage();
}
public Element getErrorCode()
{
return _errorCode;
}
public String getErrorCodeDialect()
{
return _errorCodeDialect;
}
public QName getName()
{
return _name;
}
public EndpointReference getOriginReference()
{
return _origin;
}
public Date getTimestamp()
{
return _timestamp;
}
public void setErrorCode(Element errorCode, String errorCodeDialect)
{
_errorCode = errorCode;
_errorCodeDialect = errorCodeDialect;
}
public void setOriginReference(EndpointReference origin)
{
_origin = new EndpointReference(origin, WsbfConstants.ORIGINATOR_QNAME);
}
}