001: package org.objectweb.celtix.bus.ws.rm;
002:
003: import java.math.BigInteger;
004: import java.util.Set;
005: import java.util.logging.Logger;
006:
007: import javax.annotation.Resource;
008: import javax.xml.namespace.QName;
009: import javax.xml.ws.handler.MessageContext;
010: import javax.xml.ws.handler.soap.SOAPHandler;
011: import javax.xml.ws.handler.soap.SOAPMessageContext;
012:
013: import org.objectweb.celtix.bindings.AbstractBindingBase;
014: import org.objectweb.celtix.bindings.ClientBinding;
015: import org.objectweb.celtix.bindings.JAXWSConstants;
016: import org.objectweb.celtix.bindings.ServerBinding;
017: import org.objectweb.celtix.bus.ws.addressing.AddressingPropertiesImpl;
018: import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
019: import org.objectweb.celtix.bus.ws.addressing.VersionTransformer;
020: import org.objectweb.celtix.common.logging.LogUtils;
021: import org.objectweb.celtix.ws.rm.Identifier;
022:
023: public class RMPersistenceHandler implements
024: SOAPHandler<SOAPMessageContext> {
025:
026: private static final Logger LOG = LogUtils
027: .getL7dLogger(RMPersistenceHandler.class);
028:
029: @Resource(name=JAXWSConstants.CLIENT_BINDING_PROPERTY)
030: private ClientBinding clientBinding;
031: @Resource(name=JAXWSConstants.SERVER_BINDING_PROPERTY)
032: private ServerBinding serverBinding;
033:
034: public Set<QName> getHeaders() {
035: return null;
036: }
037:
038: public void close(MessageContext arg0) {
039: }
040:
041: public boolean handleFault(SOAPMessageContext context) {
042: if (ContextUtils.isOutbound(context)) {
043: handleOutbound(context);
044: }
045: return true;
046: }
047:
048: public boolean handleMessage(SOAPMessageContext context) {
049: if (ContextUtils.isOutbound(context)) {
050: handleOutbound(context);
051: }
052: return true;
053: }
054:
055: void handleOutbound(SOAPMessageContext context) {
056: LOG.entering(getClass().getName(), "handleOutbound");
057: // do nothing unless this is an application message
058: if (!isApplicationMessage(context)) {
059: return;
060: }
061:
062: // tell the source to store a copy of the message in the
063: // retransmission queue
064: // and schedule the next retransmission
065:
066: RMPropertiesImpl rmpsOut = (RMPropertiesImpl) RMContextUtils
067: .retrieveRMProperties(context, true);
068: // assert null != rmpsOut;
069:
070: if (null == rmpsOut) {
071: // handler chain traversal may have been reversed before
072: // reaching RM logical handler - OK to ignore?
073: return;
074: }
075:
076: if (null == rmpsOut.getSequence()) {
077: // cannot be an application message (may be a partial response)
078: return;
079: }
080:
081: BigInteger mn = rmpsOut.getSequence().getMessageNumber();
082: boolean lm = null != rmpsOut.getSequence().getLastMessage();
083: Identifier sid = rmpsOut.getSequence().getIdentifier();
084:
085: // create a new SourceSequence object instead of retrieving the one
086: // maintained by the RM source for the sequence identifier
087: // as the current/last message number properties of the latter may have
088: // changed since
089:
090: SourceSequence seq = new SourceSequence(sid, null, null, mn, lm);
091: RMHandler handler = RMHandler.getHandler(getBinding());
092: RMMessageImpl msg = new RMMessageImpl(mn, context);
093:
094: handler.getSource().addUnacknowledged(seq, msg);
095: }
096:
097: AbstractBindingBase getBinding() {
098: if (null != clientBinding) {
099: return (AbstractBindingBase) clientBinding;
100: }
101: return (AbstractBindingBase) serverBinding;
102: }
103:
104: private boolean isApplicationMessage(SOAPMessageContext context) {
105: boolean isApplicationMessage = true;
106: AddressingPropertiesImpl maps = ContextUtils.retrieveMAPs(
107: context, false, true);
108:
109: if (null == maps) {
110: return false;
111: }
112:
113: // ensure the appropriate version of WS-Addressing is used
114: maps
115: .exposeAs(VersionTransformer.Names200408.WSA_NAMESPACE_NAME);
116:
117: String action = null;
118: if (maps != null && null != maps.getAction()) {
119: action = maps.getAction().getValue();
120: }
121: if (RMUtils.getRMConstants().getCreateSequenceAction().equals(
122: action)
123: || RMUtils.getRMConstants()
124: .getCreateSequenceResponseAction().equals(
125: action)
126: || RMUtils.getRMConstants()
127: .getTerminateSequenceAction().equals(action)
128: || RMUtils.getRMConstants().getLastMessageAction()
129: .equals(action)
130: || RMUtils.getRMConstants()
131: .getSequenceAcknowledgmentAction().equals(
132: action)
133: || RMUtils.getRMConstants().getSequenceInfoAction()
134: .equals(action)) {
135: isApplicationMessage = false;
136: }
137: return isApplicationMessage;
138: }
139:
140: }
|