/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed 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;
createDetail();
}
public BaseFault(QName name, String message, Throwable cause)
{
super(message, cause);
_name = name;
createDetail();
}
public BaseFault(QName name, Throwable cause)
{
super(cause);
_name = name;
createDetail();
}
/**
*
* This method adds the wsrf-bf:BaseFault derived fault XML to the
* Detail section of the SOAP fault.
*
*/
protected void createDetail()
{
Element baseFault = XmlUtils.createElement(getName(), getMessage());
setDetail(baseFault);
}
public final String getDescription()
{
return getMessage();
}
public final Element getErrorCode()
{
return _errorCode;
}
public final String getErrorCodeDialect()
{
return _errorCodeDialect;
}
public final QName getName()
{
return _name;
}
public final EndpointReference getOriginReference()
{
return _origin;
}
public final Date getTimestamp()
{
return _timestamp;
}
public final void setErrorCode(Element errorCode, String errorCodeDialect)
{
_errorCode = errorCode;
_errorCodeDialect = errorCodeDialect;
}
public final void setOriginReference(EndpointReference origin)
{
_origin = new EndpointReference(origin, WsbfConstants.ORIGINATOR_QNAME);
}
}