annotate flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 4161:ff74ff82f2d4 flys-2.9.3

Add theme for gauge discharge curve artifact
author Björn Ricks <bjoern.ricks@intevation.de>
date Tue, 16 Oct 2012 16:50:18 +0200
parents b9433322fcaf
children 02cf2b1dff84
rev   line source
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
2
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
3 import de.intevation.artifacts.common.utils.XMLUtils;
2954
b9433322fcaf Add alpha transp. and fix exceptions on unknown style attributes.
Christian Lins <christian.lins@intevation.de>
parents: 2497
diff changeset
4 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
5 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
6 import de.intevation.artifacts.httpclient.http.HttpClient;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
7 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
8
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
9 import java.io.ByteArrayOutputStream;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
10 import java.io.InputStream;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
11
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
12 import javax.servlet.http.HttpServlet;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13 import javax.servlet.http.HttpServletRequest;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import javax.servlet.http.HttpServletResponse;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
16 import org.apache.commons.codec.binary.Base64;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
17 import org.apache.commons.fileupload.FileItemIterator;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
18 import org.apache.commons.fileupload.FileItemStream;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
19 import org.apache.commons.fileupload.servlet.ServletFileUpload;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
20 import org.apache.log4j.Logger;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
21 import org.w3c.dom.Document;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
22 import org.w3c.dom.Element;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 public class FileUploadServiceImpl
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25 extends HttpServlet
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
26 {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
28
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
29 @Override
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 processPost(req, resp);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 protected void processPost(HttpServletRequest req, HttpServletResponse resp) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 logger.debug("handling post request.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 String url = getServletContext().getInitParameter("server-url");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 Document request = createFileXML(req);;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
41
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 if (request == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 return;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 HttpClient client = new HttpClientImpl(url);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
46
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 try {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48 Document result = client.callService(url, "fileupload", request);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 if (result == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 logger.warn("FileUpload service returned no result.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
53
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
54 return;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 catch (ConnectionException ce) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 logger.error(ce, ce);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
59 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
61
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
62 protected Document createFileXML(HttpServletRequest req) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 ServletFileUpload upload = new ServletFileUpload();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 try{
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 FileItemIterator iter = upload.getItemIterator(req);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68 while (iter.hasNext()) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 FileItemStream item = iter.next();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71 String name = item.getFieldName();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 InputStream stream = item.openStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74 // Process the input stream
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 ByteArrayOutputStream out = new ByteArrayOutputStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 int len;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 byte[] buffer = new byte[stream.available()];
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 out.write(buffer, 0, len);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81
2954
b9433322fcaf Add alpha transp. and fix exceptions on unknown style attributes.
Christian Lins <christian.lins@intevation.de>
parents: 2497
diff changeset
82 buffer = Base64.encodeBase64(buffer);
b9433322fcaf Add alpha transp. and fix exceptions on unknown style attributes.
Christian Lins <christian.lins@intevation.de>
parents: 2497
diff changeset
83 String b64File = new String(buffer);
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 Document fileDoc = XMLUtils.newDocument();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 ElementCreator ec = new ElementCreator(fileDoc, null, null);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 Element root = ec.create("upload");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 Element id = ec.create("artifact-uuid");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 id.setTextContent(req.getParameter("uuid"));
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
92 Element data = ec.create("data");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93 data.setTextContent(b64File);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
94
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
95 fileDoc.appendChild(root);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
96 root.appendChild(id);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97 root.appendChild(data);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99 return fileDoc;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102 catch(Exception e){
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
103 logger.debug("Failed to create xml document containing the file.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104 logger.debug(e, e);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
106 return null;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
108 }

http://dive4elements.wald.intevation.org