9537
|
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 |
9543
|
59 /* Nachfolgende 2 Zeilen sind nur für die BCE-Entwicklungsumgebung bestimmt */ |
|
60 // client.getContext().getParameters().add("proxyHost", "proxy.bce01.de"); |
|
61 // client.getContext().getParameters().add("proxyPort", "8080"); |
9537
|
62 return client; |
|
63 } |
|
64 }; |
|
65 |
|
66 @Override |
|
67 protected Representation innerGet() throws ResourceException { |
|
68 |
|
69 try { |
|
70 final Request request = getRequest(); |
|
71 |
|
72 final Map<String, Object> attributes = request.getAttributes(); |
|
73 final String vegArtifactUuid = (String) attributes.get(ATTRIBUTE_VEGETATION_ARTIFACT_UUID); |
|
74 final String baseUrl = (String) attributes.get(ATTRIBUTE_WMS_URL); |
|
75 |
|
76 final Reference originalRef = request.getOriginalRef(); |
|
77 final String query = originalRef.getQuery(); |
|
78 |
|
79 final Form queryAsForm = originalRef.getQueryAsForm(); |
|
80 final boolean isGetMap = "GetMap".equalsIgnoreCase(queryAsForm.getFirstValue("REQUEST", true)); |
|
81 |
|
82 final String decodedBaseUrl = URLDecoder.decode(baseUrl, "UTF-8"); |
|
83 |
|
84 final Reference proxyReference = new Reference(decodedBaseUrl); |
|
85 proxyReference.setQuery(query); |
|
86 |
|
87 final Client client = CLIENT.get(); |
|
88 |
|
89 final Request proxyRequest = new Request(Method.GET, proxyReference); |
|
90 final Response response = client.handle(proxyRequest); |
|
91 |
|
92 final Status status = response.getStatus(); |
|
93 |
|
94 if (!Status.SUCCESS_OK.equals(status)) |
|
95 throw new ResourceException(status); |
|
96 |
|
97 final Representation entity = response.getEntity(); |
|
98 |
|
99 /* simply redirect everything that is not an png image */ |
|
100 if (!isGetMap) |
|
101 return entity; |
|
102 |
|
103 final MediaType mediaType = entity.getMediaType(); |
|
104 if (!MediaType.IMAGE_ALL.getMainType().equals(mediaType.getMainType())) |
|
105 return entity; |
|
106 |
|
107 try { |
|
108 final BufferedImage image = ImageIO.read(entity.getStream()); |
|
109 |
|
110 // FIXME: tweak image... |
|
111 final WritableRaster raster = image.getRaster(); |
|
112 final int width = raster.getWidth(); |
|
113 final int height = raster.getHeight(); |
|
114 final int numBands = raster.getNumBands(); |
|
115 for (int x = 0; x < width; x++) { |
|
116 for (int y = 0; y < height; y++) { |
|
117 |
|
118 // final double sampleDouble = raster.getSampleDouble(x, y, 0); |
|
119 |
|
120 final int red = raster.getSample(x, y, 0); |
|
121 final int green = raster.getSample(x, y, 1); |
|
122 final int blue = raster.getSample(x, y, 2); |
|
123 final int alpha = raster.getSample(x, y, 3); |
|
124 |
|
125 raster.setSample(x, y, 0, green); |
|
126 raster.setSample(x, y, 1, red); |
|
127 raster.setSample(x, y, 2, blue); |
|
128 raster.setSample(x, y, 3, alpha); |
|
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 } |