View Javadoc

1   /*
2    * Copyright (c) 2004 Peter Antman, Mogul  <peter.antman@mogul.com>
3    *
4    * $Id: ServiceMessage.java,v 1.1.1.1 2004/05/19 12:22:46 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.amsterdam.service;
21  
22  import java.io.InputStream;
23  
24  import org.backsource.amsterdam.metadata.ContentMapEntry;
25  /***
26    * Is the message sent through the system, should be constructed
27   * by the ProtocolHandler.
28  
29     The content of a ServiceMessage may be manipulated on the way
30     down the invoker chain (eg from SGML to XML), but the message 
31     object in itself may not be changed to a new one.
32  
33     FIXME - shoild not an url be part of this?
34   *
35   *
36   * @author <a href="mailto:pra@mogul.com">Peter Antman</a>
37   * @version $Revision: 1.1.1.1 $
38   */
39  
40  public class ServiceMessage {
41     private ContentMapEntry entry;
42     private String url;
43     private boolean hasStream = false;
44     private boolean isBinary = true;//You never bo, do you
45     private String message;
46     private String mime;
47     private InputStream is;
48     private String response;
49     private Object objectMessage;
50     private boolean hasObjectMessage = false;
51     private String name = null;
52  
53     // FIXME, do we need the URL or even the ContentEntry?
54     public ServiceMessage(ContentMapEntry entry,
55                           String url,
56                           String mime, 
57                           String message) {
58        this.entry = entry;
59        this.url = url;
60        this.name = url;
61        this.mime = mime;
62        this.message = message;
63        isBinary = false;
64        hasStream = false; //Just in case
65  	
66     }
67  
68     // FIME - should it be possible to construct messages without a ContentMapEntry?
69     public ServiceMessage(
70                           String url,
71                           String mime, 
72                           String message) {
73        this(null,url,mime,message);
74     }
75      
76     public ServiceMessage(
77                           String url,
78                           String mime, 
79                           InputStream is) {
80        this(null,url,mime,is);
81     }
82      
83     public ServiceMessage(ContentMapEntry entry,
84                           String url,
85                           String mime, 
86                           InputStream is) {
87        this.entry = entry;
88        this.url = url;
89        this.name = url;
90        this.is = is;
91        hasStream = true;
92        //isBinary = MimeHelper.isBinary(mime);
93     }
94  
95     public ServiceMessage(Object message) {
96        this.objectMessage = message;
97        hasObjectMessage = true;
98     }
99     public ContentMapEntry getContentMapEntry() {
100       return entry;
101    }
102 
103    public void setContentMapEntry(ContentMapEntry entry) {
104       this.entry = entry;
105    }
106     
107    public String getUrl() {
108       return url;
109    }
110   
111    public void setUrl(String url) {
112       this.url = url;
113    }
114   
115    public String getName() {
116       return name;
117    }
118   
119    public void setName(String name) {
120       this.name = name;
121    }
122 
123    /***
124     * Check if message body is carried inside a stream
125     * If it is use getStream() else use getMessage()
126     */
127    public boolean hasStream() {
128       return hasStream;
129    }
130 
131    protected void hasStream(boolean hasStream) {
132       this.hasStream = hasStream;
133    }
134 
135    /***
136     * Check if message body is binary
137     */
138    public boolean isBinary() {
139       return isBinary;
140    }
141 
142    protected void isBinary(boolean isBinary) {
143       this.isBinary = isBinary;
144    }
145 
146    /***
147     * Get mime type
148     */
149    public String getMime() {
150       return mime;
151    }
152  
153    public void setMime(String mime) {
154       this.mime = mime;
155    }
156 
157    /***
158     * Get a stream to the underlying message. The creator
159     * of the stream <i>may not</i> count on that the message has
160     * reached the final destination when it is closed.
161     */
162    public InputStream getStream() {
163       return is;
164    }
165     
166    /***
167     * Set an InputStream, overwriting the previus one, if you
168     * do this, see to it that the original one was closed first.
169     *
170     * May be set to null, hasStream will then be set to false
171     */
172    public void setInputStream(InputStream is) {
173       this.is = is;
174       if (is != null) {
175          hasStream = true;
176          hasObjectMessage = false;
177       }else
178          hasStream = false;
179    }
180 
181    /***
182     * Get the message. Only if its text, and currently only if
183     * it was set
184     */ 
185    public String getMessage() {
186       return message;//Or should be convert from is to string
187       //if that the only one available
188    }
189 
190    /***
191     * Set a message, overwrite the prevous one.
192     */
193     
194    public void setMessage(String message) {
195       this.message = message;
196       isBinary = false;
197       hasStream = false;
198       hasObjectMessage = false;
199    }
200 
201 
202    public void setObjectMessage(Object message) {
203       this.objectMessage = message;
204       hasObjectMessage = true;
205    }
206 
207    public Object getObjectMessage() {
208       return objectMessage;
209    }
210 
211    public boolean hasObjectMessage() {
212       return hasObjectMessage;
213    }
214 
215    /***
216     * Get any response message
217     */
218    public String getResponse() {
219       return response;
220    }
221 
222    /***
223     * A publisher (or some other party in the chain) may fill in
224     * a response note (in xml). It is up to the application
225     * assembler to see to it that the parts agree on a format if
226     * this feature is used.
227     */
228    public void setResponse(String response) {
229       this.response = response;
230    }
231 
232     
233    
234 }// ServiceMessage