comparison src/main/java/org/dive4elements/artifacts/httpclient/ConsoleClient.java @ 71:a857866d162f

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:14:14 +0200
parents src/main/java/de/intevation/artifacts/httpclient/ConsoleClient.java@dbf1bfa070af
children 133281653904
comparison
equal deleted inserted replaced
70:da691e917f98 71:a857866d162f
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifacts.httpclient;
10
11 import java.io.IOException;
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.OutputStream;
15
16 import java.net.MalformedURLException;
17
18 import java.util.Arrays;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.xml.xpath.XPathConstants;
25
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Node;
28 import org.w3c.dom.NodeList;
29
30 import org.apache.log4j.Logger;
31 import org.apache.log4j.PropertyConfigurator;
32
33 import de.intevation.artifacts.httpclient.http.HttpClient;
34 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
35 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
36
37 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
38 import de.intevation.artifacts.httpclient.exceptions.NoSuchOptionException;
39 import de.intevation.artifacts.httpclient.objects.Artifact;
40 import de.intevation.artifacts.httpclient.utils.ArtifactProtocolUtils;
41 import de.intevation.artifacts.httpclient.utils.Configuration;
42 import de.intevation.artifacts.httpclient.utils.XFormNamespaceContext;
43 import de.intevation.artifacts.httpclient.utils.XMLUtils;
44
45 /**
46 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
47 */
48 public class ConsoleClient
49 {
50 /**
51 * The logging is done via Log4j. To configure the logging
52 * a file 'log4j.properties' is search in the configuration directory.
53 */
54 public static final String LOG4J_PROPERTIES = "log4j.properties";
55
56
57 /**
58 * The path of the configuration directory.
59 */
60 public static final String CONFIG_PATH = System.getProperty("config.dir",
61 "conf");
62
63 public static final String CONFIG = System.getProperty("config.file",
64 "use_case1.conf");
65
66
67 public static final String XPATH_DYNAMIC = "/art:result/art:ui/art:dynamic";
68
69 /**
70 * The logger used in this class.
71 */
72 private static Logger logger;
73
74
75 static {
76 configureLogging();
77
78 logger = Logger.getLogger(ConsoleClient.class);
79 }
80
81
82 /**
83 * Trys to load the Log4j configuration from ${config.dir}/log4j.properties.
84 */
85 public static final void configureLogging() {
86 File configDir = new File(CONFIG_PATH);
87 File propFile = new File(configDir, LOG4J_PROPERTIES);
88
89 if (propFile.isFile() && propFile.canRead()) {
90 try {
91 PropertyConfigurator.configure(propFile.toURI().toURL());
92 }
93 catch (MalformedURLException mue) {
94 mue.printStackTrace(System.err);
95 }
96 }
97 }
98
99
100 public static final Configuration readConfiguration() {
101 File configDir = new File(CONFIG_PATH);
102 File configFile = new File(configDir, CONFIG);
103
104 logger.debug("Configuration file: " + configFile.getAbsolutePath());
105
106 if (configFile.isFile() && configFile.canRead()) {
107 try {
108 Configuration conf = new Configuration(configFile);
109 conf.initialize();
110
111 return conf;
112 }
113 catch (IOException ioe) {
114 logger.error("Error while reading configuration.");
115 }
116 }
117
118 return null;
119 }
120
121
122 public static void main( String[] args )
123 {
124 logger.info("Starting console client.");
125
126 Configuration conf = readConfiguration();
127
128 String serverHost = (String) conf.getServerSettings("host");
129 String serverPort = (String) conf.getServerSettings("port");
130 HttpClient client = new HttpClientImpl(serverHost + ":" + serverPort);
131
132 try {
133 Document create = ArtifactProtocolUtils.createCreateDocument(
134 (String) conf.getArtifactSettings("fis"));
135 Artifact artifact = (Artifact) client.create(create, null);
136
137 Map attr = new HashMap();
138 String product = (String) conf.getArtifactSettings("product");
139 String[] products = extractOptions(client, artifact, product);
140 attr.put("product", products[0]);
141
142 feedAndGo(client, artifact, attr, "timeSeries");
143
144 attr.clear();
145 String area = (String) conf.getArtifactSettings("areaid");
146 String[] areas = extractOptions(client, artifact, area);
147 attr.put("areaid", areas[0]);
148 feedAndGo(client, artifact, attr, "timeseries_without_geom");
149
150 attr.clear();
151 String feature = (String) conf.getArtifactSettings("featureid");
152 String[] features = extractOptions(client, artifact, feature);
153 attr.put("featureid", features[0]);
154 feedAndGo(client, artifact, attr, "timeseries_vector_scalar");
155
156 attr.clear();
157 String vector = (String) conf.getArtifactSettings("vectorscalar");
158 String[] vectors = extractOptions(client, artifact, vector);
159 attr.put("vectorscalar", vectors[0]);
160 feedAndGo(client, artifact, attr, "timeseries_parameter");
161
162 attr.clear();
163 String parameter = (String) conf.getArtifactSettings("parameterid");
164 String[] parameters = extractOptions(client, artifact, parameter);
165 attr.put("parameterid", parameters);
166 feedAndGo(client, artifact, attr, "timeseries_depth_height");
167
168 attr.clear();
169 String measure = (String) conf.getArtifactSettings("measurementid");
170 String[] measures = extractMeasurements(client, artifact, measure);
171 attr.put("measurementid", measures);
172 feedAndGo(client, artifact, attr, "timeseries_interval");
173
174 attr.clear();
175 String min = (String) conf.getArtifactSettings("minvalue");
176 String max = (String) conf.getArtifactSettings("maxvalue");
177 attr.put("minvalue", min);
178 attr.put("maxvalue", max);
179 feedAndGo(client, artifact, attr, "timeseries_calculate_results");
180
181 try {
182 Map opts = new HashMap();
183 opts.put("mime-type", conf.getOutputSettings("mime-type"));
184 opts.put("width", conf.getOutputSettings("width"));
185 opts.put("height", conf.getOutputSettings("height"));
186 opts.put("points", conf.getOutputSettings("points"));
187
188 Document chart =
189 ArtifactProtocolUtils.createChartDocument(artifact, opts);
190
191 String dir = (String) conf.getOutputSettings("directory");
192
193 File outDir = new File(dir);
194 File output = new File(outDir, "output.png");
195 OutputStream os = new FileOutputStream(output);
196
197 client.out(artifact, chart, "chart", os);
198 }
199 catch (IOException ioe) {
200 logger.error(
201 "IO error while writing the output: " + ioe.getMessage());
202 }
203
204 logger.debug("Finished console client.");
205 }
206 catch (ConnectionException ce) {
207 logger.error(ce.getMessage());
208 }
209 catch (NoSuchOptionException nsoe) {
210 logger.error(
211 "No such option found: " + nsoe.getMessage());
212 }
213 }
214
215
216 public static void feedAndGo(
217 HttpClient client,
218 Artifact artifact,
219 Map attr,
220 String target)
221 throws ConnectionException
222 {
223 Document feed = ArtifactProtocolUtils.createFeedDocument(artifact, attr);
224 client.feed(artifact, feed, new DocumentResponseHandler());
225
226 Document advance = ArtifactProtocolUtils.createAdvanceDocument(
227 artifact,
228 target);
229
230 client.advance(artifact, advance, new DocumentResponseHandler());
231 }
232
233
234 /**
235 * XXX I think, this method should be implemented somewhere else to be able
236 * to re-use this implementation. But this method needs more work to be more
237 * abstract, so it needs to be reimplemented later, I think.
238 */
239 public static String[] extractOptions(
240 HttpClient client,
241 Artifact artifact,
242 String text)
243 throws NoSuchOptionException, ConnectionException
244 {
245 Document describe = ArtifactProtocolUtils.createDescribeDocument(
246 artifact, true);
247
248 Document description = (Document) client.describe(
249 artifact, describe, new DocumentResponseHandler());
250
251 List pieces = Arrays.asList(text.split(","));
252 List options = new ArrayList(pieces.size());
253
254 Node dynamic = XMLUtils.getNodeXPath(description, XPATH_DYNAMIC);
255
256 // TODO We should handle these cases better!!
257 NodeList items = (NodeList) XMLUtils.getXPath(
258 dynamic, "xform:select1/xform:choices/xform:item",
259 XPathConstants.NODESET, XFormNamespaceContext.INSTANCE);
260
261 if (items == null || items.getLength() == 0) {
262 items = (NodeList) XMLUtils.getXPath(
263 dynamic, "xform:select/xform:choices/xform:item",
264 XPathConstants.NODESET, XFormNamespaceContext.INSTANCE);
265 }
266
267 if (items == null || items.getLength() == 0) {
268 items = (NodeList) XMLUtils.getXPath(
269 dynamic, "xform:group/xform:select/xform:item",
270 XPathConstants.NODESET, XFormNamespaceContext.INSTANCE);
271 }
272
273
274 for (int i = 0; i < items.getLength(); i++) {
275 Node item = items.item(i);
276 Node label = (Node) XMLUtils.getXPath(
277 item, "xform:label", XPathConstants.NODE,
278 XFormNamespaceContext.INSTANCE);
279
280 Node value = (Node) XMLUtils.getXPath(
281 item, "xform:value", XPathConstants.NODE,
282 XFormNamespaceContext.INSTANCE);
283
284 if (pieces.indexOf(label.getTextContent()) >= 0)
285 options.add(value.getTextContent());
286 }
287
288 if (options.isEmpty())
289 throw new NoSuchOptionException(text);
290
291 return (String[]) options.toArray(new String[options.size()]);
292 }
293
294
295 /**
296 * XXX This method extracts the measurement ids depending on the user
297 * configuration from describe document. Currently, this is a special case
298 * that should be handled the same way as all the other options in the
299 * describe document.
300 */
301 public static String[] extractMeasurements(
302 HttpClient client,
303 Artifact artifact,
304 String text)
305 throws NoSuchOptionException, ConnectionException
306 {
307 Document describe = ArtifactProtocolUtils.createDescribeDocument(
308 artifact, true);
309
310 Document description = (Document) client.describe(
311 artifact, describe, new DocumentResponseHandler());
312
313 List pieces = Arrays.asList(text.split(","));
314 List options = new ArrayList(pieces.size());
315
316 Node dynamic = XMLUtils.getNodeXPath(description, XPATH_DYNAMIC);
317
318 NodeList params = (NodeList) XMLUtils.getXPath(
319 dynamic, "xform:group/xform:select",
320 XPathConstants.NODESET, XFormNamespaceContext.INSTANCE);
321
322 for (int i = 0; i < params.getLength(); i++) {
323 Node param = params.item(i);
324
325 NodeList items = (NodeList) XMLUtils.getXPath(
326 param, "xform:item[@disabled='false']", XPathConstants.NODESET,
327 XFormNamespaceContext.INSTANCE);
328
329 for (int j = 0; j < items.getLength(); j++) {
330 Node item = items.item(j);
331
332 Node label = (Node) XMLUtils.getXPath(
333 item, "xform:label", XPathConstants.NODE,
334 XFormNamespaceContext.INSTANCE);
335
336 if (pieces.indexOf(label.getTextContent()) < 0) {
337 continue;
338 }
339
340 Node value = (Node) XMLUtils.getXPath(
341 item, "xform:value", XPathConstants.NODE,
342 XFormNamespaceContext.INSTANCE);
343
344 options.add(value.getTextContent());
345 }
346 }
347
348 if (options.isEmpty())
349 throw new NoSuchOptionException(text);
350
351 return (String[]) options.toArray(new String[options.size()]);
352 }
353 }
354 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org