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