Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FileUploadService.java @ 5009:7c8ce0a95a64
flys/#657: Shapefile upload service returns success or failure to client (i18n tbd)
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Mon, 18 Feb 2013 16:33:13 +0100 |
parents | 442fbb290fa8 |
children | a9243df307b1 |
comparison
equal
deleted
inserted
replaced
5008:04ccec566689 | 5009:7c8ce0a95a64 |
---|---|
16 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
17 import org.w3c.dom.Document; | 17 import org.w3c.dom.Document; |
18 import org.w3c.dom.Element; | 18 import org.w3c.dom.Element; |
19 | 19 |
20 /** | 20 /** |
21 * Service that receives XML-packed Shapefile bundles from the client. | |
21 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | 22 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> |
22 */ | 23 */ |
23 public class FileUploadService extends FLYSService { | 24 public class FileUploadService extends FLYSService { |
24 | 25 |
25 /** The logger used in this service.*/ | 26 /** The logger used in this service.*/ |
33 public static final String XPATH_DATA = "/upload/data/text()"; | 34 public static final String XPATH_DATA = "/upload/data/text()"; |
34 | 35 |
35 public FileUploadService() { | 36 public FileUploadService() { |
36 } | 37 } |
37 | 38 |
38 | |
39 @Override | 39 @Override |
40 protected Document doProcess( | 40 protected Document doProcess( |
41 Document data, | 41 Document data, |
42 GlobalContext context, | 42 GlobalContext context, |
43 CallMeta callMeta | 43 CallMeta callMeta |
44 ) { | 44 ) { |
45 logger.debug("FileUploadService.process"); | 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); | |
46 | 53 |
47 String uuid = extractUuid(data); | 54 String uuid = extractUuid(data); |
48 | 55 |
49 byte[] fileData = extractData(data); | 56 byte[] fileData = extractData(data); |
50 if (fileData != null) { | 57 if (fileData != null) { |
56 FileOutputStream fos = | 63 FileOutputStream fos = |
57 new FileOutputStream( | 64 new FileOutputStream( |
58 new File(artifactDir, "user-rgd.zip")); | 65 new File(artifactDir, "user-rgd.zip")); |
59 try { | 66 try { |
60 fos.write(fileData); | 67 fos.write(fileData); |
68 | |
69 // Write operation successful | |
70 status.setTextContent("Upload erfolgreich!"); // TODO: i18n | |
61 } | 71 } |
62 finally { | 72 finally { |
63 fos.close(); | 73 fos.close(); |
64 } | 74 } |
65 } | 75 } |
66 catch (IOException ioe) { | 76 catch (IOException ioe) { |
67 logger.warn(ioe, ioe); | 77 logger.warn(ioe, ioe); |
78 status.setTextContent("Upload fehlgeschlagen!"); | |
68 } | 79 } |
69 } | 80 } |
70 else { | 81 else { |
71 logger.debug("No data in uploaded xml."); | 82 logger.debug("No data in uploaded xml."); |
83 status.setTextContent("Upload fehlgeschlagen!"); | |
72 } | 84 } |
73 | |
74 Document doc = XMLUtils.newDocument(); | |
75 ElementCreator ec = new ElementCreator(doc, null, null); | |
76 Element resp = ec.create("response"); | |
77 doc.appendChild(resp); | |
78 | 85 |
79 return doc; | 86 return doc; |
80 } | 87 } |
81 | 88 |
82 | 89 /** |
90 * Extracts the UUID from the XML document. | |
91 * @param data | |
92 * @return | |
93 */ | |
83 protected String extractUuid(Document data) { | 94 protected String extractUuid(Document data) { |
84 return XMLUtils.xpathString( | 95 return XMLUtils.xpathString( |
85 data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE); | 96 data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE); |
86 } | 97 } |
87 | 98 |
88 | 99 /** |
100 * Extracts the base64 encoded ZIP file from the XML document. | |
101 * @param data | |
102 * @return | |
103 */ | |
89 protected byte[] extractData(Document data) { | 104 protected byte[] extractData(Document data) { |
90 String b64Data = XMLUtils.xpathString( | 105 String b64Data = XMLUtils.xpathString( |
91 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE); | 106 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE); |
92 | 107 |
93 if (b64Data != null && b64Data.length() > 0) { | 108 if (b64Data != null && b64Data.length() > 0) { |