001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.policy.addressing;
019    
020    import org.codehaus.activesoap.MessageExchange;
021    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
022    import org.codehaus.activesoap.handler.stax.ElementContent;
023    import org.codehaus.activesoap.handler.stax.StaxHelper;
024    
025    import javax.xml.namespace.QName;
026    import javax.xml.stream.XMLStreamException;
027    import javax.xml.stream.XMLStreamWriter;
028    import java.util.HashMap;
029    import java.util.Map;
030    
031    /**
032     * The current WS-Address context for the current invocation context.
033     *
034     * @version $Revision: 1.5 $
035     */
036    public class AddressingContext implements ElementContent {
037        public final static QName FaultTo_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "FaultTo", "wsa");
038        public final static QName Action_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "Action", "wsa");
039        public final static QName From_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "From", "wsa");
040        public final static QName Recipient_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "Recipient", "wsa");
041        public final static QName To_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "To", "wsa");
042        public final static QName ReplyTo_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "ReplyTo", "wsa");
043        public final static QName MessageID_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "MessageID", "wsa");
044        public final static QName RelatesTo_QNAME = new QName("http://schemas.xmlsoap.org/ws/2003/03/addressing", "RelatesTo", "wsa");
045    
046        private static final String CONTEXT_KEY = AddressingContext.class.getName();
047        private static final ThreadLocal threadLocal = new ThreadLocal();
048    
049        private AttributedURI messageID;
050        private Relationship relatesTo;
051        private AttributedURI to;
052        private AttributedURI action;
053        private EndpointReferenceType from;
054        private EndpointReferenceType replyTo;
055        private EndpointReferenceType faultTo;
056        private EndpointReferenceType recipient;
057        private Map properties = new HashMap();
058    
059        /**
060         * Returns the current WS-Addressing context of the current thread.
061         *
062         * @return the current addressing context or null if the current thread has not yet registered a thread local addressing context.
063         */
064        public static AddressingContext getContext() {
065            return (AddressingContext) threadLocal.get();
066        }
067    
068        /**
069         * Returns the current WS-Addressing context of this message exchange,
070         * lazily creating one if none is created yet
071         */
072        public static AddressingContext getContext(MessageExchange exchange) {
073            AddressingContext context = (AddressingContext) exchange.getProperty(CONTEXT_KEY);
074            if (context == null) {
075                context = new AddressingContext();
076                setContext(exchange, context);
077            }
078            return context;
079        }
080    
081        public static void setContext(MessageExchange exchange, AddressingContext context) {
082            exchange.setProperty(CONTEXT_KEY, context);
083            threadLocal.set(exchange);
084        }
085    
086        public AttributedURI getMessageID() {
087            return messageID;
088        }
089    
090        public void setMessageID(AttributedURI messageID) {
091            this.messageID = messageID;
092        }
093    
094        public Relationship getRelatesTo() {
095            return relatesTo;
096        }
097    
098        public void setRelatesTo(Relationship relatesTo) {
099            this.relatesTo = relatesTo;
100        }
101    
102        public AttributedURI getTo() {
103            return to;
104        }
105    
106        public void setTo(AttributedURI to) {
107            this.to = to;
108        }
109    
110        public AttributedURI getAction() {
111            return action;
112        }
113    
114        public void setAction(AttributedURI action) {
115            this.action = action;
116        }
117    
118        public EndpointReferenceType getFrom() {
119            return from;
120        }
121    
122        public void setFrom(EndpointReferenceType from) {
123            this.from = from;
124        }
125    
126        public EndpointReferenceType getReplyTo() {
127            return replyTo;
128        }
129    
130        public void setReplyTo(EndpointReferenceType replyTo) {
131            this.replyTo = replyTo;
132        }
133    
134        public EndpointReferenceType getFaultTo() {
135            return faultTo;
136        }
137    
138        public void setFaultTo(EndpointReferenceType faultTo) {
139            this.faultTo = faultTo;
140        }
141    
142        public EndpointReferenceType getRecipient() {
143            return recipient;
144        }
145    
146        public void setRecipient(EndpointReferenceType recipient) {
147            this.recipient = recipient;
148        }
149    
150        public void addProperty(QName name, Object value) {
151            properties.put(name, value);
152        }
153    
154        public Map getProperties() {
155            return properties;
156        }
157    
158        public void setProperties(Map properties) {
159            this.properties = properties;
160        }
161    
162        public void writeContent(AnyElementMarshaler marshaler, XMLStreamWriter out) throws XMLStreamException {
163            StaxHelper.writeElement(MessageID_QNAME, messageID, marshaler, out);
164            StaxHelper.writeElement(RelatesTo_QNAME, relatesTo, marshaler, out);
165            StaxHelper.writeElement(To_QNAME, to, marshaler, out);
166            StaxHelper.writeElement(Action_QNAME, action, marshaler, out);
167            StaxHelper.writeElement(From_QNAME, from, marshaler, out);
168            StaxHelper.writeElement(ReplyTo_QNAME, replyTo, marshaler, out);
169            StaxHelper.writeElement(FaultTo_QNAME, faultTo, marshaler, out);
170            StaxHelper.writeElement(Recipient_QNAME, recipient, marshaler, out);
171        }
172    }