1 10 11 package org.mule.providers.udp; 12 13 import java.net.DatagramPacket ; 14 import java.net.InetAddress ; 15 16 import org.mule.providers.AbstractMessageAdapter; 17 import org.mule.umo.MessagingException; 18 import org.mule.umo.provider.MessageTypeNotSupportedException; 19 20 23 24 public class UdpMessageAdapter extends AbstractMessageAdapter 25 { 26 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 MessagingException 37 { 38 if (message instanceof DatagramPacket ) 39 { 40 DatagramPacket dp = (DatagramPacket )message; 41 this.message = new byte[dp.getLength()]; 42 System.arraycopy(dp.getData(), 0, this.message, 0, dp.getLength()); 43 44 InetAddress address = dp.getAddress(); 45 if (address != null) 46 { 47 setProperty(ADDRESS_PROPERTY, address); 48 } 49 50 setProperty(PORT_PROPERTY, new Integer (dp.getPort())); 51 } 52 else 53 { 54 throw new MessageTypeNotSupportedException(message, getClass()); 55 } 56 } 57 58 66 public String getPayloadAsString(String encoding) throws Exception 67 { 68 return new String (message, encoding); 69 70 } 71 72 public byte[] getPayloadAsBytes() throws Exception 73 { 74 return message; 75 } 76 77 public Object getPayload() 78 { 79 return message; 80 } 81 } 82 | Popular Tags |