Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 5424:657392b30bb4
Fix flys/issue1150 (i18n).
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Tue, 26 Mar 2013 11:42:47 +0100 |
parents | 04ccec566689 |
children |
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; |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
10 import java.io.IOException; |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
11 import java.io.InputStream; |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
12 import java.io.PrintWriter; |
2494 | 13 |
14 import javax.servlet.http.HttpServlet; | |
15 import javax.servlet.http.HttpServletRequest; | |
16 import javax.servlet.http.HttpServletResponse; | |
17 | |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
18 import org.apache.commons.codec.binary.Base64; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
19 import org.apache.commons.fileupload.FileItemIterator; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
20 import org.apache.commons.fileupload.FileItemStream; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
21 import org.apache.commons.fileupload.servlet.ServletFileUpload; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
22 import org.apache.log4j.Logger; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
23 import org.w3c.dom.Document; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
24 import org.w3c.dom.Element; |
2494 | 25 |
26 public class FileUploadServiceImpl | |
27 extends HttpServlet | |
28 { | |
29 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class); | |
30 | |
31 @Override | |
32 protected void doPost(HttpServletRequest req, HttpServletResponse resp) { | |
33 logger.debug("handling post request."); | |
34 | |
35 String url = getServletContext().getInitParameter("server-url"); | |
36 | |
4632
02cf2b1dff84
Make floodmap riveraxis layer name configurable.
Christian Lins <christian.lins@intevation.de>
parents:
2954
diff
changeset
|
37 Document request = createFileXML(req); |
2494 | 38 |
39 if (request == null) { | |
40 return; | |
41 } | |
42 HttpClient client = new HttpClientImpl(url); | |
43 | |
44 try { | |
45 Document result = client.callService(url, "fileupload", request); | |
46 | |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
47 PrintWriter respWriter = resp.getWriter(); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
48 respWriter.write("<html><link href='FLYS.css' rel='stylesheet' type='text/css'>"); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
49 respWriter.write("<body><div style='font-face: Arial,Verdana,sans-serif; font-size: 11px'>"); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
50 |
2494 | 51 if (result == null) { |
52 logger.warn("FileUpload service returned no result."); | |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
53 respWriter.write("FileUpload service returned no result"); |
2494 | 54 } |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
55 else { |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
56 String status = result.getElementsByTagName("status") |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
57 .item(0).getTextContent(); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
58 respWriter.write(status); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
59 } |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
60 |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
61 respWriter.write("</div></body></html>"); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
62 respWriter.flush(); |
2494 | 63 |
64 return; | |
65 } | |
66 catch (ConnectionException ce) { | |
67 logger.error(ce, ce); | |
68 } | |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
69 catch (IOException e) { |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
70 logger.error(e, e); |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
71 } |
2494 | 72 } |
73 | |
74 | |
75 protected Document createFileXML(HttpServletRequest req) { | |
76 ServletFileUpload upload = new ServletFileUpload(); | |
77 | |
78 try{ | |
79 FileItemIterator iter = upload.getItemIterator(req); | |
80 | |
81 while (iter.hasNext()) { | |
82 FileItemStream item = iter.next(); | |
83 InputStream stream = item.openStream(); | |
84 | |
85 // Process the input stream | |
86 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
87 int len; | |
88 byte[] buffer = new byte[stream.available()]; | |
89 while ((len = stream.read(buffer, 0, buffer.length)) != -1) { | |
90 out.write(buffer, 0, len); | |
91 } | |
92 | |
2954
b9433322fcaf
Add alpha transp. and fix exceptions on unknown style attributes.
Christian Lins <christian.lins@intevation.de>
parents:
2497
diff
changeset
|
93 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
|
94 String b64File = new String(buffer); |
2494 | 95 |
96 Document fileDoc = XMLUtils.newDocument(); | |
97 | |
98 ElementCreator ec = new ElementCreator(fileDoc, null, null); | |
99 Element root = ec.create("upload"); | |
100 Element id = ec.create("artifact-uuid"); | |
101 id.setTextContent(req.getParameter("uuid")); | |
102 | |
103 Element data = ec.create("data"); | |
104 data.setTextContent(b64File); | |
105 | |
106 fileDoc.appendChild(root); | |
107 root.appendChild(id); | |
108 root.appendChild(data); | |
109 | |
110 return fileDoc; | |
111 } | |
112 } | |
113 catch(Exception e){ | |
114 logger.debug("Failed to create xml document containing the file."); | |
115 logger.debug(e, e); | |
116 } | |
117 return null; | |
118 } | |
119 } |