Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/LProbeService.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 | 5844d7457dde |
children | b199946e9654 |
comparison
equal
deleted
inserted
replaced
324:299be497b78e | 325:30883ab746a5 |
---|---|
73 | 73 |
74 @Inject | 74 @Inject |
75 @Named("dataauthorization") | 75 @Named("dataauthorization") |
76 private Authorization authorization; | 76 private Authorization authorization; |
77 | 77 |
78 @Inject | |
79 @Named("lafimporter") | |
80 private Importer importer; | |
81 | |
82 /** | 78 /** |
83 * The logger for this class. | 79 * The logger for this class. |
84 */ | 80 */ |
85 @Inject | 81 @Inject |
86 private Logger log; | 82 private Logger log; |
251 } | 247 } |
252 catch(AuthenticationException ae) { | 248 catch(AuthenticationException ae) { |
253 return new Response(false, 699, new ArrayList<LProbeInfo>()); | 249 return new Response(false, 699, new ArrayList<LProbeInfo>()); |
254 } | 250 } |
255 } | 251 } |
256 /** | |
257 * Import LProbe object. | |
258 * See | |
259 * http://howtodoinjava.com/2013/05/21/jax-rs-resteasy-file-upload-httpclient-example/ | |
260 * for more details on the implementation. | |
261 * | |
262 * @param input MulitpartFormDataInput containing the file to upload. | |
263 * @param header The HTTP header containing authorization information. | |
264 * @return Response object. | |
265 */ | |
266 @POST | |
267 @Path("/import") | |
268 @Produces("application/json") | |
269 @Consumes("multipart/form-data") | |
270 public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) { | |
271 try { | |
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; | |
312 // TODO: Check Authorisation. How should we check the | |
313 // authorisation while importing? I think we must differ between | |
314 // updating already existing proben and creating new proben. (ti) | |
315 // <2013-08-13 16:24> | |
316 //if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) && | |
317 // auth.getMst().contains(probe.getMstId())) { | |
318 // LProbe p = probe.toLProbe(); | |
319 // return repository.create(p); | |
320 //} | |
321 // TODO: Response must contain a "file" attribute with the name of | |
322 // the uploaded file.(ti) <2013-08-13 16:23> | |
323 //return new Response(false, 698, null); | |
324 } | |
325 catch(AuthenticationException ae) { | |
326 return new Response(false, 699, null); | |
327 } | |
328 } | |
329 } | 252 } |