foo servlet

Change-Id: I27019564dbfffc963a63a0160c8e5b51bf31e23f
diff --git a/web/gui/pom.xml b/web/gui/pom.xml
index 3bfa4aa..5fbc9ea 100644
--- a/web/gui/pom.xml
+++ b/web/gui/pom.xml
@@ -79,6 +79,7 @@
                             WEB-INF/classes/onos.js=src/main/webapp/onos.js,
                             WEB-INF/classes/nav.html=src/main/webapp/nav.html,
                             WEB-INF/classes/app/view=src/main/webapp/app/view,
+                            WEB-INF/classes/raw=src/main/webapp/raw,
                             {maven-resources}
                         </Include-Resource>
                         <Bundle-SymbolicName>
@@ -87,6 +88,7 @@
                         <Import-Package>
                             org.slf4j,
                             org.osgi.framework,
+                            javax.imageio.*,
                             javax.ws.rs,javax.ws.rs.core,javax.ws.rs.ext,
                             javax.servlet.*,
                             com.sun.jersey.api,
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/FooResource.java b/web/gui/src/main/java/org/onosproject/ui/impl/FooResource.java
new file mode 100644
index 0000000..67b27db
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/FooResource.java
@@ -0,0 +1,93 @@
+/*
+ *  Copyright 2016 Open Networking Laboratory
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.onosproject.ui.impl;
+
+import com.google.common.io.ByteStreams;
+import org.onosproject.rest.AbstractInjectionResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.imageio.ImageIO;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+/**
+ * Resource for serving up post-processed raw data files.
+ */
+@Path("/")
+public class FooResource extends AbstractInjectionResource {
+
+    private static final String ROOT = "/raw/";
+    private static final String PNG = "png";
+    private static final byte UMASK = -16;
+    private static final byte LMASK = 15;
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private static void clean(ByteBuffer bb, byte b1, byte b2) {
+        bb.put((byte) ((b1 & UMASK) | (b2 & LMASK)));
+    }
+
+    private static ByteBuffer decodeBin(byte[] bytes) {
+        int size = bytes.length;
+        ByteBuffer bb = ByteBuffer.allocate(size / 2);
+        for (int i = 0; i < size; i += 2) {
+            clean(bb, bytes[i], bytes[i + 1]);
+        }
+        return bb;
+    }
+
+    private static void watermark(BufferedImage bi) {
+        // to be implemented...
+    }
+
+    private static byte[] decodeAndMark(byte[] bytes) throws IOException {
+        ByteBuffer bb = decodeBin(bytes);
+        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bb.array()));
+        watermark(bi);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageIO.write(bi, PNG, baos);
+        return baos.toByteArray();
+    }
+
+    @Path("{resource}")
+    @GET
+    @Produces("image/png")
+    public Response getBinResource(@PathParam("resource") String resource)
+            throws IOException {
+
+        String path = ROOT + resource;
+        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
+
+        if (is == null) {
+            log.warn("Didn't find resource {}", path);
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        byte[] bytes = ByteStreams.toByteArray(is);
+        log.info("Processing resource {} ({} bytes)", path, bytes.length);
+        return Response.ok(decodeAndMark(bytes)).build();
+    }
+}
diff --git a/web/gui/src/main/webapp/WEB-INF/web.xml b/web/gui/src/main/webapp/WEB-INF/web.xml
index dda59f5..406124b 100644
--- a/web/gui/src/main/webapp/WEB-INF/web.xml
+++ b/web/gui/src/main/webapp/WEB-INF/web.xml
@@ -145,6 +145,28 @@
     </servlet-mapping>
 
     <servlet>
+        <servlet-name>Foo Module</servlet-name>
+        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
+        </servlet-class>
+        <init-param>
+            <param-name>com.sun.jersey.config.property.resourceConfigClass
+            </param-name>
+            <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig
+            </param-value>
+        </init-param>
+        <init-param>
+            <param-name>com.sun.jersey.config.property.classnames</param-name>
+            <param-value>org.onosproject.ui.impl.FooResource</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>Foo Module</servlet-name>
+        <url-pattern>/raw/*</url-pattern>
+    </servlet-mapping>
+
+    <servlet>
         <servlet-name>JAX-RS Service</servlet-name>
         <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
         </servlet-class>
diff --git a/web/gui/src/main/webapp/app/fw/util/keys.js b/web/gui/src/main/webapp/app/fw/util/keys.js
index fe290c3..393f660 100644
--- a/web/gui/src/main/webapp/app/fw/util/keys.js
+++ b/web/gui/src/main/webapp/app/fw/util/keys.js
@@ -44,7 +44,7 @@
         if (eeggMax.indexOf(eegg) === 0) {
             if (eegg === eeggMax) {
                 d3.select('body').append('div').attr('id', 'eegg')
-                    .append('img').attr('src', 'data/img/eegg.png');
+                    .append('img').attr('src', 'raw/ewo.foo');
                 $timeout(function () { d3.select('#eegg').remove(); }, 3000);
                 eegg = '';
             }
diff --git a/web/gui/src/main/webapp/data/img/eegg.png b/web/gui/src/main/webapp/data/img/eegg.png
deleted file mode 100644
index f43ce38..0000000
--- a/web/gui/src/main/webapp/data/img/eegg.png
+++ /dev/null
Binary files differ
diff --git a/web/gui/src/main/webapp/data/raw/ewo.bin b/web/gui/src/main/webapp/raw/ewo.foo
similarity index 100%
rename from web/gui/src/main/webapp/data/raw/ewo.bin
rename to web/gui/src/main/webapp/raw/ewo.foo
Binary files differ