comparison src/main/java/de/intevation/lada/rest/LProbeService.java @ 321:5844d7457dde

Completed importer for LAF format. Ort objects still need some attention.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 23 Aug 2013 11:35:24 +0200
parents f0e1caad6027
children 30883ab746a5
comparison
equal deleted inserted replaced
320:6621f7345c06 321:5844d7457dde
1 package de.intevation.lada.rest; 1 package de.intevation.lada.rest;
2 2
3 import java.io.IOException;
4 import java.io.InputStream;
3 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.LinkedList;
4 import java.util.List; 8 import java.util.List;
5 import java.util.Map; 9 import java.util.Map;
6 import java.util.logging.Logger; 10 import java.util.logging.Logger;
7 11
8 import javax.enterprise.context.RequestScoped; 12 import javax.enterprise.context.RequestScoped;
32 import de.intevation.lada.auth.AuthenticationResponse; 36 import de.intevation.lada.auth.AuthenticationResponse;
33 import de.intevation.lada.auth.Authorization; 37 import de.intevation.lada.auth.Authorization;
34 import de.intevation.lada.data.LProbeRepository; 38 import de.intevation.lada.data.LProbeRepository;
35 import de.intevation.lada.data.QueryBuilder; 39 import de.intevation.lada.data.QueryBuilder;
36 import de.intevation.lada.data.Repository; 40 import de.intevation.lada.data.Repository;
41 import de.intevation.lada.data.importer.Importer;
37 import de.intevation.lada.model.LProbe; 42 import de.intevation.lada.model.LProbe;
38 import de.intevation.lada.model.LProbeInfo; 43 import de.intevation.lada.model.LProbeInfo;
39 import de.intevation.lada.utils.QueryTools; 44 import de.intevation.lada.utils.QueryTools;
40 45
41 /** 46 /**
67 private Authentication authentication; 72 private Authentication authentication;
68 73
69 @Inject 74 @Inject
70 @Named("dataauthorization") 75 @Named("dataauthorization")
71 private Authorization authorization; 76 private Authorization authorization;
77
78 @Inject
79 @Named("lafimporter")
80 private Importer importer;
72 81
73 /** 82 /**
74 * The logger for this class. 83 * The logger for this class.
75 */ 84 */
76 @Inject 85 @Inject
259 @Produces("application/json") 268 @Produces("application/json")
260 @Consumes("multipart/form-data") 269 @Consumes("multipart/form-data")
261 public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) { 270 public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) {
262 try { 271 try {
263 AuthenticationResponse auth = authentication.authorizedGroups(header); 272 AuthenticationResponse auth = authentication.authorizedGroups(header);
273 if (!authentication.isAuthorizedUser(header)) {
274 return new Response(false, 698, null);
275 }
276
277 String name = "";
278 String content = "";
279 Map<String, List<InputPart>> data = input.getFormDataMap();
280 try {
281 List<InputPart> parts = input.getParts();
282 for (InputPart part: parts) {
283 InputStream inStream = part.getBody(InputStream.class, null);
284 MultivaluedMap<String, String> headers = part.getHeaders();
285 String[] cDisp = headers.getFirst("content-disposition").split(";");
286 for (String fName : cDisp) {
287 if (fName.trim().startsWith("filename")) {
288 String[] fileName = fName.split("=");
289 name = fileName[1].trim().replace("\"", "");
290 }
291 }
292 content = IOUtils.toString(inStream);
293 }
294 }
295 catch (IOException e) {
296 return new Response(false, 603, null);
297 }
298
299 boolean success = importer.importData(content, auth);
300 List<Object> respData = new LinkedList<Object>();
301 respData.add(importer.getErrors());
302 respData.add(importer.getWarnings());
303 Map<String, String> fileData = new HashMap<String, String>();
304 fileData.put("filename", name);
305 respData.add(fileData);
306 int code = 200;
307 if (!success) {
308 code = 660;
309 }
310 Response response = new Response(success, code, respData);
311 return response;
264 // TODO: Check Authorisation. How should we check the 312 // TODO: Check Authorisation. How should we check the
265 // authorisation while importing? I think we must differ between 313 // authorisation while importing? I think we must differ between
266 // updating already existing proben and creating new proben. (ti) 314 // updating already existing proben and creating new proben. (ti)
267 // <2013-08-13 16:24> 315 // <2013-08-13 16:24>
268 //if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) && 316 //if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) &&
270 // LProbe p = probe.toLProbe(); 318 // LProbe p = probe.toLProbe();
271 // return repository.create(p); 319 // return repository.create(p);
272 //} 320 //}
273 // TODO: Response must contain a "file" attribute with the name of 321 // TODO: Response must contain a "file" attribute with the name of
274 // the uploaded file.(ti) <2013-08-13 16:23> 322 // the uploaded file.(ti) <2013-08-13 16:23>
275 return new Response(true, 200, null);
276 //return new Response(false, 698, null); 323 //return new Response(false, 698, null);
277 } 324 }
278 catch(AuthenticationException ae) { 325 catch(AuthenticationException ae) {
279 return new Response(false, 699, null); 326 return new Response(false, 699, null);
280 } 327 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)