org.apigw.monitoring.web.controller.ActionsController.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.monitoring.web.controller.ActionsController.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */

package org.apigw.monitoring.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apigw.monitoring.svc.repository.AuthEventRepository;
import org.apigw.monitoring.svc.repository.ResourceEventRepository;
import org.apigw.monitoring.types.domain.AuthEvent;
import org.apigw.monitoring.types.domain.ResourceEvent;
import org.apigw.monitoring.web.domain.Action;
import org.apigw.monitoring.web.domain.Actions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(method = RequestMethod.GET, value = "/api/timeline")
public class ActionsController {

    private static final Logger log = LoggerFactory.getLogger(ActionsController.class);

    @Autowired
    AuthEventRepository authEventRepository;

    @Autowired
    ResourceEventRepository resourceEventRepository;

    @RequestMapping("/auth")
    @ResponseStatus(HttpStatus.OK)
    public Actions getAllActions(@RequestParam(value = "user", required = false) String user) {
        log.debug("Listing actions for user: " + user);
        List<Action> actions = new ArrayList<Action>();
        List<AuthEvent> authEvents = authEventRepository.findByUser(user);
        for (AuthEvent authEvent : authEvents) {
            Action action = new Action();
            action.setClient(authEvent.getClient());
            action.setUser(authEvent.getUser());
            action.setService(authEvent.getEventType().toString());
            action.setId(authEvent.getId().toString());
            action.setMessage(authEvent.getMessage());
            action.setScope(StringUtils.commaDelimitedListToSet(authEvent.getScopes()));
            if (authEvent.getRequestState() != null) {
                action.setState(authEvent.getRequestState().toString());
            } else {
                action.setState("");
            }

            action.setTimestamp(authEvent.getTimestamp());
            action.setToken(authEvent.getToken());
            actions.add(action);
        }

        return new Actions(actions);
    }

    @RequestMapping("/resource")
    @ResponseStatus(HttpStatus.OK)
    public Actions getResourceEvents(@RequestParam(value = "user", required = false) String user) {
        log.debug("Listing resource events for user: {}", user);
        List<Action> actions = new ArrayList<>();
        final List<ResourceEvent> resourceEvents = resourceEventRepository.findByUser(user);
        for (ResourceEvent resourceEvent : resourceEvents) {
            Action action = new Action();
            action.setClient(resourceEvent.getClient());
            action.setUser(resourceEvent.getUser());
            action.setId(resourceEvent.getId().toString());
            action.setMessage(resourceEvent.getMessage());
            action.setScope(StringUtils.commaDelimitedListToSet(resourceEvent.getScopes()));
            action.setService(resourceEvent.getEventType().name());
            if (resourceEvent.getRequestState() != null) {
                action.setState(resourceEvent.getRequestState().toString());
            } else {
                action.setState("");
            }
            action.setTimestamp(resourceEvent.getTimestamp());
            actions.add(action);
        }
        return new Actions(actions);
    }

    public AuthEventRepository getAuthEventRepository() {
        return authEventRepository;
    }

    public void setAuthEventRepository(AuthEventRepository authEventRepository) {
        this.authEventRepository = authEventRepository;
    }

    public void setResourceEventRepository(ResourceEventRepository resourceEventRepository) {
        this.resourceEventRepository = resourceEventRepository;
    }
}