Mercurial > dive4elements > river
annotate gwt-client/src/main/java/org/dive4elements/river/client/server/FileUploadServiceImpl.java @ 9801:1d7a72a50183 3.2.x tip
Assume Compose V2, consistently
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 23 Nov 2023 10:14:13 +0100 |
parents | 0a5239a1e46e |
children |
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 | 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 | 21 |
22 import javax.servlet.http.HttpServlet; | |
23 import javax.servlet.http.HttpServletRequest; | |
24 import javax.servlet.http.HttpServletResponse; | |
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; |
9726 | 30 import org.apache.logging.log4j.Logger; |
31 import org.apache.logging.log4j.LogManager; | |
2497
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
32 import org.w3c.dom.Document; |
a6c6f305546c
Removed superfluous imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2494
diff
changeset
|
33 import org.w3c.dom.Element; |
2494 | 34 |
35 public class FileUploadServiceImpl | |
36 extends HttpServlet | |
37 { | |
9726 | 38 private static final Logger log = LogManager.getLogger( |
8856 | 39 FileUploadServiceImpl.class); |
2494 | 40 |
41 @Override | |
42 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
|
43 log.debug("handling post request."); |
2494 | 44 |
45 String url = getServletContext().getInitParameter("server-url"); | |
46 | |
4632
02cf2b1dff84
Make floodmap riveraxis layer name configurable.
Christian Lins <christian.lins@intevation.de>
parents:
2954
diff
changeset
|
47 Document request = createFileXML(req); |
2494 | 48 |
49 if (request == null) { | |
50 return; | |
51 } | |
52 HttpClient client = new HttpClientImpl(url); | |
53 | |
54 try { | |
55 Document result = client.callService(url, "fileupload", request); | |
56 | |
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
|
57 resp.setContentType("text/html"); |
6117
427ef7172476
Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents:
6105
diff
changeset
|
58 |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
59 PrintWriter respWriter = resp.getWriter(); |
8856 | 60 respWriter.write( |
61 "<html><body><div style='font-face: Arial,Verdana,sans-serif; " | |
62 + "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
|
63 |
2494 | 64 if (result == null) { |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
6118
diff
changeset
|
65 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
|
66 respWriter.write("FileUpload service returned no result"); |
2494 | 67 } |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
68 else { |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
69 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
|
70 .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
|
71 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
|
72 } |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
73 |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
74 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
|
75 respWriter.flush(); |
2494 | 76 |
77 return; | |
78 } | |
79 catch (ConnectionException ce) { | |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
6118
diff
changeset
|
80 log.error(ce, ce); |
2494 | 81 } |
5008
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
82 catch (IOException e) { |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
6118
diff
changeset
|
83 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
|
84 } |
2494 | 85 } |
86 | |
87 | |
88 protected Document createFileXML(HttpServletRequest req) { | |
6117
427ef7172476
Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents:
6105
diff
changeset
|
89 ServletFileUpload upload = new ServletFileUpload(); |
2494 | 90 |
91 try{ | |
92 FileItemIterator iter = upload.getItemIterator(req); | |
93 | |
94 while (iter.hasNext()) { | |
95 FileItemStream item = iter.next(); | |
96 InputStream stream = item.openStream(); | |
97 | |
98 // Process the input stream | |
99 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
6117
427ef7172476
Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents:
6105
diff
changeset
|
100 int len, cnt = 0; |
2494 | 101 byte[] buffer = new byte[stream.available()]; |
102 while ((len = stream.read(buffer, 0, buffer.length)) != -1) { | |
103 out.write(buffer, 0, len); | |
6117
427ef7172476
Add some debug output to FileUploadServiceImpl.
Christian Lins <christian.lins@intevation.de>
parents:
6105
diff
changeset
|
104 cnt += len; |
2494 | 105 } |
106 | |
6118
97d055c24ad4
Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents:
6117
diff
changeset
|
107 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
|
108 String b64File = new String(buffer); |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
6118
diff
changeset
|
109 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
|
110 + "/" + b64File.length() |
97d055c24ad4
Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents:
6117
diff
changeset
|
111 + " bytes (orig/base64) file to be uploaded"); |
2494 | 112 |
113 Document fileDoc = XMLUtils.newDocument(); | |
114 | |
115 ElementCreator ec = new ElementCreator(fileDoc, null, null); | |
116 Element root = ec.create("upload"); | |
117 Element id = ec.create("artifact-uuid"); | |
118 id.setTextContent(req.getParameter("uuid")); | |
119 | |
120 Element data = ec.create("data"); | |
121 data.setTextContent(b64File); | |
122 | |
123 fileDoc.appendChild(root); | |
124 root.appendChild(id); | |
125 root.appendChild(data); | |
126 | |
127 return fileDoc; | |
128 } | |
129 } | |
130 catch(Exception e){ | |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
6118
diff
changeset
|
131 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
|
132 log.debug(e, e); |
2494 | 133 } |
134 return null; | |
135 } | |
136 } |