View Javadoc

1   /*
2    *  Copyright (c) 2008 Rodrigo Ruiz
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.apache.axis.message.addressing;
17  
18  import javax.xml.namespace.QName;
19  import javax.xml.soap.SOAPException;
20  
21  import org.apache.axis.AxisFault;
22  import org.apache.axis.MessageContext;
23  import org.apache.axis.message.MessageElement;
24  import org.apache.axis.message.SOAPHeaderElement;
25  import org.apache.axis.soap.SOAPConstants;
26  
27  /**
28   * AddressingHeaderFault type.
29   *
30   * @author Davanum Srinivas
31   * @author Rodrigo Ruiz
32   * @version $Revision: 14 $
33   */
34  public class AddressingHeaderFault extends AxisFault {
35  
36    /**
37     * <code>serialVersionUID</code> attribute.
38     */
39    private static final long serialVersionUID = -7037754917195807959L;
40  
41    /**
42     * Namespace used by all constants in this class.
43     */
44    private static final String NS = Constants.NS_URI_ADDRESSING_2005_08;
45  
46    /**
47     * Invalid address header fault name.
48     */
49    private static final QName INVALID_ADDRESSING_HEADER = new QName(NS,
50      "InvalidAddressingHeader");
51  
52    /**
53     * Invalid address fault name.
54     */
55    public static final QName INVALID_ADDRESS = new QName(NS, "InvalidAddress");
56  
57    /**
58     * Invalid cardinality fault name.
59     */
60    public static final QName INVALID_CARDINALITY = new QName(NS, "InvalidCardinality");
61  
62    /**
63     * Header required fault name.
64     */
65    public static final QName HEADER_REQUIRED = new QName(NS,
66      "MessageAddressingHeaderRequired");
67  
68    /**
69     * Invalid EPR fault name.
70     */
71    public static final QName INVALID_EPR = new QName(NS, "InvalidEPR");
72  
73    /**
74     * Missing address fault name.
75     */
76    public static final QName MISSING_ADDRESS = new QName(NS, "MissingAddressInEPR");
77  
78    /**
79     * Duplicate Message ID fault name.
80     */
81    public static final QName DUPLICATE_MSGID = new QName(NS, "DuplicateMessageID");
82  
83    /**
84     * Action mismatch fault name.
85     */
86    public static final QName ACTION_MISMATCH = new QName(NS, "ActionMismatch");
87  
88    /**
89     * Problem header name.
90     */
91    public static final QName PROBLEM_HEADER = new QName(NS, "ProblemHeaderQName");
92  
93    /**
94     * Fault detail field name.
95     */
96    public static final QName FAULT_DETAIL = new QName(NS, "FaultDetail");
97  
98    /**
99     * Creates a new instance.
100    *
101    * @param faultString Error message
102    */
103   public AddressingHeaderFault(String faultString) {
104     this(faultString, null, null);
105   }
106 
107   /**
108    * Creates an instance, using a predefined message depending on the sub-code.
109    *
110    * @param subSubCode    Error sub-code
111    * @param problemHeader Problematic header name
112    */
113   public AddressingHeaderFault(QName subSubCode, String problemHeader) {
114     this(buildMessage(subSubCode, problemHeader), subSubCode, problemHeader);
115   }
116 
117   /**
118    * Creates a new instance.
119    *
120    * @param faultString Error message
121    * @param subSubCode Error code
122    * @param problemHeader Problem header
123    */
124   public AddressingHeaderFault(String faultString, QName subSubCode,
125     String problemHeader) {
126     MessageContext mc = MessageContext.getCurrentContext();
127 
128     if (mc == null || mc.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) {
129       this.setFaultCode(org.apache.axis.Constants.FAULT_SOAP12_SENDER);
130       if (HEADER_REQUIRED.equals(subSubCode)) {
131         this.addFaultSubCode(HEADER_REQUIRED);
132       } else {
133         this.addFaultSubCode(INVALID_ADDRESSING_HEADER);
134         if (subSubCode != null) {
135           this.addFaultSubCode(subSubCode);
136         }
137       }
138 
139       if (problemHeader != null) {
140         this.addFaultDetail(PROBLEM_HEADER, "wsa:" + problemHeader);
141       }
142     } else {
143       if (subSubCode != null && subSubCode.equals(HEADER_REQUIRED)) {
144         this.setFaultCode(HEADER_REQUIRED);
145       } else {
146         this.setFaultCode(INVALID_ADDRESSING_HEADER);
147       }
148 
149       if (problemHeader != null) {
150         try {
151           SOAPHeaderElement detailHeader = new SOAPHeaderElement(FAULT_DETAIL);
152           MessageElement problemEl = new MessageElement(PROBLEM_HEADER, "wsa:"
153             + problemHeader);
154           detailHeader.addChild(problemEl);
155           this.addHeader(detailHeader);
156         } catch (SOAPException e) {
157           // Will never happen, but... Log?
158           log.error(e);
159         }
160       }
161     }
162 
163     this.setFaultString(faultString);
164   }
165 
166   /**
167    * Creates a new instance.
168    *
169    * @param faultString Error message
170    * @param problemHeader Header error source
171    */
172   public AddressingHeaderFault(String faultString, String problemHeader) {
173     this(faultString, null, problemHeader);
174   }
175 
176   /**
177    * Gets a message that will depend on the specified code and header name.
178    *
179    * @param code   Error code
180    * @param header Header name
181    * @return Error message
182    */
183   private static String buildMessage(QName code, String header) {
184     String localName = (code == null) ? INVALID_ADDRESSING_HEADER.getLocalPart()
185                                       : code.getLocalPart();
186     if ("InvalidCardinality".equals(localName)) {
187       return "Duplicate " + header + " header";
188     } else {
189       return "Error in " + header + " header";
190     }
191   }
192 }