# HG changeset patch # User Raimund Renkert # Date 1377255379 -7200 # Node ID 30883ab746a5a14ea4b82d6d87d6bd3ce45ff201 # Parent 299be497b78ee446c39e1c0b471c926cbde4e3a3 Extracted LAF import service to its own service class. diff -r 299be497b78e -r 30883ab746a5 src/main/java/de/intevation/lada/rest/LAFImportService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/rest/LAFImportService.java Fri Aug 23 12:56:19 2013 +0200 @@ -0,0 +1,108 @@ +package de.intevation.lada.rest; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.commons.io.IOUtils; +import org.jboss.resteasy.plugins.providers.multipart.InputPart; +import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; + +import de.intevation.lada.auth.Authentication; +import de.intevation.lada.auth.AuthenticationException; +import de.intevation.lada.auth.AuthenticationResponse; +import de.intevation.lada.data.importer.Importer; + +/** +* This class produces a RESTful service to import a LAF file. +* +* @author Raimund Renkert +*/ +@Path("/import") +@RequestScoped +public class LAFImportService +{ + @Inject + @Named("ldapauth") + private Authentication authentication; + + @Inject + @Named("lafimporter") + private Importer importer; + + /** + * Import LProbe object. + * See + * http://howtodoinjava.com/2013/05/21/jax-rs-resteasy-file-upload-httpclient-example/ + * for more details on the implementation. + * + * @param input MulitpartFormDataInput containing the file to upload. + * @param header The HTTP header containing authorization information. + * @return Response object. + */ + @POST + @Path("/laf") + @Produces("application/json") + @Consumes("multipart/form-data") + public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) { + try { + AuthenticationResponse auth = authentication.authorizedGroups(header); + if (!authentication.isAuthorizedUser(header)) { + return new Response(false, 698, null); + } + + String name = ""; + String content = ""; + Map> data = input.getFormDataMap(); + try { + List parts = input.getParts(); + for (InputPart part: parts) { + InputStream inStream = part.getBody(InputStream.class, null); + MultivaluedMap headers = part.getHeaders(); + String[] cDisp = headers.getFirst("content-disposition").split(";"); + for (String fName : cDisp) { + if (fName.trim().startsWith("filename")) { + String[] fileName = fName.split("="); + name = fileName[1].trim().replace("\"", ""); + } + } + content = IOUtils.toString(inStream); + } + } + catch (IOException e) { + return new Response(false, 603, null); + } + + boolean success = importer.importData(content, auth); + List respData = new LinkedList(); + respData.add(importer.getErrors()); + respData.add(importer.getWarnings()); + Map fileData = new HashMap(); + fileData.put("filename", name); + respData.add(fileData); + int code = 200; + if (!success) { + code = 660; + } + Response response = new Response(success, code, respData); + return response; + } + catch(AuthenticationException ae) { + return new Response(false, 699, null); + } + } +} diff -r 299be497b78e -r 30883ab746a5 src/main/java/de/intevation/lada/rest/LProbeService.java --- a/src/main/java/de/intevation/lada/rest/LProbeService.java Fri Aug 23 11:49:38 2013 +0200 +++ b/src/main/java/de/intevation/lada/rest/LProbeService.java Fri Aug 23 12:56:19 2013 +0200 @@ -75,10 +75,6 @@ @Named("dataauthorization") private Authorization authorization; - @Inject - @Named("lafimporter") - private Importer importer; - /** * The logger for this class. */ @@ -253,77 +249,4 @@ return new Response(false, 699, new ArrayList()); } } - /** - * Import LProbe object. - * See - * http://howtodoinjava.com/2013/05/21/jax-rs-resteasy-file-upload-httpclient-example/ - * for more details on the implementation. - * - * @param input MulitpartFormDataInput containing the file to upload. - * @param header The HTTP header containing authorization information. - * @return Response object. - */ - @POST - @Path("/import") - @Produces("application/json") - @Consumes("multipart/form-data") - public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) { - try { - AuthenticationResponse auth = authentication.authorizedGroups(header); - if (!authentication.isAuthorizedUser(header)) { - return new Response(false, 698, null); - } - - String name = ""; - String content = ""; - Map> data = input.getFormDataMap(); - try { - List parts = input.getParts(); - for (InputPart part: parts) { - InputStream inStream = part.getBody(InputStream.class, null); - MultivaluedMap headers = part.getHeaders(); - String[] cDisp = headers.getFirst("content-disposition").split(";"); - for (String fName : cDisp) { - if (fName.trim().startsWith("filename")) { - String[] fileName = fName.split("="); - name = fileName[1].trim().replace("\"", ""); - } - } - content = IOUtils.toString(inStream); - } - } - catch (IOException e) { - return new Response(false, 603, null); - } - - boolean success = importer.importData(content, auth); - List respData = new LinkedList(); - respData.add(importer.getErrors()); - respData.add(importer.getWarnings()); - Map fileData = new HashMap(); - fileData.put("filename", name); - respData.add(fileData); - int code = 200; - if (!success) { - code = 660; - } - Response response = new Response(success, code, respData); - return response; - // TODO: Check Authorisation. How should we check the - // authorisation while importing? I think we must differ between - // updating already existing proben and creating new proben. (ti) - // <2013-08-13 16:24> - //if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) && - // auth.getMst().contains(probe.getMstId())) { - // LProbe p = probe.toLProbe(); - // return repository.create(p); - //} - // TODO: Response must contain a "file" attribute with the name of - // the uploaded file.(ti) <2013-08-13 16:23> - //return new Response(false, 698, null); - } - catch(AuthenticationException ae) { - return new Response(false, 699, null); - } - } }