annotate flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 5008:04ccec566689

flys/#657: Shapefile upload now reports success or failure to client.
author Christian Lins <christian.lins@intevation.de>
date Mon, 18 Feb 2013 16:29:37 +0100
parents 02cf2b1dff84
children
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;
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14 import javax.servlet.http.HttpServlet;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
15 import javax.servlet.http.HttpServletRequest;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
16 import javax.servlet.http.HttpServletResponse;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
26 public class FileUploadServiceImpl
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27 extends HttpServlet
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
28 {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
29 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
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 @Override
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33 logger.debug("handling post request.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 String url = getServletContext().getInitParameter("server-url");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39 if (request == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 return;
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 HttpClient client = new HttpClientImpl(url);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44 try {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
45 Document result = client.callService(url, "fileupload", request);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 if (result == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 return;
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 catch (ConnectionException ce) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 logger.error(ce, ce);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73
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 protected Document createFileXML(HttpServletRequest req) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 ServletFileUpload upload = new ServletFileUpload();
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 try{
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 FileItemIterator iter = upload.getItemIterator(req);
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 while (iter.hasNext()) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 FileItemStream item = iter.next();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 InputStream stream = item.openStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 // Process the input stream
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 ByteArrayOutputStream out = new ByteArrayOutputStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 int len;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 byte[] buffer = new byte[stream.available()];
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 out.write(buffer, 0, len);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
95
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
96 Document fileDoc = XMLUtils.newDocument();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98 ElementCreator ec = new ElementCreator(fileDoc, null, null);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99 Element root = ec.create("upload");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 Element id = ec.create("artifact-uuid");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 id.setTextContent(req.getParameter("uuid"));
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 Element data = ec.create("data");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104 data.setTextContent(b64File);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
106 fileDoc.appendChild(root);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 root.appendChild(id);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
108 root.appendChild(data);
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 return fileDoc;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
111 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
112 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
113 catch(Exception e){
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
114 logger.debug("Failed to create xml document containing the file.");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
115 logger.debug(e, e);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
116 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
117 return null;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
118 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
119 }

http://dive4elements.wald.intevation.org