Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/VegetationWmsResource.java @ 9537:bf6b63208f34
Work on uinfo inundation duration calculation.
Using proxy-wms to induce additional style information (work in progress).
author | gernotbelger |
---|---|
date | Wed, 17 Oct 2018 11:23:17 +0200 |
parents | |
children | 3264c2df4f18 |
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde * Software engineering by * Björnsen Beratende Ingenieure GmbH * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.artifacts.uinfo.inundationduration; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import org.dive4elements.artifactdatabase.rest.BaseResource; import org.restlet.Client; import org.restlet.Context; import org.restlet.Request; import org.restlet.Response; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Method; import org.restlet.data.Protocol; import org.restlet.data.Reference; import org.restlet.data.Status; import org.restlet.representation.OutputRepresentation; import org.restlet.representation.Representation; import org.restlet.resource.ResourceException; /** * Proof of context wms proxy. * * @author Gernot Belger */ public class VegetationWmsResource extends BaseResource { public static final String BASE_PATH = "vegetationWms"; private static final String ATTRIBUTE_VEGETATION_ARTIFACT_UUID = "vegetation-artifact-uuid"; private static final String ATTRIBUTE_WMS_URL = "wmsUrl"; private static final ThreadLocal<Client> CLIENT = new ThreadLocal<Client>() { @Override protected Client initialValue() { final List<Protocol> protocols = Arrays.asList(Protocol.HTTPS, Protocol.HTTP); final Client client = new Client(new Context(), protocols); // FIXME client.getContext().getParameters().add("proxyHost", "proxy.bce01.de"); client.getContext().getParameters().add("proxyPort", "8080"); return client; } }; @Override protected Representation innerGet() throws ResourceException { try { final Request request = getRequest(); final Map<String, Object> attributes = request.getAttributes(); final String vegArtifactUuid = (String) attributes.get(ATTRIBUTE_VEGETATION_ARTIFACT_UUID); final String baseUrl = (String) attributes.get(ATTRIBUTE_WMS_URL); final Reference originalRef = request.getOriginalRef(); final String query = originalRef.getQuery(); final Form queryAsForm = originalRef.getQueryAsForm(); final boolean isGetMap = "GetMap".equalsIgnoreCase(queryAsForm.getFirstValue("REQUEST", true)); final String decodedBaseUrl = URLDecoder.decode(baseUrl, "UTF-8"); final Reference proxyReference = new Reference(decodedBaseUrl); proxyReference.setQuery(query); final Client client = CLIENT.get(); final Request proxyRequest = new Request(Method.GET, proxyReference); final Response response = client.handle(proxyRequest); final Status status = response.getStatus(); if (!Status.SUCCESS_OK.equals(status)) throw new ResourceException(status); final Representation entity = response.getEntity(); /* simply redirect everything that is not an png image */ if (!isGetMap) return entity; final MediaType mediaType = entity.getMediaType(); if (!MediaType.IMAGE_ALL.getMainType().equals(mediaType.getMainType())) return entity; try { final BufferedImage image = ImageIO.read(entity.getStream()); // FIXME: tweak image... final WritableRaster raster = image.getRaster(); final int width = raster.getWidth(); final int height = raster.getHeight(); final int numBands = raster.getNumBands(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // final double sampleDouble = raster.getSampleDouble(x, y, 0); final int red = raster.getSample(x, y, 0); final int green = raster.getSample(x, y, 1); final int blue = raster.getSample(x, y, 2); final int alpha = raster.getSample(x, y, 3); raster.setSample(x, y, 0, green); raster.setSample(x, y, 1, red); raster.setSample(x, y, 2, blue); raster.setSample(x, y, 3, alpha); } } final Representation newResponse = new OutputRepresentation(mediaType) { @Override public void write(final OutputStream outputStream) throws IOException { // REMARK: in most cases the media-subtype can be used as image-format name. final String subType = mediaType.getSubType(); ImageIO.write(image, subType, outputStream); } }; return newResponse; } catch (final IOException e) { e.printStackTrace(); throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e); } } catch (final UnsupportedEncodingException e) { e.printStackTrace(); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e); } } }