Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FileUploadService.java @ 5772:c0d0b9fd1aa8
issue1276: Re-enable fixation autorecommend at discharge_curves.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 19 Apr 2013 09:22:44 +0200 |
parents | 7c8ce0a95a64 |
children | a9243df307b1 |
rev | line source |
---|---|
2639 | 1 package de.intevation.flys.artifacts.services; |
2 | |
4656
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
3 import de.intevation.artifacts.CallMeta; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
4 import de.intevation.artifacts.GlobalContext; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
5 import de.intevation.artifacts.common.ArtifactNamespaceContext; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
6 import de.intevation.artifacts.common.utils.FileTools; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
7 import de.intevation.artifacts.common.utils.XMLUtils; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
8 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
9 import de.intevation.flys.utils.FLYSUtils; |
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
10 |
2639 | 11 import java.io.File; |
12 import java.io.FileOutputStream; | |
13 import java.io.IOException; | |
14 | |
4656
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
15 import org.apache.commons.codec.binary.Base64; |
2639 | 16 import org.apache.log4j.Logger; |
17 import org.w3c.dom.Document; | |
18 import org.w3c.dom.Element; | |
19 | |
20 /** | |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
21 * Service that receives XML-packed Shapefile bundles from the client. |
2639 | 22 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> |
23 */ | |
24 public class FileUploadService extends FLYSService { | |
25 | |
26 /** The logger used in this service.*/ | |
27 private static Logger logger = Logger.getLogger(FileUploadService.class); | |
28 | |
29 /** XPath that points to the artifact uuid.*/ | |
30 public static final String XPATH_ARTIFACT_UUID = | |
31 "/upload/artifact-uuid/text()"; | |
32 | |
33 /** XPath that points to the base64 encoded data.*/ | |
34 public static final String XPATH_DATA = "/upload/data/text()"; | |
35 | |
36 public FileUploadService() { | |
37 } | |
38 | |
39 @Override | |
40 protected Document doProcess( | |
41 Document data, | |
42 GlobalContext context, | |
43 CallMeta callMeta | |
44 ) { | |
45 logger.debug("FileUploadService.process"); | |
46 | |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
47 Document doc = XMLUtils.newDocument(); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
48 ElementCreator ec = new ElementCreator(doc, null, null); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
49 Element resp = ec.create("response"); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
50 Element status = ec.create("status"); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
51 resp.appendChild(status); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
52 doc.appendChild(resp); |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
53 |
2639 | 54 String uuid = extractUuid(data); |
55 | |
56 byte[] fileData = extractData(data); | |
57 if (fileData != null) { | |
58 try { | |
59 String shapePath = FLYSUtils.getXPathString( | |
4656
442fbb290fa8
Introduce river mapfile generation (WARNING: Changes in config xml files required!).
Christian Lins <christian.lins@intevation.de>
parents:
3084
diff
changeset
|
60 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR); |
2639 | 61 |
62 File artifactDir = FileTools.getDirectory(shapePath, uuid); | |
63 FileOutputStream fos = | |
2641
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
64 new FileOutputStream( |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
65 new File(artifactDir, "user-rgd.zip")); |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
66 try { |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
67 fos.write(fileData); |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
68 |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
69 // Write operation successful |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
70 status.setTextContent("Upload erfolgreich!"); // TODO: i18n |
2641
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
71 } |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
72 finally { |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
73 fos.close(); |
2bad13107161
Make file upload cross platform.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2640
diff
changeset
|
74 } |
2639 | 75 } |
76 catch (IOException ioe) { | |
77 logger.warn(ioe, ioe); | |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
78 status.setTextContent("Upload fehlgeschlagen!"); |
2639 | 79 } |
80 } | |
81 else { | |
2640
b484318ca9c6
Make it compilable again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2639
diff
changeset
|
82 logger.debug("No data in uploaded xml."); |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
83 status.setTextContent("Upload fehlgeschlagen!"); |
2639 | 84 } |
85 | |
86 return doc; | |
87 } | |
88 | |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
89 /** |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
90 * Extracts the UUID from the XML document. |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
91 * @param data |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
92 * @return |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
93 */ |
2639 | 94 protected String extractUuid(Document data) { |
95 return XMLUtils.xpathString( | |
96 data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE); | |
97 } | |
98 | |
5009
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
99 /** |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
100 * Extracts the base64 encoded ZIP file from the XML document. |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
101 * @param data |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
102 * @return |
7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
Christian Lins <christian.lins@intevation.de>
parents:
4656
diff
changeset
|
103 */ |
2639 | 104 protected byte[] extractData(Document data) { |
105 String b64Data = XMLUtils.xpathString( | |
106 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE); | |
107 | |
3084
d78b7c06e061
Fixed string comparision for identity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3083
diff
changeset
|
108 if (b64Data != null && b64Data.length() > 0) { |
2639 | 109 byte[] fileData = Base64.decodeBase64(b64Data); |
110 return fileData; | |
111 } | |
112 return null; | |
113 } | |
114 } | |
3083
4bd3d8bbb60c
Added missing vim lines.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2646
diff
changeset
|
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |