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