comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FileUploadService.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents d78b7c06e061
children 442fbb290fa8
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.services;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 import org.apache.log4j.Logger;
8
9 import org.apache.commons.codec.binary.Base64;
10
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13
14 import de.intevation.artifacts.common.ArtifactNamespaceContext;
15 import de.intevation.artifacts.common.utils.XMLUtils;
16 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
17 import de.intevation.artifacts.common.utils.FileTools;
18
19 import de.intevation.artifacts.CallMeta;
20 import de.intevation.artifacts.GlobalContext;
21
22 import de.intevation.flys.utils.FLYSUtils;
23
24 /**
25 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
26 */
27 public class FileUploadService extends FLYSService {
28
29 /** The logger used in this service.*/
30 private static Logger logger = Logger.getLogger(FileUploadService.class);
31
32 /** XPath that points to the artifact uuid.*/
33 public static final String XPATH_ARTIFACT_UUID =
34 "/upload/artifact-uuid/text()";
35
36 /** XPath that points to the base64 encoded data.*/
37 public static final String XPATH_DATA = "/upload/data/text()";
38
39 public FileUploadService() {
40 }
41
42
43 @Override
44 protected Document doProcess(
45 Document data,
46 GlobalContext context,
47 CallMeta callMeta
48 ) {
49 logger.debug("FileUploadService.process");
50
51 String uuid = extractUuid(data);
52
53 byte[] fileData = extractData(data);
54 if (fileData != null) {
55 try {
56 String shapePath = FLYSUtils.getXPathString(
57 FLYSUtils.XPATH_SHAPEFILE_DIR);
58
59 File artifactDir = FileTools.getDirectory(shapePath, uuid);
60 FileOutputStream fos =
61 new FileOutputStream(
62 new File(artifactDir, "user-rgd.zip"));
63 try {
64 fos.write(fileData);
65 }
66 finally {
67 fos.close();
68 }
69 }
70 catch (IOException ioe) {
71 logger.warn(ioe, ioe);
72 }
73 }
74 else {
75 logger.debug("No data in uploaded xml.");
76 }
77
78 Document doc = XMLUtils.newDocument();
79 ElementCreator ec = new ElementCreator(doc, null, null);
80 Element resp = ec.create("response");
81 doc.appendChild(resp);
82
83 return doc;
84 }
85
86
87 protected String extractUuid(Document data) {
88 return XMLUtils.xpathString(
89 data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE);
90 }
91
92
93 protected byte[] extractData(Document data) {
94 String b64Data = XMLUtils.xpathString(
95 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE);
96
97 if (b64Data != null && b64Data.length() > 0) {
98 byte[] fileData = Base64.decodeBase64(b64Data);
99 return fileData;
100 }
101 return null;
102 }
103 }
104 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org