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: */package org.apache.cxf.message;
019:
020: import org.w3c.dom.Node;
021:
022: /**
023: * Holder for utility methods relating to messages.
024: */
025: public final class MessageUtils {
026:
027: /**
028: * Prevents instantiation.
029: */
030: private MessageUtils() {
031: }
032:
033: /**
034: * Determine if message is outbound.
035: *
036: * @param message the current Message
037: * @return true iff the message direction is outbound
038: */
039: public static boolean isOutbound(Message message) {
040: Exchange exchange = message.getExchange();
041: return message != null
042: && exchange != null
043: && (message == exchange.getOutMessage() || message == exchange
044: .getOutFaultMessage());
045: }
046:
047: /**
048: * Determine if message is fault.
049: *
050: * @param message the current Message
051: * @return true iff the message is a fault
052: */
053: public static boolean isFault(Message message) {
054: return message != null
055: && message.getExchange() != null
056: && (message == message.getExchange()
057: .getInFaultMessage() || message == message
058: .getExchange().getOutFaultMessage());
059: }
060:
061: /**
062: * Determine the fault mode for the underlying (fault) message
063: * (for use on server side only).
064: *
065: * @param message the fault message
066: * @return the FaultMode
067: */
068: public static FaultMode getFaultMode(Message message) {
069: if (message != null
070: && message.getExchange() != null
071: && message == message.getExchange()
072: .getOutFaultMessage()) {
073: FaultMode mode = message.get(FaultMode.class);
074: if (null != mode) {
075: return mode;
076: } else {
077: return FaultMode.RUNTIME_FAULT;
078: }
079: }
080: return null;
081: }
082:
083: /**
084: * Determine if current messaging role is that of requestor.
085: *
086: * @param message the current Message
087: * @return true iff the current messaging role is that of requestor
088: */
089: public static boolean isRequestor(Message message) {
090: Boolean requestor = (Boolean) message
091: .get(Message.REQUESTOR_ROLE);
092: return requestor != null && requestor.booleanValue();
093: }
094:
095: /**
096: * Determine if the current message is a partial response.
097: *
098: * @param message the current message
099: * @return true iff the current messags is a partial response
100: */
101: public static boolean isPartialResponse(Message message) {
102: return Boolean.TRUE.equals(message
103: .get(Message.PARTIAL_RESPONSE_MESSAGE));
104: }
105:
106: /**
107: * Returns true if a value is either the String "true" or Boolean.TRUE.
108: * @param value
109: * @return true iff value is either the String "true" or Boolean.TRUE
110: */
111: public static boolean isTrue(Object value) {
112: if (Boolean.TRUE.equals(value) || "true".equals(value)) {
113: return true;
114: }
115:
116: return false;
117: }
118:
119: /**
120: * Returns true if the underlying content format is a W3C DOM or a SAAJ message.
121: */
122: public static boolean isDOMPresent(Message m) {
123: for (Class c : m.getContentFormats()) {
124: if (c.equals(Node.class)
125: || c.getName().equals("javax.xml.soap.SOAPMessage")) {
126: return true;
127: }
128: }
129: return false;
130: }
131:
132: }
|