@@ -0,0 +1,2928 @@
+package net.kencochrane.raven.sentrystub;%0A%0Aimport javax.servlet.ServletContextEvent;%0Aimport javax.servlet.ServletContextListener;%0Aimport javax.servlet.annotation.WebListener;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.net.DatagramPacket;%0Aimport java.net.DatagramSocket;%0Aimport java.net.SocketException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0A@WebListener%0Apublic class SentryUdpContextListener implements ServletContextListener %7B%0A    private static final int DEFAULT_SENTRY_UDP_PORT = 9001;%0A    private static final String SENTRY_UDP_PORT_PARAMETER = %22sentryUdpPort%22;%0A    private ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());%0A    private DatagramSocket udpSocket;%0A%0A    @Override%0A    public void contextInitialized(ServletContextEvent sce) %7B%0A        String sentryUdpPortParameter = sce.getServletContext().getInitParameter(SENTRY_UDP_PORT_PARAMETER);%0A        int port = DEFAULT_SENTRY_UDP_PORT;%0A        if (sentryUdpPortParameter != null)%0A            port = Integer.parseInt(sentryUdpPortParameter);%0A        try %7B%0A            udpSocket = new DatagramSocket(port);%0A            new UdpListenerThread().start();%0A        %7D catch (SocketException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void contextDestroyed(ServletContextEvent sce) %7B%0A        executorService.shutdownNow();%0A        udpSocket.close();%0A    %7D%0A%0A    private static class UdpRequestHandler implements Runnable %7B%0A        private final DatagramPacket datagramPacket;%0A%0A        private UdpRequestHandler(DatagramPacket datagramPacket) %7B%0A            this.datagramPacket = datagramPacket;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            try %7B%0A                ByteArrayInputStream bais = new ByteArrayInputStream(datagramPacket.getData(),%0A                        datagramPacket.getOffset(),%0A                        datagramPacket.getLength());%0A                //TODO extract the AUTH header validate it, send the rest to the JSon validator%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A            %7D%0A        %7D%0A    %7D%0A%0A    private class UdpListenerThread extends Thread %7B%0A        @Override%0A        public void run() %7B%0A            while (!udpSocket.isClosed()) %7B%0A                try %7B%0A                    // We'll assume that no-one sends a %3E 65KB datagram (max size allowed on IPV4).%0A                    byte%5B%5D buffer = new byte%5B1 %3C%3C 16%5D;%0A                    DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);%0A                    udpSocket.receive(datagramPacket);%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Add a listener starting a UDP socket