blob: 389ec74daa01b0ec602b476f9a4a9d0315643f4b [file] [log] [blame]
Simon Hunta8346602016-01-30 14:34:42 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunta8346602016-01-30 14:34:42 -08003 *
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 */
16package org.onosproject.ui.impl;
17
18import com.google.common.io.ByteStreams;
19import org.onosproject.rest.AbstractInjectionResource;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import javax.imageio.ImageIO;
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.PathParam;
27import javax.ws.rs.Produces;
28import javax.ws.rs.core.Response;
29import java.awt.image.BufferedImage;
30import java.io.ByteArrayInputStream;
31import java.io.ByteArrayOutputStream;
32import java.io.IOException;
33import java.io.InputStream;
34import java.nio.ByteBuffer;
35
36/**
37 * Resource for serving up post-processed raw data files.
38 */
39@Path("/")
40public 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}