Mercurial > lada > lada-server
annotate src/main/java/de/intevation/lada/rest/LAFExportService.java @ 328:835eec282baa
New service for LAF export.
This service exports a single probe object identified by its id.
It is reachable via /server/rest/export/laf/$PROBEID.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 27 Aug 2013 09:47:07 +0200 |
parents | |
children | 5c996cf50437 |
rev | line source |
---|---|
328
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.lada.rest; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
2 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
3 import java.io.BufferedOutputStream; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
4 import java.io.File; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
5 import java.io.InputStream; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
6 import java.io.OutputStream; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
7 import java.util.LinkedList; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
8 import java.util.List; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
9 import java.util.Map; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
10 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
11 import javax.enterprise.context.RequestScoped; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
12 import javax.inject.Inject; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
13 import javax.inject.Named; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
14 import javax.ws.rs.Consumes; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
15 import javax.ws.rs.GET; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
16 import javax.ws.rs.POST; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
17 import javax.ws.rs.Path; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
18 import javax.ws.rs.PathParam; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
19 import javax.ws.rs.Produces; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
20 import javax.ws.rs.core.Context; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
21 import javax.ws.rs.core.HttpHeaders; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
22 import javax.ws.rs.core.Response; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
23 import javax.ws.rs.core.Response.ResponseBuilder; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
24 import javax.ws.rs.core.Response.Status; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
25 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
26 import org.json.JSONException; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
27 import org.json.JSONObject; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
28 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
29 import de.intevation.lada.auth.Authentication; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
30 import de.intevation.lada.auth.AuthenticationException; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
31 import de.intevation.lada.auth.AuthenticationResponse; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
32 import de.intevation.lada.auth.Authorization; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
33 import de.intevation.lada.data.exporter.Exporter; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
34 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
35 /** |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
36 * This class produces a RESTful service to export a LAF file. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
37 * |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
38 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
39 */ |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
40 @Path("/export") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
41 @RequestScoped |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
42 public class LAFExportService |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
43 { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
44 @Inject |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
45 @Named("ldapauth") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
46 private Authentication authentication; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
47 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
48 @Inject |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
49 @Named("dataauthorization") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
50 private Authorization authorization; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
51 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
52 @Inject |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
53 @Named("lafexporter") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
54 private Exporter exporter; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
55 /** |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
56 * Import LProbe object. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
57 * See |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
58 * http://howtodoinjava.com/2013/05/21/jax-rs-resteasy-file-upload-httpclient-example/ |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
59 * for more details on the implementation. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
60 * |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
61 * @param input MulitpartFormDataInput containing the file to upload. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
62 * @param header The HTTP header containing authorization information. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
63 * @return Response object. |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
64 */ |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
65 @GET |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
66 @Path("/laf/{id}") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
67 @Produces("text/plain") |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
68 public Response download( |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
69 @PathParam("id") String probeId, |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
70 @Context HttpHeaders header |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
71 ) { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
72 try { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
73 String fileName = "export.laf"; |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
74 AuthenticationResponse auth = authentication.authorizedGroups(header); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
75 if (!authentication.isAuthorizedUser(header)) { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
76 ResponseBuilder response = Response.status(Status.FORBIDDEN); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
77 return response.build(); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
78 } |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
79 if (!authentication.hasAccess(header, probeId) || |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
80 !authorization.isReadOnly(probeId)) { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
81 ResponseBuilder response = Response.status(Status.FORBIDDEN); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
82 return response.build(); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
83 } |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
84 InputStream exported = exporter.export(probeId, auth); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
85 ResponseBuilder response = Response.ok((Object)exported); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
86 response.header( |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
87 "Content-Disposition", |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
88 "attachment; filename=\"" + fileName + "\""); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
89 return response.build(); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
90 |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
91 } |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
92 catch(AuthenticationException ae) { |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
93 ResponseBuilder response = Response.status(Status.FORBIDDEN); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
94 return response.build(); |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
95 } |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
96 } |
835eec282baa
New service for LAF export.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
97 } |