comparison gwt-client/src/main/java/org/dive4elements/river/client/server/GFIServiceImpl.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/server/GFIServiceImpl.java@821a02bbfb4e
children a3cd78333185
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.server;
2
3 import java.io.InputStream;
4 import java.io.UnsupportedEncodingException;
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Scanner;
12
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16
17 import org.apache.log4j.Logger;
18
19 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
20
21 import org.dive4elements.artifacts.common.utils.XMLUtils;
22
23 import org.dive4elements.river.client.shared.exceptions.ServerException;
24 import org.dive4elements.river.client.shared.model.AttributedTheme;
25 import org.dive4elements.river.client.shared.model.FeatureInfo;
26 import org.dive4elements.river.client.shared.model.FeatureInfoResponse;
27 import org.dive4elements.river.client.shared.model.Theme;
28
29 import org.dive4elements.river.client.client.services.GFIService;
30
31
32 /**
33 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
34 */
35 public class GFIServiceImpl
36 extends RemoteServiceServlet
37 implements GFIService
38 {
39 public static final String ERR_NO_VALID_GFI_URL =
40 "error_no_valid_gfi_url";
41
42 public static final String ERR_GFI_REQUEST_FAILED =
43 "error_gfi_req_failed";
44
45 public static final String ERR_PARSING_RESPONSE_FAILED =
46 "error_gfi_parsing_failed";
47
48
49 private static final Logger logger =
50 Logger.getLogger(GFIServiceImpl.class);
51
52
53 /**
54 * @param theme
55 * @param format
56 * @param bbox
57 * @param height
58 * @param width
59 * @param x
60 * @param y
61 *
62 * @return
63 */
64 public FeatureInfoResponse query(
65 Theme theme,
66 String format,
67 String bbox,
68 String projection,
69 int height,
70 int width,
71 int x,
72 int y
73 ) throws ServerException
74 {
75 logger.info("GFIServiceImpl.query");
76
77 String path = createGetFeautureInfoURL(
78 theme, format, bbox, projection, height, width, x, y);
79
80 logger.debug("URL=" + path);
81
82 try {
83 URL url = new URL(path);
84
85 URLConnection conn = url.openConnection();
86 conn.connect();
87
88 InputStream is = conn.getInputStream();
89
90 return parseResponse(is);
91
92 }
93 catch (IOException ioe) {
94 logger.warn(ioe, ioe);
95 }
96
97 throw new ServerException(ERR_GFI_REQUEST_FAILED);
98 }
99
100
101 /**
102 * @param map
103 * @param theme
104 * @param format
105 * @param x
106 * @param y
107 *
108 * @return
109 */
110 protected String createGetFeautureInfoURL(
111 Theme theme,
112 String infoFormat,
113 String bbox,
114 String projection,
115 int height,
116 int width,
117 int x,
118 int y
119 ) throws ServerException
120 {
121 String url = getUrl(theme);
122
123 if (url == null || url.length() == 0) {
124 throw new ServerException(ERR_NO_VALID_GFI_URL);
125 }
126
127 String layers = ((AttributedTheme)theme).getAttr("layers");
128
129 StringBuilder sb = new StringBuilder();
130 sb.append(url);
131
132 if (url.indexOf("?") < 0) {
133 sb.append("?SERVICE=WMS");
134 }
135 else {
136 sb.append("&SERVICE=WMS");
137 }
138
139 sb.append("&VERSION=1.1.1");
140 sb.append("&REQUEST=GetFeatureInfo");
141 sb.append("&LAYERS=" + layers);
142 sb.append("&QUERY_LAYERS=" + layers);
143 sb.append("&BBOX=" + bbox);
144 sb.append("&HEIGHT=" + height);
145 sb.append("&WIDTH=" + width);
146 sb.append("&FORMAT=image/png");
147 sb.append("&INFO_FORMAT=" + infoFormat);
148 sb.append("&SRS=" + projection);
149 sb.append("&X=" + String.valueOf(x));
150 sb.append("&Y=" + String.valueOf(y));
151
152 return sb.toString();
153 }
154
155
156 protected String getUrl(Theme theme) {
157 AttributedTheme attr = (AttributedTheme) theme;
158 return attr.getAttr("url");
159 }
160
161
162 protected FeatureInfoResponse parseResponse(InputStream is) {
163 logger.debug("GFIServiceImpl.parseResponse");
164
165 ByteArrayOutputStream baos = new ByteArrayOutputStream();
166 byte [] buf = new byte[1024];
167
168 int r = -1;
169 try {
170 while ((r = is.read(buf)) >= 0) {
171 baos.write(buf, 0, r);
172 }
173 } catch (IOException ex) {
174 logger.warn("GetFeatureInfo response could not be read: ", ex);
175 return new FeatureInfoResponse();
176 }
177
178 String content;
179 try {
180 content = baos.toString("UTF-8");
181 }
182 catch (UnsupportedEncodingException uee) {
183 content = baos.toString();
184 }
185
186 Document response = XMLUtils.parseDocument(content);
187 if (response != null) {
188 List<FeatureInfo> features = new ArrayList<FeatureInfo>();
189 parseFeatureInfos(response, features);
190 return new FeatureInfoResponse(features, null);
191 }
192 // Unable to parse just return the response as is
193 return new FeatureInfoResponse(new ArrayList<FeatureInfo>(), content);
194 }
195
196
197 protected void parseFeatureInfos(Node node, List<FeatureInfo> features) {
198 logger.debug("GFIServiceImpl.parseFeatureInfos");
199
200 String name = node.getNodeName();
201
202 if (name.endsWith("_layer")) {
203 features.add(parseFeature(node));
204
205 return;
206 }
207
208 NodeList children = node.getChildNodes();
209
210 if (children != null && children.getLength() > 0) {
211 for (int i = 0, n = children.getLength(); i < n; i++) {
212 parseFeatureInfos(children.item(i), features);
213 }
214 }
215 }
216
217
218 protected FeatureInfo parseFeature(Node node) {
219 logger.debug("GFIServiceImpl.parseFeature");
220
221 String layername = node.getNodeName();
222
223 FeatureInfo f = new FeatureInfo(layername);
224
225 NodeList children = node.getChildNodes();
226 int numChildren = children != null ? children.getLength() : 0;
227
228 logger.debug("Feature '" + layername + "' has " + numChildren + " nodes.");
229
230 for (int i = 0; i < numChildren; i++) {
231 Node tmp = children.item(i);
232 String nodeName = tmp.getNodeName();
233
234 logger.debug(" node name: '" + nodeName + "'");
235
236 if (nodeName.equals("gml:name")) {
237 logger.debug("NAME node has child: " + tmp.getFirstChild().getNodeValue());
238 f.setLayername(tmp.getFirstChild().getNodeValue());
239 }
240 else if (nodeName.endsWith("_feature")) {
241 parseFeatureAttributes(tmp, f);
242 }
243 }
244
245 return f;
246 }
247
248
249 protected void parseFeatureAttributes(Node node, FeatureInfo f) {
250 logger.debug("GFIServiceImpl.parseFeatureAttributes");
251
252 NodeList children = node.getChildNodes();
253 int numChildren = children != null ? children.getLength() : 0;
254
255 logger.debug("Has " + numChildren + " attributes.");
256
257 for (int i = 0; i < numChildren; i++) {
258 Node tmp = children.item(i);
259 String name = tmp.getNodeName();
260
261 logger.debug(" tmp attribute name: '" + name + "'");
262
263 if (name.equals("gml:boundedBy")) {
264 // TODO
265 }
266 else {
267 Node child = tmp.getFirstChild();
268 if (child != null) {
269 f.addAttr(name, child.getNodeValue());
270 }
271 }
272 }
273 }
274 }
275 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org