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