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.api.exceptions;
36  
37  import java.text.MessageFormat;
38  import java.util.Calendar;
39  import java.util.GregorianCalendar;
40  
41  import org.apache.xmlbeans.XmlString;
42  import org.oasisOpen.docs.wsrf.bf2.BaseFaultDocument;
43  import org.oasisOpen.docs.wsrf.bf2.BaseFaultType;
44  import org.oasisOpen.docs.wsrf.bf2.BaseFaultType.Description;
45  import org.ogf.graap.wsag.api.WsagConstants;
46  
47  /**
48   * NegotiationException
49   * 
50   * @author Oliver Waeldrich
51   */
52  public class NegotiationException extends Exception
53  {
54  
55      /**
56       * Basefault error code.
57       */
58      public static final int ERROR_CODE = 1001;
59  
60      /**
61       * Default language identifier in base fault.
62       */
63      public static final String LOCAL_EN = "en";
64  
65      /**
66       * Definition of main error message.
67       */
68      private static final String DESCRIPTION_MESSAGE = "{0}: {1}";
69  
70      /**
71       * Definition of cause error message.
72       */
73      private static final String CAUSE_MESSAGE = "Caused by {0}: {1}";
74  
75      /**
76       * Definition of stack trace error message.
77       */
78      private static final String STACKTRACE_MESSAGE = "\tat {0}";
79  
80      /**
81       * exception timestamp
82       */
83      private Calendar timestamp = new GregorianCalendar();
84  
85      private static final long serialVersionUID = 1L;
86  
87      /**
88       * default constructor
89       */
90      public NegotiationException()
91      {
92          super();
93      }
94  
95      /**
96       * @param message
97       *            the exception message
98       * @param cause
99       *            the exception cause
100      */
101     public NegotiationException( String message, Throwable cause )
102     {
103         super( message, cause );
104     }
105 
106     /**
107      * @param message
108      *            the exception message
109      */
110     public NegotiationException( String message )
111     {
112         super( message );
113     }
114 
115     /**
116      * @param cause
117      *            the exception cause
118      */
119     public NegotiationException( Throwable cause )
120     {
121         super( cause );
122     }
123 
124     /**
125      * @return the base fault document representing this exception.
126      */
127     public BaseFaultType getBaseFault()
128     {
129         BaseFaultDocument baseFaultDocument = BaseFaultDocument.Factory.newInstance();
130         BaseFaultType baseFault = baseFaultDocument.addNewBaseFault();
131 
132         baseFault.addNewErrorCode();
133         baseFault.getErrorCode().setDialect( WsagConstants.WSAG4J_NAMESPACE );
134         baseFault.getErrorCode().set( XmlString.Factory.newValue( getErrorCode() ) );
135 
136         baseFault.setTimestamp( timestamp );
137 
138         Description description = baseFault.addNewDescription();
139         description.setLang( LOCAL_EN );
140         description.setStringValue( MessageFormat.format( DESCRIPTION_MESSAGE, new Object[] {
141             this.getClass().getName(), getMessage() } ) );
142 
143         StackTraceElement[] stackTrace = getStackTrace();
144         for ( int i = 0; i < stackTrace.length; i++ )
145         {
146             description = baseFault.addNewDescription();
147             description.setLang( LOCAL_EN );
148             description.setStringValue( MessageFormat.format( STACKTRACE_MESSAGE,
149                                                               new Object[] { stackTrace[i] } ) );
150         }
151 
152         if ( getCause() != null )
153         {
154             description = baseFault.addNewDescription();
155             description.setLang( LOCAL_EN );
156             description.setStringValue( MessageFormat.format( CAUSE_MESSAGE, new Object[] {
157                 getCause().getClass().getName(), getCause().getMessage() } ) );
158         }
159 
160         return baseFault;
161     }
162 
163     /**
164      * Specific exceptions must override this method.
165      * 
166      * @return the base fault error code for this exception
167      */
168     protected String getErrorCode()
169     {
170         return Integer.toString( ERROR_CODE );
171     }
172 }