annotate gwt-client/src/main/java/org/dive4elements/river/client/server/FileUploadServiceImpl.java @ 8203:238fc722f87a

sed 's/logger/log/g' src/**/*.java
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Sep 2014 13:19:22 +0200
parents 97d055c24ad4
children 5e38e2924c07
rev   line source
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5993
ea9eef426962 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5861
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5993
ea9eef426962 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5861
diff changeset
6 * documentation coming with Dive4Elements River for details.
5861
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
172338b1407f GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
9 package org.dive4elements.river.client.server;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
10
5835
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
11 import org.dive4elements.artifacts.common.utils.XMLUtils;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
12 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
13 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
14 import org.dive4elements.artifacts.httpclient.http.HttpClient;
821a02bbfb4e Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5834
diff changeset
15 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
16
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
17 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
18 import java.io.IOException;
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
19 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
20 import java.io.PrintWriter;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
22 import javax.servlet.http.HttpServlet;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23 import javax.servlet.http.HttpServletRequest;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 import javax.servlet.http.HttpServletResponse;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25
2497
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
26 import org.apache.commons.codec.binary.Base64;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
27 import org.apache.commons.fileupload.FileItemIterator;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
28 import org.apache.commons.fileupload.FileItemStream;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
29 import org.apache.commons.fileupload.servlet.ServletFileUpload;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
30 import org.apache.log4j.Logger;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
31 import org.w3c.dom.Document;
a6c6f305546c Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
32 import org.w3c.dom.Element;
2494
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 public class FileUploadServiceImpl
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 extends HttpServlet
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 {
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
37 private static final Logger log = Logger.getLogger(FileUploadServiceImpl.class);
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 @Override
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
41 log.debug("handling post request.");
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 String url = getServletContext().getInitParameter("server-url");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
44
4632
02cf2b1dff84 Make floodmap riveraxis layer name configurable.
Christian Lins <christian.lins@intevation.de>
parents: 2954
diff changeset
45 Document request = createFileXML(req);
2494
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 if (request == null) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48 return;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 HttpClient client = new HttpClientImpl(url);
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 try {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
53 Document result = client.callService(url, "fileupload", request);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
54
6105
d79bfbe55417 Explicitly set Content-Type of shapefile upload response message to text/html. See flys/#657.
Christian Lins <christian.lins@intevation.de>
parents: 5993
diff changeset
55 resp.setContentType("text/html");
6117
427ef7172476 Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 6105
diff changeset
56
5008
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
57 PrintWriter respWriter = resp.getWriter();
6105
d79bfbe55417 Explicitly set Content-Type of shapefile upload response message to text/html. See flys/#657.
Christian Lins <christian.lins@intevation.de>
parents: 5993
diff changeset
58 respWriter.write("<html><body><div style='font-face: Arial,Verdana,sans-serif; font-size: 11px'>");
5008
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
59
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 if (result == null) {
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
61 log.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
62 respWriter.write("FileUpload service returned no result");
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 }
5008
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
64 else {
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
65 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
66 .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
67 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
68 }
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
69
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
70 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
71 respWriter.flush();
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 return;
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 catch (ConnectionException ce) {
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
76 log.error(ce, ce);
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 }
5008
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
78 catch (IOException e) {
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
79 log.error(e, e);
5008
04ccec566689 flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents: 4632
diff changeset
80 }
2494
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
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 protected Document createFileXML(HttpServletRequest req) {
6117
427ef7172476 Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 6105
diff changeset
85 ServletFileUpload upload = new ServletFileUpload();
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 try{
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 FileItemIterator iter = upload.getItemIterator(req);
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 while (iter.hasNext()) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
91 FileItemStream item = iter.next();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
92 InputStream stream = item.openStream();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
94 // Process the input stream
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
6117
427ef7172476 Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 6105
diff changeset
96 int len, cnt = 0;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97 byte[] buffer = new byte[stream.available()];
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
98 while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99 out.write(buffer, 0, len);
6117
427ef7172476 Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents: 6105
diff changeset
100 cnt += len;
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102
6118
97d055c24ad4 Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents: 6117
diff changeset
103 buffer = Base64.encodeBase64(out.toByteArray());
2954
b9433322fcaf Add alpha transp. and fix exceptions on unknown style attributes.
Christian Lins <christian.lins@intevation.de>
parents: 2497
diff changeset
104 String b64File = new String(buffer);
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
105 log.debug("FileUploadServiceImpl.createFileXML(): " + cnt
6118
97d055c24ad4 Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents: 6117
diff changeset
106 + "/" + b64File.length()
97d055c24ad4 Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents: 6117
diff changeset
107 + " bytes (orig/base64) file to be uploaded");
2494
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 Document fileDoc = XMLUtils.newDocument();
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
110
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
111 ElementCreator ec = new ElementCreator(fileDoc, null, null);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
112 Element root = ec.create("upload");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
113 Element id = ec.create("artifact-uuid");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
114 id.setTextContent(req.getParameter("uuid"));
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 Element data = ec.create("data");
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
117 data.setTextContent(b64File);
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 fileDoc.appendChild(root);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
120 root.appendChild(id);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
121 root.appendChild(data);
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
122
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
123 return fileDoc;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
124 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
125 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
126 catch(Exception e){
8203
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
127 log.debug("Failed to create xml document containing the file.");
238fc722f87a sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents: 6118
diff changeset
128 log.debug(e, e);
2494
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
129 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
130 return null;
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
131 }
88b51e6e0334 Issue 657.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
132 }

http://dive4elements.wald.intevation.org