View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf.faults;
36  
37  import java.util.GregorianCalendar;
38  
39  import javax.xml.namespace.QName;
40  
41  import org.apache.muse.util.xml.XmlUtils;
42  import org.apache.muse.util.xml.XsdUtils;
43  import org.apache.muse.ws.addressing.EndpointReference;
44  import org.apache.muse.ws.resource.WsrfConstants;
45  import org.apache.muse.ws.resource.basefaults.BaseFault;
46  import org.apache.muse.ws.resource.basefaults.WsbfConstants;
47  import org.ogf.schemas.graap.wsAgreement.ContinuingFaultDocument;
48  import org.ogf.schemas.graap.wsAgreement.ContinuingFaultType;
49  import org.w3c.dom.Element;
50  
51  /**
52   * AbstractAgreementFault
53   * 
54   * @author Oliver Waeldrich
55   * 
56   */
57  public abstract class AbstractAgreementFault extends BaseFault
58  {
59  
60      private static final long serialVersionUID = 1L;
61  
62      /**
63       * 
64       * @param element
65       *            the fault element
66       */
67      public AbstractAgreementFault( Element element )
68      {
69          super( element );
70      }
71  
72      /**
73       * 
74       * @param name
75       *            fault name
76       * 
77       * @param message
78       *            error message
79       */
80      public AbstractAgreementFault( QName name, String message )
81      {
82          super( name, message );
83      }
84  
85      /**
86       * 
87       * @param name
88       *            fault name
89       * @param message
90       *            error message
91       * @param throwable
92       *            cause
93       */
94      public AbstractAgreementFault( QName name, String message, Throwable throwable )
95      {
96          super( name, message, throwable );
97      }
98  
99      /**
100      * 
101      * @param name
102      *            fault name
103      * @param throwable
104      *            cause
105      */
106     public AbstractAgreementFault( QName name, Throwable throwable )
107     {
108         super( name, throwable );
109     }
110 
111     /**
112      * Creates the detail section of the fault with timestamp and description.
113      */
114     protected void createDetail()
115     {
116         Element baseFault = XmlUtils.createElement( getName() );
117 
118         Element timestamp =
119             XmlUtils.createElement( new QName( WsrfConstants.NAMESPACE_URI, "Timestamp", WsrfConstants.PREFIX ),
120                                     XsdUtils.getLocalTimeString( getTimestamp() ) );
121 
122         Element description =
123             XmlUtils.createElement( new QName( WsrfConstants.NAMESPACE_URI, "Description",
124                                                WsrfConstants.PREFIX ), getMessage() );
125 
126         baseFault.appendChild( timestamp );
127         baseFault.appendChild( description );
128 
129         setDetail( baseFault );
130     }
131 
132     /*
133      * (non-Javadoc)
134      * 
135      * @see org.apache.muse.ws.resource.basefaults.BaseFault#getDetail()
136      */
137     @Override
138     public Element getDetail()
139     {
140         Element baseFault = XmlUtils.createElement( getName() );
141         XmlUtils.setElement( baseFault, WsbfConstants.TIMESTAMP_QNAME, getTimestamp() );
142 
143         EndpointReference originEPR = getOriginReference();
144 
145         if ( originEPR != null )
146         {
147             Element originXML = originEPR.toXML();
148             baseFault.appendChild( originXML );
149         }
150 
151         Element errorCode = getErrorCode();
152 
153         if ( errorCode != null )
154         {
155             Element errorXML = XmlUtils.createElement( WsbfConstants.ERROR_CODE_QNAME );
156             errorXML.setAttribute( WsbfConstants.ERROR_CODE_DIALECT, getErrorCodeDialect() );
157             XmlUtils.setElement( errorXML, WsbfConstants.ERROR_CODE_QNAME, errorCode, false );
158             baseFault.appendChild( errorXML );
159         }
160 
161         try
162         {
163             //
164             // root cause message is included into the SOAP error,
165             // we try to go one level deeper.
166             //
167             Throwable cause = getCause().getCause();
168             if ( cause != null )
169             {
170                 ContinuingFaultType fault =
171                     ContinuingFaultDocument.Factory.newInstance().addNewContinuingFault();
172                 fault.addNewDescription();
173                 fault.getDescriptionArray( 0 ).setLang( "en" );
174                 fault.getDescriptionArray( 0 ).setStringValue( cause.getMessage() );
175                 GregorianCalendar cal = new GregorianCalendar();
176                 cal.setTime( getTimestamp() );
177                 fault.setTimestamp( cal );
178                 Element causeXML = XmlUtils.createElement( WsbfConstants.FAULT_CAUSE_QNAME );
179                 XmlUtils.setElement( causeXML, ContinuingFaultDocument.type.getDocumentElementName(),
180                                      fault.getDomNode(), true );
181                 XmlUtils.setElement( baseFault, WsbfConstants.FAULT_CAUSE_QNAME, causeXML, true );
182             }
183         }
184         catch ( Exception e )
185         {
186             // do nothing
187         }
188 
189         return baseFault;
190     }
191 }