view src/main/java/de/intevation/lada/rest/LMessungService.java @ 211:30d2aad7371e

Updated authorization in LMessung service filter.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 03 Jul 2013 12:02:57 +0200
parents a305412206a3
children 5b232dab4b50
line wrap: on
line source
package de.intevation.lada.rest;

import java.util.ArrayList;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
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 javax.ws.rs.core.UriInfo;

import de.intevation.lada.authentication.Authentication;
import de.intevation.lada.authentication.AuthenticationException;
import de.intevation.lada.authentication.AuthenticationResponse;
import de.intevation.lada.data.QueryBuilder;
import de.intevation.lada.data.Repository;
import de.intevation.lada.model.LMessung;

/**
* This class produces a RESTful service to read, write and update
* LMessung objects.
*
* @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
*/
@Path("/messung")
@RequestScoped
public class LMessungService
{
    /**
     * The Repository for LMessung.
     */
    @Inject
    @Named("lmessungrepository")
    private Repository repository;

    /**
     * The authorization module.
     */
    @Inject
    @Named("ldapauth")
    private Authentication authentication;

    /**
     * Request LMessung via a filter.
     *
     * Query parameters are used for the filter in form of key-value pairs.
     * This filter can take the three parameters
     *   probe=$PROBEID (String)
     *
     * @param info The URL query parameters.
     * @return JSON Object via Rest service.
     */
    @GET
    @Produces("text/json")
    public Response filter(
        @Context UriInfo info,
        @Context HttpHeaders headers
    ) {
        try {
            if (!authentication.isAuthorizedUser(headers)) {
                return new Response(false, 699, new ArrayList<LMessung>());
            }
            MultivaluedMap<String, String> params = info.getQueryParameters();
            if (params.isEmpty() || !(params.containsKey("probeId"))) {
                return new Response(false, 609, new ArrayList<LMessung>());
            }
            String probeId = params.getFirst("probeId");
            if (authentication.hasAccess(headers, probeId)) {
                QueryBuilder<LMessung> builder =
                    new QueryBuilder<LMessung>(
                        repository.getEntityManager(),
                        LMessung.class);
                builder.and("probeId", probeId);
                return repository.filter(builder.getQuery());
            }
            return new Response(false, 698, new ArrayList<LMessung>());
        }
        catch(AuthenticationException ae) {
            return new Response(false, 699, new ArrayList<LMessung>());
        }
    }

    /**
     * Update a LMessung object.
     *
     * @param messung   The LMessung object to update.
     * @param headers   The HTTP header containing authorization information.
     * @return Response object.
     */
    @PUT
    @Path("/{id}")
    @Produces("text/json")
    @Consumes("application/json")
    public Response update(
        LMessung messung,
        @Context HttpHeaders headers
    ) {
        try {
            String probeId = messung.getLProbeId();
            if (authentication.hasAccess(headers, probeId)) {
                return repository.update(messung);
            }
            return new Response(false, 698, new ArrayList<LMessung>());
        }
        catch(AuthenticationException ae) {
            return new Response(false, 699, new ArrayList<LMessung>());
        }
    }

    /**
     * Create a new LMessung object.
     *
     * @param messung   The new LMessung object.
     * @param headers   The HTTP header containing authorization information.
     * @return Response object.
     */
    @POST
    @Produces("text/json")
    @Consumes("application/json")
    public Response create(
        LMessung messung,
        @Context HttpHeaders headers
    ) {
        try {
            String probeId = messung.getLProbeId();
            if (authentication.hasAccess(headers, probeId)) {
                return repository.create(messung);
            }
            return new Response(false, 698, new ArrayList<LMessung>());
        }
        catch(AuthenticationException ae) {
            return new Response(false, 699, new ArrayList<LMessung>());
        }
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)