001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.addressing;
020:
021: import org.apache.axis2.AxisFault;
022: import org.apache.axis2.Constants;
023: import org.apache.axis2.context.MessageContext;
024: import org.apache.axis2.description.AxisOperation;
025: import org.apache.axis2.description.Parameter;
026: import org.apache.axis2.util.LoggingControl;
027: import org.apache.axis2.util.Utils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.util.Map;
032:
033: public class AddressingHelper {
034:
035: private static final Log log = LogFactory
036: .getLog(AddressingHelper.class);
037:
038: /**
039: * Returns true if the ReplyTo address does not match one of the supported
040: * anonymous urls. If the ReplyTo is not set, anonymous is assumed, per the Final
041: * spec. The AddressingInHandler should have set the ReplyTo to non-null in the
042: * 2004/08 case to ensure the different semantics. (per AXIS2-885)
043: *
044: * @param messageContext
045: */
046: public static boolean isReplyRedirected(
047: MessageContext messageContext) {
048: EndpointReference replyTo = messageContext.getReplyTo();
049: if (replyTo == null) {
050: if (LoggingControl.debugLoggingAllowed
051: && log.isDebugEnabled()) {
052: log
053: .debug(messageContext.getLogIDString()
054: + " isReplyRedirected: ReplyTo is null. Returning false");
055: }
056: return false;
057: } else {
058: return !replyTo.hasAnonymousAddress();
059: }
060: }
061:
062: /**
063: * Returns true if the FaultTo address does not match one of the supported
064: * anonymous urls. If the FaultTo is not set, the ReplyTo is checked per the
065: * spec.
066: *
067: * @param messageContext
068: * @see #isReplyRedirected(org.apache.axis2.context.MessageContext)
069: */
070: public static boolean isFaultRedirected(
071: MessageContext messageContext) {
072: EndpointReference faultTo = messageContext.getFaultTo();
073: if (faultTo == null) {
074: if (LoggingControl.debugLoggingAllowed
075: && log.isDebugEnabled()) {
076: log
077: .debug(messageContext.getLogIDString()
078: + " isReplyRedirected: FaultTo is null. Returning isReplyRedirected");
079: }
080: return isReplyRedirected(messageContext);
081: } else {
082: return !faultTo.hasAnonymousAddress();
083: }
084: }
085:
086: /**
087: * If the inbound FaultTo header was invalid and caused a fault, the fault should not be
088: * sent to it.
089: *
090: * @return true if the fault should be sent to the FaultTo
091: */
092: public static boolean shouldSendFaultToFaultTo(
093: MessageContext messageContext) {
094: // there are some information that the fault thrower wants to pass to the fault path.
095: // Means that the fault is a ws-addressing one hence use the ws-addressing fault action.
096: Object faultInfoForHeaders = messageContext
097: .getProperty(Constants.FAULT_INFORMATION_FOR_HEADERS);
098: // if the exception is due to a problem in the faultTo header itself, we can not use those
099: // fault informatio to send the error. Try to send using replyTo, leave it to transport
100: boolean doNotSendFaultUsingFaultTo = false;
101: if (faultInfoForHeaders != null) {
102: // TODO: This should probably store a QName instead of a String.. currently we rely on prefix string matching!!
103: String problemHeaderName = (String) ((Map) faultInfoForHeaders)
104: .get(AddressingConstants.Final.FAULT_HEADER_PROB_HEADER_QNAME);
105: doNotSendFaultUsingFaultTo = (problemHeaderName != null && (AddressingConstants.WSA_DEFAULT_PREFIX
106: + ":" + AddressingConstants.WSA_FAULT_TO)
107: .equals(problemHeaderName));
108: }
109: return !doNotSendFaultUsingFaultTo;
110: }
111:
112: /**
113: * Extract the parameter representing the Anonymous flag from the AxisOperation
114: * and return the String value. Return the default of "optional" if not specified.
115: *
116: * @param axisOperation
117: */
118: public static String getAnonymousParameterValue(
119: AxisOperation axisOperation) {
120: String value = "";
121: if (axisOperation != null) {
122: value = Utils
123: .getParameterValue(axisOperation
124: .getParameter(AddressingConstants.WSAW_ANONYMOUS_PARAMETER_NAME));
125: if (LoggingControl.debugLoggingAllowed
126: && log.isDebugEnabled()) {
127: log.debug("getAnonymousParameterValue: value: '"
128: + value + "'");
129: }
130: }
131:
132: if (value == null || "".equals(value.trim())) {
133: value = "optional";
134: }
135: return value.trim();
136: }
137:
138: /**
139: * Set the value of an existing unlocked Parameter representing Anonymous or add a new one if one
140: * does not exist. If a locked Parameter of the same name already exists the method will trace and
141: * return.
142: *
143: * @param axisOperation
144: * @param value
145: */
146: public static void setAnonymousParameterValue(
147: AxisOperation axisOperation, String value) {
148: if (value == null) {
149: if (LoggingControl.debugLoggingAllowed
150: && log.isDebugEnabled()) {
151: log
152: .debug("setAnonymousParameterValue: value passed in is null. return");
153: }
154: return;
155: }
156:
157: Parameter param = axisOperation
158: .getParameter(AddressingConstants.WSAW_ANONYMOUS_PARAMETER_NAME);
159: // If an existing parameter exists
160: if (param != null) {
161: if (LoggingControl.debugLoggingAllowed
162: && log.isDebugEnabled()) {
163: log
164: .debug("setAnonymousParameterValue: Parameter already exists");
165: }
166: // and is not locked
167: if (!param.isLocked()) {
168: if (LoggingControl.debugLoggingAllowed
169: && log.isDebugEnabled()) {
170: log
171: .debug("setAnonymousParameterValue: Parameter not locked. Setting value: "
172: + value);
173: }
174: // set the value
175: param.setValue(value);
176: }
177: } else {
178: // otherwise, if no Parameter exists
179: if (LoggingControl.debugLoggingAllowed
180: && log.isDebugEnabled()) {
181: log
182: .debug("setAnonymousParameterValue: Parameter does not exist");
183: }
184: // Create new Parameter with correct name/value
185: param = new Parameter();
186: param
187: .setName(AddressingConstants.WSAW_ANONYMOUS_PARAMETER_NAME);
188: param.setValue(value);
189: try {
190: if (LoggingControl.debugLoggingAllowed
191: && log.isDebugEnabled()) {
192: log
193: .debug("setAnonymousParameterValue: Adding parameter with value: "
194: + value);
195: }
196: // and add it to the AxisOperation object
197: axisOperation.addParameter(param);
198: } catch (AxisFault af) {
199: // This should not happen. AxisFault is only ever thrown when a locked Parameter
200: // of the same name already exists and this should be dealt with by the outer
201: // if statement.
202: if (LoggingControl.debugLoggingAllowed
203: && log.isDebugEnabled()) {
204: log
205: .debug("setAnonymousParameterValue: addParameter failed: "
206: + af.getMessage());
207: }
208: }
209: }
210: }
211: }
|