Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/LAFImportService.java @ 325:30883ab746a5
Extracted LAF import service to its own service class.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Fri, 23 Aug 2013 12:56:19 +0200 |
parents | |
children | 5d11428e6a09 |
comparison
equal
deleted
inserted
replaced
324:299be497b78e | 325:30883ab746a5 |
---|---|
1 package de.intevation.lada.rest; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.util.HashMap; | |
6 import java.util.LinkedList; | |
7 import java.util.List; | |
8 import java.util.Map; | |
9 | |
10 import javax.enterprise.context.RequestScoped; | |
11 import javax.inject.Inject; | |
12 import javax.inject.Named; | |
13 import javax.ws.rs.Consumes; | |
14 import javax.ws.rs.POST; | |
15 import javax.ws.rs.Path; | |
16 import javax.ws.rs.Produces; | |
17 import javax.ws.rs.core.Context; | |
18 import javax.ws.rs.core.HttpHeaders; | |
19 import javax.ws.rs.core.MultivaluedMap; | |
20 | |
21 import org.apache.commons.io.IOUtils; | |
22 import org.jboss.resteasy.plugins.providers.multipart.InputPart; | |
23 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; | |
24 | |
25 import de.intevation.lada.auth.Authentication; | |
26 import de.intevation.lada.auth.AuthenticationException; | |
27 import de.intevation.lada.auth.AuthenticationResponse; | |
28 import de.intevation.lada.data.importer.Importer; | |
29 | |
30 /** | |
31 * This class produces a RESTful service to import a LAF file. | |
32 * | |
33 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> | |
34 */ | |
35 @Path("/import") | |
36 @RequestScoped | |
37 public class LAFImportService | |
38 { | |
39 @Inject | |
40 @Named("ldapauth") | |
41 private Authentication authentication; | |
42 | |
43 @Inject | |
44 @Named("lafimporter") | |
45 private Importer importer; | |
46 | |
47 /** | |
48 * Import LProbe object. | |
49 * See | |
50 * http://howtodoinjava.com/2013/05/21/jax-rs-resteasy-file-upload-httpclient-example/ | |
51 * for more details on the implementation. | |
52 * | |
53 * @param input MulitpartFormDataInput containing the file to upload. | |
54 * @param header The HTTP header containing authorization information. | |
55 * @return Response object. | |
56 */ | |
57 @POST | |
58 @Path("/laf") | |
59 @Produces("application/json") | |
60 @Consumes("multipart/form-data") | |
61 public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) { | |
62 try { | |
63 AuthenticationResponse auth = authentication.authorizedGroups(header); | |
64 if (!authentication.isAuthorizedUser(header)) { | |
65 return new Response(false, 698, null); | |
66 } | |
67 | |
68 String name = ""; | |
69 String content = ""; | |
70 Map<String, List<InputPart>> data = input.getFormDataMap(); | |
71 try { | |
72 List<InputPart> parts = input.getParts(); | |
73 for (InputPart part: parts) { | |
74 InputStream inStream = part.getBody(InputStream.class, null); | |
75 MultivaluedMap<String, String> headers = part.getHeaders(); | |
76 String[] cDisp = headers.getFirst("content-disposition").split(";"); | |
77 for (String fName : cDisp) { | |
78 if (fName.trim().startsWith("filename")) { | |
79 String[] fileName = fName.split("="); | |
80 name = fileName[1].trim().replace("\"", ""); | |
81 } | |
82 } | |
83 content = IOUtils.toString(inStream); | |
84 } | |
85 } | |
86 catch (IOException e) { | |
87 return new Response(false, 603, null); | |
88 } | |
89 | |
90 boolean success = importer.importData(content, auth); | |
91 List<Object> respData = new LinkedList<Object>(); | |
92 respData.add(importer.getErrors()); | |
93 respData.add(importer.getWarnings()); | |
94 Map<String, String> fileData = new HashMap<String, String>(); | |
95 fileData.put("filename", name); | |
96 respData.add(fileData); | |
97 int code = 200; | |
98 if (!success) { | |
99 code = 660; | |
100 } | |
101 Response response = new Response(success, code, respData); | |
102 return response; | |
103 } | |
104 catch(AuthenticationException ae) { | |
105 return new Response(false, 699, null); | |
106 } | |
107 } | |
108 } |