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
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.ElementCreator;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15 import javax.servlet.http.HttpServlet;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
16 import javax.servlet.http.HttpServletRequest;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
17 import javax.servlet.http.HttpServletResponse;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 public class FileUploadServiceImpl
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 extends HttpServlet
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 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 @Override
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 processPost(req, resp);
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
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 protected void processPost(HttpServletRequest req, HttpServletResponse resp) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 logger.debug("handling post request.");
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 String url = getServletContext().getInitParameter("server-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 Document request = createFileXML(req);;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 if (request == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 return;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52 HttpClient client = new HttpClientImpl(url);
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 try {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 Document result = client.callService(url, "fileupload", request);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 if (result == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 logger.warn("FileUpload service returned no result.");
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 return;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
62 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 catch (ConnectionException ce) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 logger.error(ce, ce);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 }
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 protected Document createFileXML(HttpServletRequest req) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70 ServletFileUpload upload = new ServletFileUpload();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 try{
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73 FileItemIterator iter = upload.getItemIterator(req);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 while (iter.hasNext()) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 FileItemStream item = iter.next();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 String name = item.getFieldName();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 InputStream stream = item.openStream();
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 // Process the input stream
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 ByteArrayOutputStream out = new ByteArrayOutputStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 int len;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 byte[] buffer = new byte[stream.available()];
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 out.write(buffer, 0, len);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 Base64 encoder = new Base64();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 String b64File = Base64.encodeBase64String(buffer);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
92
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93 Document fileDoc = XMLUtils.newDocument();
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 ElementCreator ec = new ElementCreator(fileDoc, null, null);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
96 Element root = ec.create("upload");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97 Element id = ec.create("artifact-uuid");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98 id.setTextContent(req.getParameter("uuid"));
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 Element data = ec.create("data");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 data.setTextContent(b64File);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
103 fileDoc.appendChild(root);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104 root.appendChild(id);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105 root.appendChild(data);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
106
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 return fileDoc;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
108 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
109 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
110 catch(Exception e){
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
111 logger.debug("Failed to create xml document containing the file.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
112 logger.debug(e, e);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
113 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
114 return null;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
115 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
116 }

http://dive4elements.wald.intevation.org