Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/exporter/JsonExportService.java @ 1043:1b9516c60a5a schema-update
Added json export.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 05 Sep 2016 10:19:23 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1042:3a5a9a0492d1 | 1043:1b9516c60a5a |
---|---|
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU GPL (v>=3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out | |
6 * the documentation coming with IMIS-Labordaten-Application for details. | |
7 */ | |
8 package de.intevation.lada.rest.exporter; | |
9 | |
10 import java.io.IOException; | |
11 import java.io.InputStream; | |
12 import java.util.ArrayList; | |
13 import java.util.List; | |
14 | |
15 import javax.enterprise.context.RequestScoped; | |
16 import javax.inject.Inject; | |
17 import javax.json.JsonArray; | |
18 import javax.json.JsonObject; | |
19 import javax.servlet.http.HttpServletRequest; | |
20 import javax.ws.rs.Consumes; | |
21 import javax.ws.rs.POST; | |
22 import javax.ws.rs.Path; | |
23 import javax.ws.rs.Produces; | |
24 import javax.ws.rs.core.Context; | |
25 | |
26 import org.apache.commons.io.IOUtils; | |
27 | |
28 import de.intevation.lada.exporter.ExportConfig; | |
29 import de.intevation.lada.exporter.ExportFormat; | |
30 import de.intevation.lada.exporter.Exporter; | |
31 import de.intevation.lada.util.annotation.AuthorizationConfig; | |
32 import de.intevation.lada.util.auth.Authorization; | |
33 import de.intevation.lada.util.auth.AuthorizationType; | |
34 import de.intevation.lada.util.auth.UserInfo; | |
35 import de.intevation.lada.util.rest.Response; | |
36 | |
37 /** | |
38 * REST service to export probe objects and the child objects associated with | |
39 * the selected Probe objects. | |
40 * <p> | |
41 * To request objects post a JSON formatted string with an array of probe ids. | |
42 * <pre> | |
43 * <code> | |
44 * { | |
45 * "proben": [[number], [number], ...] | |
46 * } | |
47 * </code> | |
48 * </pre> | |
49 * | |
50 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> | |
51 */ | |
52 @Path("data/export") | |
53 @RequestScoped | |
54 public class JsonExportService { | |
55 | |
56 /** | |
57 * The exporter. | |
58 */ | |
59 @Inject | |
60 @ExportConfig(format=ExportFormat.JSON) | |
61 private Exporter exporter; | |
62 | |
63 /** | |
64 * The authorization module. | |
65 */ | |
66 @Inject | |
67 @AuthorizationConfig(type=AuthorizationType.HEADER) | |
68 private Authorization authorization; | |
69 | |
70 | |
71 /** | |
72 * Export Probe objects. | |
73 * | |
74 * The service takes JSON formatted POST data containing probe ids and | |
75 * exports the Probe objects filtered by these ids. | |
76 * | |
77 * @param proben JSON formatted string with an array of probe ids. | |
78 * @param header The HTTP header containing authorization information. | |
79 * @return The LAF file to export. | |
80 */ | |
81 @POST | |
82 @Path("/json") | |
83 @Consumes("application/json") | |
84 @Produces("application/json") | |
85 public String download( | |
86 JsonObject proben, | |
87 @Context HttpServletRequest request | |
88 ) { | |
89 JsonArray array = proben.getJsonArray("proben"); | |
90 List<Integer> probeIds = new ArrayList<Integer>(); | |
91 UserInfo userInfo = authorization.getInfo(request); | |
92 for (int i = 0; i < array.size(); i++) { | |
93 Integer probeId = array.getInt(i); | |
94 probeIds.add(probeId); | |
95 } | |
96 InputStream exported = exporter.export(probeIds, userInfo); | |
97 if (exported == null) { | |
98 return new Response(false, 600, null).toString(); | |
99 } | |
100 try { | |
101 return IOUtils.toString(exported, "utf-8"); | |
102 } catch (IOException e) { | |
103 return new Response(false, 600, null).toString(); | |
104 } | |
105 } | |
106 } | |
107 |