Mercurial > dive4elements > river
annotate gwt-client/src/main/java/org/dive4elements/river/client/server/FileUploadServiceImpl.java @ 6199:734ba3732bb6
CapabilitiesParse parses the "queryable" attribute of a WMSLayer now (#1253).
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Wed, 05 Jun 2013 13:13:20 +0200 |
parents | 97d055c24ad4 |
children | 238fc722f87a |
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 { | |
37 private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class); | |
38 | |
39 @Override | |
40 protected void doPost(HttpServletRequest req, HttpServletResponse resp) { | |
41 logger.debug("handling post request."); | |
42 | |
43 String url = getServletContext().getInitParameter("server-url"); | |
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 | 46 |
47 if (request == null) { | |
48 return; | |
49 } | |
50 HttpClient client = new HttpClientImpl(url); | |
51 | |
52 try { | |
53 Document result = client.callService(url, "fileupload", request); | |
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 | 60 if (result == null) { |
61 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
|
62 respWriter.write("FileUpload service returned no result"); |
2494 | 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 | 72 |
73 return; | |
74 } | |
75 catch (ConnectionException ce) { | |
76 logger.error(ce, ce); | |
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) { |
04ccec566689
flys/#657: Shapefile upload now reports success or failure to client.
Christian Lins <christian.lins@intevation.de>
parents:
4632
diff
changeset
|
79 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
|
80 } |
2494 | 81 } |
82 | |
83 | |
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 | 86 |
87 try{ | |
88 FileItemIterator iter = upload.getItemIterator(req); | |
89 | |
90 while (iter.hasNext()) { | |
91 FileItemStream item = iter.next(); | |
92 InputStream stream = item.openStream(); | |
93 | |
94 // Process the input stream | |
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 | 97 byte[] buffer = new byte[stream.available()]; |
98 while ((len = stream.read(buffer, 0, buffer.length)) != -1) { | |
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 | 101 } |
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); |
6118
97d055c24ad4
Fixes bug that caused the garbled upload of user shapefiles (#657).
Christian Lins <christian.lins@intevation.de>
parents:
6117
diff
changeset
|
105 logger.debug("FileUploadServiceImpl.createFileXML(): " + cnt |
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 | 108 |
109 Document fileDoc = XMLUtils.newDocument(); | |
110 | |
111 ElementCreator ec = new ElementCreator(fileDoc, null, null); | |
112 Element root = ec.create("upload"); | |
113 Element id = ec.create("artifact-uuid"); | |
114 id.setTextContent(req.getParameter("uuid")); | |
115 | |
116 Element data = ec.create("data"); | |
117 data.setTextContent(b64File); | |
118 | |
119 fileDoc.appendChild(root); | |
120 root.appendChild(id); | |
121 root.appendChild(data); | |
122 | |
123 return fileDoc; | |
124 } | |
125 } | |
126 catch(Exception e){ | |
127 logger.debug("Failed to create xml document containing the file."); | |
128 logger.debug(e, e); | |
129 } | |
130 return null; | |
131 } | |
132 } |