view src/main/java/de/intevation/lada/rest/exporter/LafExportService.java @ 1183:f252a92aae1c

Do not try to export non-existant Probe objects. Doing so led to 'null' being printed instead of valid LAF.
author Tom Gottfried <tom@intevation.de>
date Fri, 18 Nov 2016 19:50:17 +0100
parents 186d602e031a
children
line wrap: on
line source
/* 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.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.JsonValue;
import javax.json.JsonNumber;
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 de.intevation.lada.model.land.Probe;
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.annotation.RepositoryConfig;
import de.intevation.lada.util.data.QueryBuilder;
import de.intevation.lada.util.data.Repository;
import de.intevation.lada.util.data.RepositoryType;
import de.intevation.lada.util.auth.Authorization;
import de.intevation.lada.util.auth.AuthorizationType;
import de.intevation.lada.util.auth.UserInfo;

/**
 * REST service to export probe objects and the child objects associated with
 * the selected Probe objects.
 * <p>
 * To request objects post a JSON formatted string with an array of probe ids.
 * <pre>
 * <code>
 * {
 *  "proben": [[number], [number], ...]
 * }
 * </code>
 * </pre>
 *
 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
 */
@Path("data/export")
@RequestScoped
public class LafExportService {

    /**
     * The data repository granting read-only access.
     */
    @Inject
    @RepositoryConfig(type=RepositoryType.RO)
    private Repository repository;

    /**
     * The exporter.
     */
    @Inject
    @ExportConfig(format=ExportFormat.LAF)
    private Exporter exporter;

    /**
     * The authorization module.
     */
    @Inject
    @AuthorizationConfig(type=AuthorizationType.HEADER)
    private Authorization authorization;


    /**
     * Export Probe objects.
     *
     * The service takes JSON formatted  POST data containing probe ids and
     * exports the Probe objects filtered by these ids.
     *
     * @param proben    JSON formatted string with an array of 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
    ) {
        List<Integer> providedIds = new ArrayList<Integer>();
        for (JsonValue id : proben.getJsonArray("proben")) {
            if (id instanceof JsonNumber) {
                providedIds.add(((JsonNumber)id).intValue());
            }
        }

        QueryBuilder<Probe> pBuilder = new QueryBuilder<Probe>(
            repository.entityManager("land"), Probe.class);
        pBuilder.andIn("id", providedIds);
        List<Probe> pObjects = repository.filterPlain(
            pBuilder.getQuery(), "land");

        if (pObjects.isEmpty()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }

        List<Integer> probeIds = new ArrayList<Integer>();
        for (Probe p : pObjects) {
            probeIds.add(p.getId());
        }
        UserInfo userInfo = authorization.getInfo(request);
        InputStream exported = exporter.export(probeIds, userInfo);

        ResponseBuilder response = Response.ok((Object)exported);
        response.header(
            "Content-Disposition",
            "attachment; filename=\"export.laf\"");
        return response.build();
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)