View Javadoc

1   package org.codehaus.xfire;
2   
3   import java.io.InputStream;
4   import java.io.OutputStream;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import javax.xml.stream.XMLInputFactory;
9   import javax.xml.stream.XMLStreamException;
10  import javax.xml.stream.XMLStreamReader;
11  
12  import org.codehaus.xfire.service.Service;
13  import org.codehaus.xfire.soap.SoapVersion;
14  import org.codehaus.xfire.soap.SoapVersionFactory;
15  import org.codehaus.xfire.transport.Session;
16  import org.codehaus.xfire.transport.Transport;
17  
18  /***
19   * Holds inforrmation about the message request and response.
20   * 
21   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
22   * @since Feb 13, 2004
23   */
24  public class MessageContext
25  {
26      private static ThreadLocal messageContexts = new ThreadLocal();
27      
28      private Session session;
29      private OutputStream responseStream;
30      private String requestUri;
31      private String serviceName;
32      private String action;
33      private Map properties;
34      private SoapVersion soapVersion;
35      private Service service;
36      private Transport transport;
37      private XMLStreamReader xmlStreamReader;
38      
39      public MessageContext()
40      {
41          properties = new HashMap();
42          messageContexts.set(this);
43      }
44      
45      /***
46       * Create a MessageContext to invoke a service with the
47       * specified document as the request.
48       */
49      public MessageContext( String service,
50                             String action,
51                             OutputStream response, 
52                             Session session, 
53                             String requestUri )
54      {
55          messageContexts.set(this);
56          
57          properties = new HashMap();
58          
59          this.serviceName = service;
60          this.action = action;
61          this.responseStream = response;
62          this.session = session;
63          this.requestUri = requestUri;
64      }
65  
66      /***
67       * Get the message context for the current thread.
68       * 
69       * @return The current MessageContext or null if there is none.
70       */
71      public static MessageContext getCurrentMessageContext()
72      {
73          return (MessageContext) messageContexts.get();
74      }
75  
76      public Object getProperty( Object key )
77      {
78      	return properties.get(key);
79      }
80      
81      public void setProperty( Object key, Object value )
82      {
83      	properties.put(key, value);
84      }
85      
86      public String getRequestUri()
87      {
88          return requestUri;
89      }
90  
91      public void setRequestUri( String requestUri )
92      {
93          this.requestUri = requestUri;
94      }
95      
96      public OutputStream getResponseStream()
97      {
98          return responseStream;
99      }
100     
101     public void setResponseStream( OutputStream responseStream )
102     {
103         this.responseStream = responseStream;
104     }
105     
106     public void setRequestStream( InputStream requestStream )
107     {
108         try
109         {
110             XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
111             XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(requestStream);
112             setXMLStreamReader(xmlStreamReader);
113         }
114         catch (XMLStreamException e)
115         {
116             throw (IllegalArgumentException)new IllegalArgumentException("Invalid xml request stream").initCause(e);
117         }
118     }
119 
120     /***
121      * The session that this request is a part of.
122      * 
123      * @return
124      */
125     public Session getSession()
126     {
127         return session;
128     }
129    
130     public void setSession( Session session )
131     {
132         this.session = session;
133     }
134     
135     public String getAction()
136     {
137         return action;
138     }
139     
140     public void setAction( String action )
141     {
142         this.action = action;
143     }
144     
145     public String getServiceName()
146     {
147         return serviceName;
148     }
149     
150     public void setServiceName( String service )
151     {
152         this.serviceName = service;
153     }
154 
155     public SoapVersion getSoapVersion()
156     {
157         return soapVersion;
158     }
159     
160     public void setSoapVersion( String soapVersion )
161     {
162         this.soapVersion = SoapVersionFactory.getInstance().getSoapVersion(soapVersion);
163     }
164     
165     /***
166      * The service being invoked.
167      * 
168      * @return
169      */
170     public Service getService()
171     {
172         return service;
173     }
174     
175     public void setService( Service service )
176     {
177         this.service = service;
178     }
179     
180     /***
181      * @return Returns the xmlStreamReader.
182      */
183     public XMLStreamReader getXMLStreamReader()
184     {
185         return xmlStreamReader;
186     }
187     
188     /***
189      * @param xmlStreamReader The xmlStreamReader to set.
190      */
191     public void setXMLStreamReader(XMLStreamReader xmlStreamReader)
192     {
193         this.xmlStreamReader = xmlStreamReader;
194     }
195     
196     /***
197      * @return Returns the transport.
198      */
199     public Transport getTransport()
200     {
201         return transport;
202     }
203     /***
204      * @param transport The transport to set.
205      */
206     public void setTransport(Transport transport)
207     {
208         this.transport = transport;
209     }
210 }