Simon Hunt | a834660 | 2016-01-30 14:34:42 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | a834660 | 2016-01-30 14:34:42 -0800 | [diff] [blame] | 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 | } |