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