1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.udp; |
12 | |
|
13 | |
import org.mule.api.ThreadSafeAccess; |
14 | |
import org.mule.api.transport.MessageTypeNotSupportedException; |
15 | |
import org.mule.transport.AbstractMessageAdapter; |
16 | |
|
17 | |
import java.net.DatagramPacket; |
18 | |
import java.net.InetAddress; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class UdpMessageAdapter extends AbstractMessageAdapter |
25 | |
{ |
26 | |
|
27 | |
|
28 | |
|
29 | |
private static final long serialVersionUID = -7767141617682012504L; |
30 | |
|
31 | |
public static final String ADDRESS_PROPERTY = "packet.address"; |
32 | |
public static final String PORT_PROPERTY = "packet.port"; |
33 | |
|
34 | |
private byte[] message; |
35 | |
|
36 | |
public UdpMessageAdapter(Object message) throws MessageTypeNotSupportedException |
37 | 3008 | { |
38 | 3008 | if (message instanceof DatagramPacket) |
39 | |
{ |
40 | 3006 | DatagramPacket dp = (DatagramPacket)message; |
41 | 3006 | this.message = new byte[dp.getLength()]; |
42 | 3006 | System.arraycopy(dp.getData(), 0, this.message, 0, dp.getLength()); |
43 | |
|
44 | 3006 | InetAddress address = dp.getAddress(); |
45 | 3006 | if (address != null) |
46 | |
{ |
47 | 3000 | setProperty(ADDRESS_PROPERTY, address); |
48 | |
} |
49 | |
|
50 | 3006 | setProperty(PORT_PROPERTY, new Integer(dp.getPort())); |
51 | 3006 | } |
52 | |
else |
53 | |
{ |
54 | 2 | throw new MessageTypeNotSupportedException(message, getClass()); |
55 | |
} |
56 | 3006 | } |
57 | |
|
58 | |
protected UdpMessageAdapter(UdpMessageAdapter template) |
59 | |
{ |
60 | 2999 | super(template); |
61 | 2999 | message = template.message; |
62 | 2999 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public String getPayloadAsString(String encoding) throws Exception |
73 | |
{ |
74 | 0 | return new String(message, encoding); |
75 | |
|
76 | |
} |
77 | |
|
78 | |
public byte[] getPayloadAsBytes() throws Exception |
79 | |
{ |
80 | 0 | return message; |
81 | |
} |
82 | |
|
83 | |
public Object getPayload() |
84 | |
{ |
85 | 24000 | return message; |
86 | |
} |
87 | |
|
88 | |
public ThreadSafeAccess newThreadCopy() |
89 | |
{ |
90 | 2999 | return new UdpMessageAdapter(this); |
91 | |
} |
92 | |
|
93 | |
} |