comparison 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
comparison
equal deleted inserted replaced
9536:aa23225fd85f 9537:bf6b63208f34
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
2 * Software engineering by
3 * Björnsen Beratende Ingenieure GmbH
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
5 *
6 * This file is Free Software under the GNU AGPL (>=v3)
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
8 * documentation coming with Dive4Elements River for details.
9 */
10 package org.dive4elements.river.artifacts.uinfo.inundationduration;
11
12 import java.awt.image.BufferedImage;
13 import java.awt.image.WritableRaster;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URLDecoder;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.imageio.ImageIO;
23
24 import org.dive4elements.artifactdatabase.rest.BaseResource;
25 import org.restlet.Client;
26 import org.restlet.Context;
27 import org.restlet.Request;
28 import org.restlet.Response;
29 import org.restlet.data.Form;
30 import org.restlet.data.MediaType;
31 import org.restlet.data.Method;
32 import org.restlet.data.Protocol;
33 import org.restlet.data.Reference;
34 import org.restlet.data.Status;
35 import org.restlet.representation.OutputRepresentation;
36 import org.restlet.representation.Representation;
37 import org.restlet.resource.ResourceException;
38
39 /**
40 * Proof of context wms proxy.
41 *
42 * @author Gernot Belger
43 */
44 public class VegetationWmsResource extends BaseResource {
45
46 public static final String BASE_PATH = "vegetationWms";
47
48 private static final String ATTRIBUTE_VEGETATION_ARTIFACT_UUID = "vegetation-artifact-uuid";
49
50 private static final String ATTRIBUTE_WMS_URL = "wmsUrl";
51
52 private static final ThreadLocal<Client> CLIENT = new ThreadLocal<Client>() {
53 @Override
54 protected Client initialValue() {
55
56 final List<Protocol> protocols = Arrays.asList(Protocol.HTTPS, Protocol.HTTP);
57 final Client client = new Client(new Context(), protocols);
58 // FIXME
59 client.getContext().getParameters().add("proxyHost", "proxy.bce01.de");
60 client.getContext().getParameters().add("proxyPort", "8080");
61 return client;
62 }
63 };
64
65 @Override
66 protected Representation innerGet() throws ResourceException {
67
68 try {
69 final Request request = getRequest();
70
71 final Map<String, Object> attributes = request.getAttributes();
72 final String vegArtifactUuid = (String) attributes.get(ATTRIBUTE_VEGETATION_ARTIFACT_UUID);
73 final String baseUrl = (String) attributes.get(ATTRIBUTE_WMS_URL);
74
75 final Reference originalRef = request.getOriginalRef();
76 final String query = originalRef.getQuery();
77
78 final Form queryAsForm = originalRef.getQueryAsForm();
79 final boolean isGetMap = "GetMap".equalsIgnoreCase(queryAsForm.getFirstValue("REQUEST", true));
80
81 final String decodedBaseUrl = URLDecoder.decode(baseUrl, "UTF-8");
82
83 final Reference proxyReference = new Reference(decodedBaseUrl);
84 proxyReference.setQuery(query);
85
86 final Client client = CLIENT.get();
87
88 final Request proxyRequest = new Request(Method.GET, proxyReference);
89 final Response response = client.handle(proxyRequest);
90
91 final Status status = response.getStatus();
92
93 if (!Status.SUCCESS_OK.equals(status))
94 throw new ResourceException(status);
95
96 final Representation entity = response.getEntity();
97
98 /* simply redirect everything that is not an png image */
99 if (!isGetMap)
100 return entity;
101
102 final MediaType mediaType = entity.getMediaType();
103 if (!MediaType.IMAGE_ALL.getMainType().equals(mediaType.getMainType()))
104 return entity;
105
106 try {
107 final BufferedImage image = ImageIO.read(entity.getStream());
108
109 // FIXME: tweak image...
110 final WritableRaster raster = image.getRaster();
111 final int width = raster.getWidth();
112 final int height = raster.getHeight();
113 final int numBands = raster.getNumBands();
114 for (int x = 0; x < width; x++) {
115 for (int y = 0; y < height; y++) {
116
117 // final double sampleDouble = raster.getSampleDouble(x, y, 0);
118
119 final int red = raster.getSample(x, y, 0);
120 final int green = raster.getSample(x, y, 1);
121 final int blue = raster.getSample(x, y, 2);
122 final int alpha = raster.getSample(x, y, 3);
123
124 raster.setSample(x, y, 0, green);
125 raster.setSample(x, y, 1, red);
126 raster.setSample(x, y, 2, blue);
127 raster.setSample(x, y, 3, alpha);
128 }
129 }
130
131
132 final Representation newResponse = new OutputRepresentation(mediaType) {
133
134 @Override
135 public void write(final OutputStream outputStream) throws IOException {
136
137 // REMARK: in most cases the media-subtype can be used as image-format name.
138 final String subType = mediaType.getSubType();
139 ImageIO.write(image, subType, outputStream);
140
141 }
142 };
143
144 return newResponse;
145 }
146 catch (final IOException e) {
147 e.printStackTrace();
148 throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
149 }
150
151 }
152 catch (final UnsupportedEncodingException e) {
153 e.printStackTrace();
154 throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e);
155 }
156 }
157 }

http://dive4elements.wald.intevation.org