[Lada-commits] [PATCH 10 of 10] Added REST services for Im-/Export for laf files

Wald Commits scm-commit at wald.intevation.org
Thu Apr 16 15:49:25 CEST 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1429192202 -7200
# Node ID 23ab3247b36eb07c6099d49b0c09d846a38c48a2
# Parent  8782e3a00b71e0561ccc2ce2cf087f61c57fd468
Added REST services for Im-/Export for laf files.

diff -r 8782e3a00b71 -r 23ab3247b36e src/main/java/de/intevation/lada/rest/exporter/LafExportService.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/rest/exporter/LafExportService.java	Thu Apr 16 15:50:02 2015 +0200
@@ -0,0 +1,87 @@
+package de.intevation.lada.rest.exporter;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.servlet.http.HttpServletRequest;
+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.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.lada.exporter.ExportConfig;
+import de.intevation.lada.exporter.ExportFormat;
+import de.intevation.lada.exporter.Exporter;
+import de.intevation.lada.util.annotation.AuthorizationConfig;
+import de.intevation.lada.util.auth.Authorization;
+import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.auth.UserInfo;
+
+/**
+ * This class produces a RESTful service to interact with probe objects.
+ *
+ * @author <a href="mailto:rrenkert at intevation.de">Raimund Renkert</a>
+ */
+ at Path("export")
+ at RequestScoped
+public class LafExportService {
+
+    /* The logger used in this class.*/
+    @Inject
+    private Logger logger;
+
+    @Inject
+    @ExportConfig(format=ExportFormat.LAF)
+    private Exporter exporter;
+
+    @Inject
+    @AuthorizationConfig(type=AuthorizationType.OPEN_ID)
+    Authorization authorization;
+
+
+    /**
+     * Export LProbe objects.
+     *
+     * The service takes form url encoded POST data containing probe ids and
+     * exports the LProbe objects filtered by these ids.
+     *
+     * @param proben    Form data (url encoded) string with probe ids.
+     * @param header    The HTTP header containing authorization information.
+     * @return The LAF file to export.
+     */
+    @POST
+    @Path("/laf")
+    @Consumes("application/json")
+    @Produces("text/plain")
+    public Response download(
+        JsonObject proben,
+        @Context HttpServletRequest request
+    ) {
+        JsonArray array = proben.getJsonArray("proben");
+        List<Integer> probeIds = new ArrayList<Integer>();
+        String fileName = "export.laf";
+        UserInfo userInfo = authorization.getInfo(request);
+        for (int i = 0; i < array.size(); i++) {
+            Integer probeId = array.getInt(i);
+            //if (authorization.isAuthorized(userInfo, probeId)) {
+                probeIds.add(probeId);
+            //}
+        }
+        InputStream exported = exporter.export(probeIds, userInfo);
+        ResponseBuilder response = Response.ok((Object)exported);
+        response.header(
+            "Content-Disposition",
+            "attachment; filename=\"" + fileName + "\"");
+        return response.build();
+    }
+}
diff -r 8782e3a00b71 -r 23ab3247b36e src/main/java/de/intevation/lada/rest/importer/LafImportService.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/rest/importer/LafImportService.java	Thu Apr 16 15:50:02 2015 +0200
@@ -0,0 +1,81 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3) 
+ * and comes with ABSOLUTELY NO WARRANTY! Check out 
+ * the documentation coming with IMIS-Labordaten-Application for details. 
+ */
+package de.intevation.lada.rest.importer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+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.MediaType;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.lada.importer.ImportConfig;
+import de.intevation.lada.importer.ImportFormat;
+import de.intevation.lada.importer.Importer;
+import de.intevation.lada.util.annotation.AuthorizationConfig;
+import de.intevation.lada.util.auth.Authorization;
+import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.auth.UserInfo;
+import de.intevation.lada.util.rest.Response;
+
+/**
+ * This class produces a RESTful service to interact with probe objects.
+ *
+ * @author <a href="mailto:rrenkert at intevation.de">Raimund Renkert</a>
+ */
+ at Path("import")
+ at RequestScoped
+public class LafImportService {
+
+    /* The logger used in this class.*/
+    @Inject
+    private Logger logger;
+
+    @Inject
+    @ImportConfig(format=ImportFormat.LAF)
+    private Importer importer;
+
+    @Inject
+    @AuthorizationConfig(type=AuthorizationType.OPEN_ID)
+    private Authorization authorization;
+
+    /**
+     * Import a file in the LAF format.
+     *
+     * @param input MulitpartFormDataInput containing the file to upload.
+     * @param header    The HTTP header containing authorization information.
+     * @return Response object.
+     */
+    @POST
+    @Path("/laf")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.TEXT_PLAIN)
+    public Response upload(
+        String content,
+        @Context HttpServletRequest request) {
+        UserInfo userInfo = authorization.getInfo(request);
+
+        logger.debug(content);
+        importer.doImport(content, userInfo);
+        Map<String, Object> respData = new HashMap<String,Object>();
+        respData.put("errors", importer.getErrors());
+        respData.put("warnings", importer.getWarnings());
+        int code = 200;
+        Response response = new Response(importer.getErrors().isEmpty(), code, respData);
+        importer.reset();
+        return response;
+    }
+}


More information about the Lada-commits mailing list