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