1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.cxf;
12
13 import org.mule.api.DefaultMuleException;
14 import org.mule.api.MuleException;
15 import org.mule.api.transport.MessageAdapter;
16 import org.mule.transport.AbstractMessageAdapter;
17 import org.mule.transport.cxf.i18n.CxfMessages;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import javax.activation.DataHandler;
24
25 import org.apache.cxf.attachment.AttachmentImpl;
26 import org.apache.cxf.helpers.CastUtils;
27 import org.apache.cxf.message.AbstractWrappedMessage;
28 import org.apache.cxf.message.Attachment;
29 import org.apache.cxf.message.Message;
30
31
32
33
34 public class CxfMessageAdapter extends AbstractMessageAdapter
35 {
36
37
38
39 private static final long serialVersionUID = -1L;
40
41 private Message payload;
42
43 public CxfMessageAdapter(MessageAdapter msg) throws MuleException
44 {
45 super(msg);
46 }
47
48 public void setPayload(Message message)
49 {
50 this.payload = message;
51 }
52
53
54
55
56 public Object getPayload()
57 {
58 List<Object> objs = CastUtils.cast(payload.getContent(List.class));
59
60 if (objs == null)
61 {
62
63 Object o = payload.getContent(Object.class);
64 if (o != null)
65 {
66 return o;
67 }
68 else
69 {
70 return new Object[0];
71 }
72 }
73 if (objs.size() == 1 && objs.get(0) != null)
74 {
75 return objs.get(0);
76 }
77 else
78 {
79 return objs.toArray();
80 }
81 }
82
83 public void addAttachment(String name, DataHandler dataHandler) throws Exception
84 {
85 Collection<Attachment> attachments = getAttachments();
86 AttachmentImpl newA = new AttachmentImpl(name);
87 newA.setDataHandler(dataHandler);
88 attachments.add(newA);
89 }
90
91 public void removeAttachment(String name) throws Exception
92 {
93 Collection<Attachment> attachments = getAttachments();
94 List<Attachment> newAttachments = new ArrayList<Attachment>();
95 for (Attachment attachment : attachments)
96 {
97
98
99 if (attachment.getId() != null && attachment.getId().equals(name))
100 {
101 continue;
102 }
103 newAttachments.add(attachment);
104 }
105 payload.setAttachments(newAttachments);
106 }
107
108 protected Collection<Attachment> getAttachments() throws MuleException
109 {
110 if (payload instanceof AbstractWrappedMessage)
111 {
112 AbstractWrappedMessage soap = (AbstractWrappedMessage) payload;
113 return soap.getAttachments();
114 }
115 else
116 {
117
118
119 throw new DefaultMuleException(CxfMessages.inappropriateMessageTypeForAttachments(payload.getClass()
120 .getName()));
121 }
122 }
123 }