Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/FileUploadService.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FileUploadService.java@7c8ce0a95a64 |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.services; | |
2 | |
3 import org.dive4elements.artifacts.CallMeta; | |
4 import org.dive4elements.artifacts.GlobalContext; | |
5 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
6 import org.dive4elements.artifacts.common.utils.FileTools; | |
7 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
8 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
9 import org.dive4elements.river.utils.FLYSUtils; | |
10 | |
11 import java.io.File; | |
12 import java.io.FileOutputStream; | |
13 import java.io.IOException; | |
14 | |
15 import org.apache.commons.codec.binary.Base64; | |
16 import org.apache.log4j.Logger; | |
17 import org.w3c.dom.Document; | |
18 import org.w3c.dom.Element; | |
19 | |
20 /** | |
21 * Service that receives XML-packed Shapefile bundles from the client. | |
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 | |
47 Document doc = XMLUtils.newDocument(); | |
48 ElementCreator ec = new ElementCreator(doc, null, null); | |
49 Element resp = ec.create("response"); | |
50 Element status = ec.create("status"); | |
51 resp.appendChild(status); | |
52 doc.appendChild(resp); | |
53 | |
54 String uuid = extractUuid(data); | |
55 | |
56 byte[] fileData = extractData(data); | |
57 if (fileData != null) { | |
58 try { | |
59 String shapePath = FLYSUtils.getXPathString( | |
60 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR); | |
61 | |
62 File artifactDir = FileTools.getDirectory(shapePath, uuid); | |
63 FileOutputStream fos = | |
64 new FileOutputStream( | |
65 new File(artifactDir, "user-rgd.zip")); | |
66 try { | |
67 fos.write(fileData); | |
68 | |
69 // Write operation successful | |
70 status.setTextContent("Upload erfolgreich!"); // TODO: i18n | |
71 } | |
72 finally { | |
73 fos.close(); | |
74 } | |
75 } | |
76 catch (IOException ioe) { | |
77 logger.warn(ioe, ioe); | |
78 status.setTextContent("Upload fehlgeschlagen!"); | |
79 } | |
80 } | |
81 else { | |
82 logger.debug("No data in uploaded xml."); | |
83 status.setTextContent("Upload fehlgeschlagen!"); | |
84 } | |
85 | |
86 return doc; | |
87 } | |
88 | |
89 /** | |
90 * Extracts the UUID from the XML document. | |
91 * @param data | |
92 * @return | |
93 */ | |
94 protected String extractUuid(Document data) { | |
95 return XMLUtils.xpathString( | |
96 data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE); | |
97 } | |
98 | |
99 /** | |
100 * Extracts the base64 encoded ZIP file from the XML document. | |
101 * @param data | |
102 * @return | |
103 */ | |
104 protected byte[] extractData(Document data) { | |
105 String b64Data = XMLUtils.xpathString( | |
106 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE); | |
107 | |
108 if (b64Data != null && b64Data.length() > 0) { | |
109 byte[] fileData = Base64.decodeBase64(b64Data); | |
110 return fileData; | |
111 } | |
112 return null; | |
113 } | |
114 } | |
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |