Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/FeedServiceImpl.java @ 851:aa83a6a864b4
Added FeedService Implementation.
flys-client/trunk@2636 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 12:48:25 +0000 |
parents | |
children | 3d379e6b9a5f |
comparison
equal
deleted
inserted
replaced
850:d02c3835df28 | 851:aa83a6a864b4 |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import org.w3c.dom.Document; | |
4 | |
5 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
6 | |
7 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
8 import de.intevation.artifacts.common.utils.ClientProtocolUtils; | |
9 import de.intevation.artifacts.common.utils.XMLUtils; | |
10 | |
11 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
12 import de.intevation.artifacts.httpclient.http.HttpClient; | |
13 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
14 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; | |
15 | |
16 import de.intevation.flys.client.shared.exceptions.ServerException; | |
17 import de.intevation.flys.client.shared.model.Artifact; | |
18 import de.intevation.flys.client.shared.model.ArtifactDescription; | |
19 import de.intevation.flys.client.shared.model.Data; | |
20 import de.intevation.flys.client.shared.model.DataItem; | |
21 import de.intevation.flys.client.client.services.FeedService; | |
22 | |
23 /** | |
24 * This interface provides a method that bundles the artifact specific | |
25 * operation FEED. | |
26 */ | |
27 public class FeedServiceImpl | |
28 extends RemoteServiceServlet | |
29 implements FeedService | |
30 { | |
31 /** XPath that points to the result type of a feed or advance operation.*/ | |
32 public static final String XPATH_RESULT = "/art:result/@art:type"; | |
33 | |
34 /** XPath that points to the result type of a feed or advance operation.*/ | |
35 public static final String XPATH_RESULT_MSG = "/art:result/text()"; | |
36 | |
37 /** A constant that marks errors.*/ | |
38 public static final String OPERATION_FAILURE = "FAILURE"; | |
39 | |
40 /** The error message key that is thrown if an error occured while feeding | |
41 * new data.*/ | |
42 public static final String ERROR_FEED_DATA = "error_feed_data"; | |
43 | |
44 | |
45 /** | |
46 * This method triggers the FEED operation. | |
47 * | |
48 * @param url The url of the artifact server. | |
49 * @param artifact The artifact that needs to be fed. | |
50 * @param data An array of Data objects that contain the information that | |
51 * are used for the FEED operation. | |
52 * | |
53 * @return a new artifact parsed from the description of FEED. | |
54 */ | |
55 public Artifact feed( | |
56 String url, | |
57 String locale, | |
58 Artifact artifact, | |
59 Data[] data) | |
60 throws ServerException | |
61 { | |
62 System.out.println("StepForwardServiceImpl.feed"); | |
63 | |
64 Document feed = ClientProtocolUtils.newFeedDocument( | |
65 artifact.getUuid(), | |
66 artifact.getHash(), | |
67 createKVP(data)); | |
68 | |
69 HttpClient client = new HttpClientImpl(url, locale); | |
70 | |
71 try { | |
72 Document description = (Document) client.feed( | |
73 new de.intevation.artifacts.httpclient.objects.Artifact( | |
74 artifact.getUuid(), | |
75 artifact.getHash()), | |
76 feed, | |
77 new DocumentResponseHandler()); | |
78 | |
79 if (description == null) { | |
80 System.err.println("StepForwardService.feed() - FAILED"); | |
81 throw new ServerException(ERROR_FEED_DATA); | |
82 } | |
83 | |
84 String result = XMLUtils.xpathString( | |
85 description, | |
86 XPATH_RESULT, | |
87 ArtifactNamespaceContext.INSTANCE); | |
88 | |
89 if (result == null || !result.equals(OPERATION_FAILURE)) { | |
90 System.out.println("StepForwardService.feed() - SUCCESS"); | |
91 return (Artifact) new FLYSArtifactCreator().create(description); | |
92 } | |
93 else if (result != null && result.equals(OPERATION_FAILURE)) { | |
94 String msg = XMLUtils.xpathString( | |
95 description, | |
96 XPATH_RESULT_MSG, | |
97 ArtifactNamespaceContext.INSTANCE); | |
98 throw new ServerException(msg); | |
99 } | |
100 } | |
101 catch (ConnectionException ce) { | |
102 System.err.println(ce.getLocalizedMessage()); | |
103 } | |
104 | |
105 System.err.println("StepForwardService.feed() - FAILED"); | |
106 throw new ServerException(ERROR_FEED_DATA); | |
107 } | |
108 | |
109 | |
110 /** | |
111 * This method creates an array of key/value pairs from an array of Data | |
112 * objects. The string array is used as parameter for the feed() operation. | |
113 * | |
114 * @param data The data that should be transformed into the string array. | |
115 * | |
116 * @return a string array that contains key/value pairs. | |
117 */ | |
118 protected String[][] createKVP(Data[] data) { | |
119 String[][] kvp = new String[data.length][]; | |
120 | |
121 int i = 0; | |
122 | |
123 for (Data d: data) { | |
124 DataItem[] items = d.getItems(); | |
125 String key = d.getLabel(); | |
126 String value = items[0].getStringValue(); | |
127 | |
128 kvp[i++] = new String[] { key, value }; | |
129 } | |
130 | |
131 return kvp; | |
132 } | |
133 } | |
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |