Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/CSVExportServiceImpl.java @ 9189:13a0c4be17ea
bom for csv export added
author | gernotbelger |
---|---|
date | Thu, 28 Jun 2018 14:11:12 +0200 |
parents | 238fc722f87a |
children |
comparison
equal
deleted
inserted
replaced
9188:3931e8741f53 | 9189:13a0c4be17ea |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.client.server; | 9 package org.dive4elements.river.client.server; |
10 | 10 |
11 import java.io.IOException; | |
12 import java.io.InputStream; | |
13 import java.io.InputStreamReader; | |
14 import java.io.Reader; | |
11 import java.util.ArrayList; | 15 import java.util.ArrayList; |
12 import java.util.List; | 16 import java.util.List; |
13 | 17 |
14 import java.io.Reader; | 18 import org.apache.log4j.Logger; |
15 import java.io.InputStream; | 19 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
16 import java.io.InputStreamReader; | 20 import org.dive4elements.artifacts.common.utils.XMLUtils; |
17 import java.io.IOException; | 21 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
18 | 22 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; |
23 import org.dive4elements.river.client.client.services.CSVExportService; | |
24 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
19 import org.w3c.dom.Document; | 25 import org.w3c.dom.Document; |
20 import org.w3c.dom.Element; | 26 import org.w3c.dom.Element; |
21 | |
22 import org.apache.log4j.Logger; | |
23 | 27 |
24 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | 28 import com.google.gwt.user.server.rpc.RemoteServiceServlet; |
25 | 29 |
26 import au.com.bytecode.opencsv.CSVReader; | 30 import au.com.bytecode.opencsv.CSVReader; |
27 | 31 |
28 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
29 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
30 | |
31 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
32 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
33 | |
34 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
35 import org.dive4elements.river.client.client.services.CSVExportService; | |
36 | |
37 | |
38 /** | 32 /** |
39 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | 33 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> |
40 */ | 34 */ |
41 public class CSVExportServiceImpl | 35 public class CSVExportServiceImpl extends RemoteServiceServlet implements CSVExportService { |
42 extends RemoteServiceServlet | 36 private static final Logger log = Logger.getLogger(CSVExportServiceImpl.class); |
43 implements CSVExportService | |
44 { | |
45 private static final Logger log = | |
46 Logger.getLogger(CSVExportServiceImpl.class); | |
47 | 37 |
38 public static final String ERROR_NO_EXPORT_FOUND = "error_no_export_found"; | |
48 | 39 |
49 public static final String ERROR_NO_EXPORT_FOUND = | 40 @Override |
50 "error_no_export_found"; | 41 public List<String[]> getCSV(final String locale, final String uuid, final String name) throws ServerException { |
51 | |
52 public List<String[]> getCSV( | |
53 String locale, | |
54 String uuid, | |
55 String name) | |
56 throws ServerException | |
57 { | |
58 log.info("CSVExportServiceImpl.getCSV"); | 42 log.info("CSVExportServiceImpl.getCSV"); |
59 | 43 |
60 String url = getServletContext().getInitParameter("server-url"); | 44 final String url = getServletContext().getInitParameter("server-url"); |
61 | 45 |
62 Document requestDoc = XMLUtils.newDocument(); | 46 final Document requestDoc = XMLUtils.newDocument(); |
63 | 47 |
64 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | 48 final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(requestDoc, ArtifactNamespaceContext.NAMESPACE_URI, |
65 requestDoc, | 49 ArtifactNamespaceContext.NAMESPACE_PREFIX); |
66 ArtifactNamespaceContext.NAMESPACE_URI, | |
67 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
68 | 50 |
69 Element action = ec.create("action"); | 51 final Element action = ec.create("action"); |
70 ec.addAttr(action, "type", "csv", true); | 52 ec.addAttr(action, "type", "csv", true); |
71 ec.addAttr(action, "name", name, true); | 53 ec.addAttr(action, "name", name, true); |
72 | 54 |
73 requestDoc.appendChild(action); | 55 requestDoc.appendChild(action); |
74 | 56 |
75 HttpClient client = new HttpClientImpl(url, locale); | 57 final HttpClient client = new HttpClientImpl(url, locale); |
76 | 58 |
77 try { | 59 try { |
78 InputStream in = client.collectionOut(requestDoc, uuid, "export"); | 60 final InputStream in = client.collectionOut(requestDoc, uuid, "export"); |
79 Reader reader = new InputStreamReader (in, "UTF-8"); | 61 final Reader reader = new InputStreamReader(in, "UTF-8"); |
80 CSVReader csvReader = new CSVReader (reader, ';'); | 62 final CSVReader csvReader = new CSVReader(reader, ';'); |
81 | 63 |
82 List<String[]> lines = new ArrayList<String[]>(); | 64 final byte[] BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; |
83 String[] line = null; | 65 final char bomChar = new String(BOM, "UTF-8").charAt(0); |
66 final StringBuilder bomCharCheckBuilder = new StringBuilder().append(bomChar).append("#"); | |
84 | 67 |
68 final List<String[]> lines = new ArrayList<String[]>(); | |
69 String[] line = null; | |
85 while ((line = csvReader.readNext()) != null) { | 70 while ((line = csvReader.readNext()) != null) { |
86 if (line != null) { | 71 if (line != null) { |
87 if (!line[0].startsWith("#") && line.length > 0) { | 72 if (line.length > 0 && !line[0].startsWith("#") && !line[0].startsWith(bomCharCheckBuilder.toString())) { |
88 if (line[0].replace("'", "").length() > 0) { | 73 if (line[0].replace("'", "").length() > 0) { |
89 lines.add(line); | 74 lines.add(line); |
90 } | 75 } |
91 } | 76 } |
92 } | 77 } |
93 } | 78 } |
94 | 79 |
95 return lines; | 80 return lines; |
96 } | 81 } |
97 catch (IOException ce) { | 82 catch (final IOException ce) { |
98 log.error(ce.getLocalizedMessage()); | 83 log.error(ce.getLocalizedMessage()); |
99 } | 84 } |
100 | 85 |
101 throw new ServerException(ERROR_NO_EXPORT_FOUND); | 86 throw new ServerException(ERROR_NO_EXPORT_FOUND); |
102 } | 87 } |