Simon Hunt

foo servlet

Change-Id: I27019564dbfffc963a63a0160c8e5b51bf31e23f
...@@ -79,6 +79,7 @@ ...@@ -79,6 +79,7 @@
79 WEB-INF/classes/onos.js=src/main/webapp/onos.js, 79 WEB-INF/classes/onos.js=src/main/webapp/onos.js,
80 WEB-INF/classes/nav.html=src/main/webapp/nav.html, 80 WEB-INF/classes/nav.html=src/main/webapp/nav.html,
81 WEB-INF/classes/app/view=src/main/webapp/app/view, 81 WEB-INF/classes/app/view=src/main/webapp/app/view,
82 + WEB-INF/classes/raw=src/main/webapp/raw,
82 {maven-resources} 83 {maven-resources}
83 </Include-Resource> 84 </Include-Resource>
84 <Bundle-SymbolicName> 85 <Bundle-SymbolicName>
...@@ -87,6 +88,7 @@ ...@@ -87,6 +88,7 @@
87 <Import-Package> 88 <Import-Package>
88 org.slf4j, 89 org.slf4j,
89 org.osgi.framework, 90 org.osgi.framework,
91 + javax.imageio.*,
90 javax.ws.rs,javax.ws.rs.core,javax.ws.rs.ext, 92 javax.ws.rs,javax.ws.rs.core,javax.ws.rs.ext,
91 javax.servlet.*, 93 javax.servlet.*,
92 com.sun.jersey.api, 94 com.sun.jersey.api,
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ui.impl;
17 +
18 +import com.google.common.io.ByteStreams;
19 +import org.onosproject.rest.AbstractInjectionResource;
20 +import org.slf4j.Logger;
21 +import org.slf4j.LoggerFactory;
22 +
23 +import javax.imageio.ImageIO;
24 +import javax.ws.rs.GET;
25 +import javax.ws.rs.Path;
26 +import javax.ws.rs.PathParam;
27 +import javax.ws.rs.Produces;
28 +import javax.ws.rs.core.Response;
29 +import java.awt.image.BufferedImage;
30 +import java.io.ByteArrayInputStream;
31 +import java.io.ByteArrayOutputStream;
32 +import java.io.IOException;
33 +import java.io.InputStream;
34 +import java.nio.ByteBuffer;
35 +
36 +/**
37 + * Resource for serving up post-processed raw data files.
38 + */
39 +@Path("/")
40 +public class FooResource extends AbstractInjectionResource {
41 +
42 + private static final String ROOT = "/raw/";
43 + private static final String PNG = "png";
44 + private static final byte UMASK = -16;
45 + private static final byte LMASK = 15;
46 +
47 + private final Logger log = LoggerFactory.getLogger(getClass());
48 +
49 + private static void clean(ByteBuffer bb, byte b1, byte b2) {
50 + bb.put((byte) ((b1 & UMASK) | (b2 & LMASK)));
51 + }
52 +
53 + private static ByteBuffer decodeBin(byte[] bytes) {
54 + int size = bytes.length;
55 + ByteBuffer bb = ByteBuffer.allocate(size / 2);
56 + for (int i = 0; i < size; i += 2) {
57 + clean(bb, bytes[i], bytes[i + 1]);
58 + }
59 + return bb;
60 + }
61 +
62 + private static void watermark(BufferedImage bi) {
63 + // to be implemented...
64 + }
65 +
66 + private static byte[] decodeAndMark(byte[] bytes) throws IOException {
67 + ByteBuffer bb = decodeBin(bytes);
68 + BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bb.array()));
69 + watermark(bi);
70 + ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 + ImageIO.write(bi, PNG, baos);
72 + return baos.toByteArray();
73 + }
74 +
75 + @Path("{resource}")
76 + @GET
77 + @Produces("image/png")
78 + public Response getBinResource(@PathParam("resource") String resource)
79 + throws IOException {
80 +
81 + String path = ROOT + resource;
82 + InputStream is = getClass().getClassLoader().getResourceAsStream(path);
83 +
84 + if (is == null) {
85 + log.warn("Didn't find resource {}", path);
86 + return Response.status(Response.Status.NOT_FOUND).build();
87 + }
88 +
89 + byte[] bytes = ByteStreams.toByteArray(is);
90 + log.info("Processing resource {} ({} bytes)", path, bytes.length);
91 + return Response.ok(decodeAndMark(bytes)).build();
92 + }
93 +}
...@@ -145,6 +145,28 @@ ...@@ -145,6 +145,28 @@
145 </servlet-mapping> 145 </servlet-mapping>
146 146
147 <servlet> 147 <servlet>
148 + <servlet-name>Foo Module</servlet-name>
149 + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
150 + </servlet-class>
151 + <init-param>
152 + <param-name>com.sun.jersey.config.property.resourceConfigClass
153 + </param-name>
154 + <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig
155 + </param-value>
156 + </init-param>
157 + <init-param>
158 + <param-name>com.sun.jersey.config.property.classnames</param-name>
159 + <param-value>org.onosproject.ui.impl.FooResource</param-value>
160 + </init-param>
161 + <load-on-startup>1</load-on-startup>
162 + </servlet>
163 +
164 + <servlet-mapping>
165 + <servlet-name>Foo Module</servlet-name>
166 + <url-pattern>/raw/*</url-pattern>
167 + </servlet-mapping>
168 +
169 + <servlet>
148 <servlet-name>JAX-RS Service</servlet-name> 170 <servlet-name>JAX-RS Service</servlet-name>
149 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer 171 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
150 </servlet-class> 172 </servlet-class>
......
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
44 if (eeggMax.indexOf(eegg) === 0) { 44 if (eeggMax.indexOf(eegg) === 0) {
45 if (eegg === eeggMax) { 45 if (eegg === eeggMax) {
46 d3.select('body').append('div').attr('id', 'eegg') 46 d3.select('body').append('div').attr('id', 'eegg')
47 - .append('img').attr('src', 'data/img/eegg.png'); 47 + .append('img').attr('src', 'raw/ewo.foo');
48 $timeout(function () { d3.select('#eegg').remove(); }, 3000); 48 $timeout(function () { d3.select('#eegg').remove(); }, 3000);
49 eegg = ''; 49 eegg = '';
50 } 50 }
......