Mercurial > dive4elements > river
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 | 1 package de.intevation.flys.client.server; |
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 | 11 |
12 import javax.servlet.http.HttpServlet; | |
13 import javax.servlet.http.HttpServletRequest; | |
14 import javax.servlet.http.HttpServletResponse; | |
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 | 23 |
24 public class FileUploadServiceImpl | |
25 extends HttpServlet | |
26 { | |
27 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class); | |
28 | |
29 @Override | |
30 protected void doPost(HttpServletRequest req, HttpServletResponse resp) { | |
31 processPost(req, resp); | |
32 } | |
33 | |
34 | |
35 protected void processPost(HttpServletRequest req, HttpServletResponse resp) { | |
36 logger.debug("handling post request."); | |
37 | |
38 String url = getServletContext().getInitParameter("server-url"); | |
39 | |
40 Document request = createFileXML(req);; | |
41 | |
42 if (request == null) { | |
43 return; | |
44 } | |
45 HttpClient client = new HttpClientImpl(url); | |
46 | |
47 try { | |
48 Document result = client.callService(url, "fileupload", request); | |
49 | |
50 if (result == null) { | |
51 logger.warn("FileUpload service returned no result."); | |
52 } | |
53 | |
54 return; | |
55 } | |
56 catch (ConnectionException ce) { | |
57 logger.error(ce, ce); | |
58 } | |
59 } | |
60 | |
61 | |
62 protected Document createFileXML(HttpServletRequest req) { | |
63 ServletFileUpload upload = new ServletFileUpload(); | |
64 | |
65 try{ | |
66 FileItemIterator iter = upload.getItemIterator(req); | |
67 | |
68 while (iter.hasNext()) { | |
69 FileItemStream item = iter.next(); | |
70 | |
71 String name = item.getFieldName(); | |
72 InputStream stream = item.openStream(); | |
73 | |
74 // Process the input stream | |
75 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
76 int len; | |
77 byte[] buffer = new byte[stream.available()]; | |
78 while ((len = stream.read(buffer, 0, buffer.length)) != -1) { | |
79 out.write(buffer, 0, len); | |
80 } | |
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 | 84 |
85 Document fileDoc = XMLUtils.newDocument(); | |
86 | |
87 ElementCreator ec = new ElementCreator(fileDoc, null, null); | |
88 Element root = ec.create("upload"); | |
89 Element id = ec.create("artifact-uuid"); | |
90 id.setTextContent(req.getParameter("uuid")); | |
91 | |
92 Element data = ec.create("data"); | |
93 data.setTextContent(b64File); | |
94 | |
95 fileDoc.appendChild(root); | |
96 root.appendChild(id); | |
97 root.appendChild(data); | |
98 | |
99 return fileDoc; | |
100 } | |
101 } | |
102 catch(Exception e){ | |
103 logger.debug("Failed to create xml document containing the file."); | |
104 logger.debug(e, e); | |
105 } | |
106 return null; | |
107 } | |
108 } |