annotate src/main/java/de/intevation/lada/rest/exporter/JsonExportService.java @ 1097:186d602e031a

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