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