Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 2508:4aa70825bde1
map print: generate valid url to access service.
flys-client/trunk@4363 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 09 May 2012 11:18:24 +0000 |
parents | a6c6f305546c |
children | b9433322fcaf |
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.ElementCreator; |
2494 | 4 |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
5 import de.intevation.artifacts.common.utils.XMLUtils; |
2494 | 6 |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
7 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; |
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 de.intevation.artifacts.httpclient.http.HttpClient; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
10 import de.intevation.artifacts.httpclient.http.HttpClientImpl; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
11 |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
12 import java.io.ByteArrayOutputStream; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
13 import java.io.InputStream; |
2494 | 14 |
15 import javax.servlet.http.HttpServlet; | |
16 import javax.servlet.http.HttpServletRequest; | |
17 import javax.servlet.http.HttpServletResponse; | |
18 | |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
19 import org.apache.commons.codec.binary.Base64; |
2494 | 20 |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
21 import org.apache.commons.fileupload.FileItemIterator; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
22 import org.apache.commons.fileupload.FileItemStream; |
2494 | 23 |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
24 import org.apache.commons.fileupload.servlet.ServletFileUpload; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
25 |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
26 import org.apache.log4j.Logger; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
27 |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
28 import org.w3c.dom.Document; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
29 import org.w3c.dom.Element; |
2494 | 30 |
31 public class FileUploadServiceImpl | |
32 extends HttpServlet | |
33 { | |
34 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class); | |
35 | |
36 @Override | |
37 protected void doPost(HttpServletRequest req, HttpServletResponse resp) { | |
38 processPost(req, resp); | |
39 } | |
40 | |
41 | |
42 protected void processPost(HttpServletRequest req, HttpServletResponse resp) { | |
43 logger.debug("handling post request."); | |
44 | |
45 String url = getServletContext().getInitParameter("server-url"); | |
46 | |
47 Document request = createFileXML(req);; | |
48 | |
49 if (request == null) { | |
50 return; | |
51 } | |
52 HttpClient client = new HttpClientImpl(url); | |
53 | |
54 try { | |
55 Document result = client.callService(url, "fileupload", request); | |
56 | |
57 if (result == null) { | |
58 logger.warn("FileUpload service returned no result."); | |
59 } | |
60 | |
61 return; | |
62 } | |
63 catch (ConnectionException ce) { | |
64 logger.error(ce, ce); | |
65 } | |
66 } | |
67 | |
68 | |
69 protected Document createFileXML(HttpServletRequest req) { | |
70 ServletFileUpload upload = new ServletFileUpload(); | |
71 | |
72 try{ | |
73 FileItemIterator iter = upload.getItemIterator(req); | |
74 | |
75 while (iter.hasNext()) { | |
76 FileItemStream item = iter.next(); | |
77 | |
78 String name = item.getFieldName(); | |
79 InputStream stream = item.openStream(); | |
80 | |
81 | |
82 // Process the input stream | |
83 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
84 int len; | |
85 byte[] buffer = new byte[stream.available()]; | |
86 while ((len = stream.read(buffer, 0, buffer.length)) != -1) { | |
87 out.write(buffer, 0, len); | |
88 } | |
89 | |
90 Base64 encoder = new Base64(); | |
91 String b64File = Base64.encodeBase64String(buffer); | |
92 | |
93 Document fileDoc = XMLUtils.newDocument(); | |
94 | |
95 ElementCreator ec = new ElementCreator(fileDoc, null, null); | |
96 Element root = ec.create("upload"); | |
97 Element id = ec.create("artifact-uuid"); | |
98 id.setTextContent(req.getParameter("uuid")); | |
99 | |
100 Element data = ec.create("data"); | |
101 data.setTextContent(b64File); | |
102 | |
103 fileDoc.appendChild(root); | |
104 root.appendChild(id); | |
105 root.appendChild(data); | |
106 | |
107 return fileDoc; | |
108 } | |
109 } | |
110 catch(Exception e){ | |
111 logger.debug("Failed to create xml document containing the file."); | |
112 logger.debug(e, e); | |
113 } | |
114 return null; | |
115 } | |
116 } |