KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > attachment > AttachmentImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.attachment;
23
24 import javax.activation.DataHandler JavaDoc;
25 import javax.mail.BodyPart JavaDoc;
26 import javax.mail.internet.MimeMultipart JavaDoc;
27 import javax.xml.rpc.ServiceException JavaDoc;
28 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
29 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
30 import javax.xml.rpc.server.ServletEndpointContext JavaDoc;
31 import javax.xml.soap.AttachmentPart JavaDoc;
32 import javax.xml.soap.SOAPMessage JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import java.awt.*;
35 import java.rmi.RemoteException JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Service Endpoint for the MIME mapping required by JAXRPC-1.1
40  *
41  * image/gif java.awt.Image
42  * image/jpeg java.awt.Image
43  * text/plain java.lang.String
44  * multipart/* javax.mail.internet.MimeMultipart
45  * text/xml javax.xml.transform.Source
46  * application/xml javax.xml.transform.Source
47  *
48  * @author Thomas.Diesler@jboss.org
49  * @since Nov 17, 2004
50  */

51 public class AttachmentImpl implements Attachment, ServiceLifecycle JavaDoc
52 {
53    private ServletEndpointContext JavaDoc context;
54
55    /** Service endpoint method for image/gif */
56    public String JavaDoc sendMimeImageGIF(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
57    {
58       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59
60       validateStringMessage(buffer, message);
61
62       String JavaDoc expContentType = "image/gif";
63       validateAttachmentPart(buffer, expContentType, mimepart);
64
65       String JavaDoc resultStr = getResultStr(buffer, expContentType);
66       return resultStr;
67    }
68
69    /** Service endpoint method for image/jpeg */
70    public String JavaDoc sendMimeImageJPEG(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
71    {
72       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
73
74       validateStringMessage(buffer, message);
75
76       String JavaDoc expContentType = "image/jpeg";
77       validateAttachmentPart(buffer, expContentType, mimepart);
78
79       String JavaDoc resultStr = getResultStr(buffer, expContentType);
80       return resultStr;
81    }
82
83    /** Service endpoint method for text/plain */
84    public String JavaDoc sendMimeTextPlain(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
85    {
86       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
87
88       validateStringMessage(buffer, message);
89
90       String JavaDoc expContentType = "text/plain";
91       validateAttachmentPart(buffer, expContentType, mimepart);
92
93       String JavaDoc resultStr = getResultStr(buffer, expContentType);
94       return resultStr;
95    }
96
97    /** Service endpoint method for multipart/* */
98    public String JavaDoc sendMimeMultipart(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
99    {
100       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101
102       validateStringMessage(buffer, message);
103
104       String JavaDoc expContentType = "multipart/*";
105       validateAttachmentPart(buffer, expContentType, mimepart);
106
107       String JavaDoc resultStr = getResultStr(buffer, expContentType);
108       return resultStr;
109    }
110
111    /** Service endpoint method for text/xml */
112    public String JavaDoc sendMimeTextXML(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
113    {
114       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
115
116       validateStringMessage(buffer, message);
117
118       String JavaDoc expContentType = "text/xml";
119       validateAttachmentPart(buffer, expContentType, mimepart);
120
121       String JavaDoc resultStr = getResultStr(buffer, expContentType);
122       return resultStr;
123    }
124
125    /** Service endpoint method for application/xml */
126    public String JavaDoc sendMimeApplicationXML(String JavaDoc message, Object JavaDoc mimepart) throws RemoteException JavaDoc
127    {
128       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
129
130       validateStringMessage(buffer, message);
131
132       String JavaDoc expContentType = "application/xml";
133       validateAttachmentPart(buffer, expContentType, mimepart);
134
135       String JavaDoc resultStr = getResultStr(buffer, expContentType);
136       return resultStr;
137    }
138
139    /** Service endpoint method for image/gif */
140    public Object JavaDoc echoMimeImageGIF(Object JavaDoc mimepart) throws RemoteException JavaDoc
141    {
142       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
143       String JavaDoc expContentType = "image/gif";
144       validateAttachmentPart(buffer, expContentType, mimepart);
145
146       return mimepart;
147    }
148
149    /** Service endpoint method for image/jpeg */
150    public Object JavaDoc echoMimeImageJPEG(Object JavaDoc mimepart) throws RemoteException JavaDoc
151    {
152       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
153       String JavaDoc expContentType = "image/jpeg";
154       validateAttachmentPart(buffer, expContentType, mimepart);
155
156       return mimepart;
157    }
158
159    /** Service endpoint method for text/plain */
160    public Object JavaDoc echoMimeTextPlain(Object JavaDoc mimepart) throws RemoteException JavaDoc
161    {
162       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
163       String JavaDoc expContentType = "text/plain";
164       validateAttachmentPart(buffer, expContentType, mimepart);
165
166       return mimepart;
167    }
168
169    /** Service endpoint method for multipart/* */
170    public Object JavaDoc echoMimeMultipart(Object JavaDoc mimepart) throws RemoteException JavaDoc
171    {
172       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
173       String JavaDoc expContentType = "multipart/*";
174       validateAttachmentPart(buffer, expContentType, mimepart);
175
176       return mimepart;
177    }
178
179    /** Service endpoint method for text/xml */
180    public Object JavaDoc echoMimeTextXML(Object JavaDoc mimepart) throws RemoteException JavaDoc
181    {
182       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183       String JavaDoc expContentType = "text/xml";
184       validateAttachmentPart(buffer, expContentType, mimepart);
185
186       return mimepart;
187    }
188
189    /** Service endpoint method for application/xml */
190    public Source JavaDoc echoMimeApplicationXML(Source JavaDoc mimepart) throws RemoteException JavaDoc
191    {
192       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
193       String JavaDoc expContentType = "application/xml";
194       validateAttachmentPart(buffer, expContentType, mimepart);
195
196       return mimepart;
197    }
198
199    public DataHandler JavaDoc echoHandler(DataHandler JavaDoc mimepart) throws RemoteException JavaDoc
200    {
201       return mimepart;
202    }
203
204    private void validateStringMessage(StringBuffer JavaDoc buffer, String JavaDoc message)
205    {
206       if ("Some text message".equals(message) == false)
207          buffer.append("[message=" + message + "]");
208    }
209
210    private void validateAttachmentPart(StringBuffer JavaDoc buffer, String JavaDoc expContentType, Object JavaDoc mimepart)
211    {
212       SOAPMessageContext JavaDoc msgContext = (SOAPMessageContext JavaDoc)context.getMessageContext();
213       SOAPMessage JavaDoc soapMessage = msgContext.getMessage();
214
215       Iterator JavaDoc attachments = soapMessage.getAttachments();
216       if (attachments.hasNext())
217       {
218          AttachmentPart JavaDoc ap = (AttachmentPart JavaDoc)attachments.next();
219          String JavaDoc contentType = ap.getContentType();
220
221          if (expContentType.equals("multipart/*"))
222          {
223             if (contentType.startsWith("multipart/") == false)
224                buffer.append("[contentType=" + contentType + "]");
225          }
226          else if (expContentType.equals("text/xml"))
227          {
228             if (contentType.equals("text/xml") == false && contentType.equals("application/xml") == false)
229                buffer.append("[contentType=" + contentType + "]");
230          }
231          else
232          {
233             if (contentType.equals(expContentType) == false)
234                buffer.append("[contentType=" + contentType + "]");
235          }
236          validateSinglePart(buffer, expContentType, ap);
237       }
238       else
239       {
240          buffer.append("[no attachments]");
241       }
242
243       validateSinglePart(buffer, expContentType, mimepart);
244    }
245
246    private void validateSinglePart(StringBuffer JavaDoc buffer, String JavaDoc contentType, Object JavaDoc content)
247    {
248       try
249       {
250          if (content instanceof AttachmentPart JavaDoc)
251             content = ((AttachmentPart JavaDoc)content).getContent();
252
253          if (contentType.equals("image/gif") || contentType.equals("image/jpeg"))
254          {
255             if ((content instanceof Image) == false)
256                buffer.append("[content=" + content + "]");
257          }
258          else if (contentType.equals("text/plain"))
259          {
260             if ((content instanceof String JavaDoc) == false)
261                buffer.append("[content=" + content + "]");
262          }
263          else if (contentType.startsWith("multipart/"))
264          {
265             if ((content instanceof MimeMultipart JavaDoc) == false)
266             {
267                buffer.append("[content=" + content + "]");
268             }
269             else
270             {
271                MimeMultipart JavaDoc mmp = (MimeMultipart JavaDoc)content;
272
273                int mmpCount = mmp.getCount();
274                if (mmpCount < 1)
275                   buffer.append("[count=" + mmpCount + "]");
276
277                for (int i = 0; i < mmpCount; i++)
278                {
279                   BodyPart JavaDoc bp = mmp.getBodyPart(i);
280                   String JavaDoc bpct = bp.getContentType();
281                   Object JavaDoc bpc = bp.getContent();
282                   validateSinglePart(buffer, bpct, bpc);
283                }
284             }
285          }
286          else if (contentType.equals("text/xml") || contentType.equals("application/xml"))
287          {
288             if ((content instanceof Source JavaDoc) == false)
289                buffer.append("[content=" + content + "]");
290          }
291          else
292          {
293             throw new IllegalArgumentException JavaDoc("Unsupported mime type: " + contentType);
294          }
295       }
296       catch (Exception JavaDoc e)
297       {
298          buffer.append("[" + e + "]");
299       }
300    }
301
302    private String JavaDoc getResultStr(StringBuffer JavaDoc buffer, String JavaDoc expContentType)
303    {
304       String JavaDoc retStr = (buffer.length() == 0 ? "[pass]" : buffer.toString());
305       System.out.println(expContentType + ": " + retStr);
306       return retStr;
307    }
308
309    // ServiceLifecycle *******************************************************************************************
310

311    public void init(Object JavaDoc context) throws ServiceException JavaDoc
312    {
313       this.context = (ServletEndpointContext JavaDoc)context;
314    }
315
316    public void destroy()
317    {
318       this.context = null;
319    }
320 }
321
Popular Tags