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 | 108 | super(msg); |
46 | 108 | } |
47 | |
|
48 | |
public void setPayload(Message message) |
49 | |
{ |
50 | 106 | this.payload = message; |
51 | 106 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public Object getPayload() |
57 | |
{ |
58 | 448 | List<Object> objs = CastUtils.cast(payload.getContent(List.class)); |
59 | |
|
60 | 448 | if (objs == null) |
61 | |
{ |
62 | |
|
63 | 18 | Object o = payload.getContent(Object.class); |
64 | 18 | if (o != null) |
65 | |
{ |
66 | 0 | return o; |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 18 | return new Object[0]; |
71 | |
} |
72 | |
} |
73 | 430 | if (objs.size() == 1 && objs.get(0) != null) |
74 | |
{ |
75 | 422 | return objs.get(0); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 8 | return objs.toArray(); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
public void addAttachment(String name, DataHandler dataHandler) throws Exception |
84 | |
{ |
85 | 0 | Collection<Attachment> attachments = getAttachments(); |
86 | 0 | AttachmentImpl newA = new AttachmentImpl(name); |
87 | 0 | newA.setDataHandler(dataHandler); |
88 | 0 | attachments.add(newA); |
89 | 0 | } |
90 | |
|
91 | |
public void removeAttachment(String name) throws Exception |
92 | |
{ |
93 | 0 | Collection<Attachment> attachments = getAttachments(); |
94 | 0 | List<Attachment> newAttachments = new ArrayList<Attachment>(); |
95 | 0 | for (Attachment attachment : attachments) |
96 | |
{ |
97 | |
|
98 | |
|
99 | 0 | if (attachment.getId() != null && attachment.getId().equals(name)) |
100 | |
{ |
101 | 0 | continue; |
102 | |
} |
103 | 0 | newAttachments.add(attachment); |
104 | |
} |
105 | 0 | payload.setAttachments(newAttachments); |
106 | 0 | } |
107 | |
|
108 | |
protected Collection<Attachment> getAttachments() throws MuleException |
109 | |
{ |
110 | 0 | if (payload instanceof AbstractWrappedMessage) |
111 | |
{ |
112 | 0 | AbstractWrappedMessage soap = (AbstractWrappedMessage) payload; |
113 | 0 | return soap.getAttachments(); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | |
|
118 | |
|
119 | 0 | throw new DefaultMuleException(CxfMessages.inappropriateMessageTypeForAttachments(payload.getClass() |
120 | |
.getName())); |
121 | |
} |
122 | |
} |
123 | |
} |