annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FileUploadService.java @ 4357:11cf29eb002c

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

http://dive4elements.wald.intevation.org