org.apigw.monitoring.svc.impl.JmsOAuthMonitoringService.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.monitoring.svc.impl.JmsOAuthMonitoringService.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.svc.impl;

import org.apigw.monitoring.svc.OAuthMonitoringService;
import org.apigw.monitoring.svc.exception.ApigwMonitoringException;
import org.apigw.monitoring.types.enums.AuthEventType;
import org.apigw.monitoring.types.enums.ResourceEventType;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Service
public class JmsOAuthMonitoringService implements OAuthMonitoringService {

    private static Logger log = LoggerFactory.getLogger(JmsOAuthMonitoringService.class);
    private JmsTemplate template;
    private Destination monitoringQueue;

    private static final ObjectMapper mapper = new ObjectMapper();

    public void logAuthorizationGrant(long timestamp, String client, Set<String> scopes, String code, String state,
            String message, String user) {
        log.debug("logAuthorizationGrant( client: {}, scopes: {}, state: {}, message: {}, code: {}", client, scopes,
                state, message, code);
        sendLog(createAuthLog(timestamp, null, null, client, scopes, state, message, user, code,
                AuthEventType.AUTHORIZATIONGRANT), AuthEventType.AUTHORIZATIONGRANT.toString());
    }

    public void logCreateAccessToken(long timestamp, String token, String client, Set<String> scopes, String code,
            String state, String message, String user) {
        log.debug(
                "logCreateAccessToken(token: {}, client: {}, scopes: {}, state: {}, message: {}, code: {}, user: {}",
                token, client, scopes, state, message, code, user);
        sendLog(createAuthLog(timestamp, null, token, client, scopes, state, message, user, code,
                AuthEventType.CREATEACCESSTOKEN), AuthEventType.CREATEACCESSTOKEN.toString());
    }

    public void logRevokeAccessToken(long timestamp, String token, String client, Set<String> scopes, String state,
            String message, String user) {
        log.debug("logRevokeAccessToken(token: {}, client: {}, scopes: {}, state: {}, message: {}, user: {}", token,
                client, scopes, state, message, user);
        sendLog(createAuthLog(timestamp, null, token, client, scopes, state, message, user, null,
                AuthEventType.REVOKEACCESSTOKEN), AuthEventType.REVOKEACCESSTOKEN.toString());
    }

    public void logLoadAuthentication(long timestamp, String token, String client, Set<String> scopes, String state,
            String message, String user) {
        log.debug("logLoadAuthentication(token: {}, client: {}, scopes: {}, state: {}, message: {}, user: {}",
                token, client, scopes, state, message, user);
        sendLog(createAuthLog(timestamp, null, token, client, scopes, state, message, user, null,
                AuthEventType.LOADAUTHENTICATION), AuthEventType.LOADAUTHENTICATION.toString());
    }

    public void logLoadAuthentication(long timestamp, String id, String token, String client, Set<String> scopes,
            String state, String message, String user) {
        log.debug(
                "logLoadAuthentication(id: {}, token: {}, client: {}, scopes: {}, state: {}, message: {}, user: {}",
                id, token, client, scopes, state, message, user);
        sendLog(createAuthLog(timestamp, id, token, client, scopes, state, message, user, null,
                AuthEventType.LOADAUTHENTICATION), AuthEventType.LOADAUTHENTICATION.toString());
    }

    public void logStart(long timestamp, String corrId, String endpoint, String resource, String ip, String message,
            Map<String, String> headers) {
        log.debug("logStart(corrId: {}, endpoint: {}, resource: {})", corrId, endpoint, resource);
        sendLog(createResourceLog(timestamp, corrId, endpoint, resource, ip, message, headers, 0,
                ResourceEventType.LOGSTART), ResourceEventType.LOGSTART.toString());
    }

    public void logEnd(long timestamp, String corrId, String message, int httpStatus, Map<String, String> headers) {
        log.debug("logEnd(corrId: {})", corrId);
        sendLog(createResourceLog(timestamp, corrId, null, null, null, message, headers, httpStatus,
                ResourceEventType.LOGEND), ResourceEventType.LOGEND.toString());
    }

    @Override
    public void logRequest(long timestamp, String corrId, String endpoint, String resource, String method,
            Set<String> scopes, String requestBody, String state, String message, String client, String user) {
        log.debug(
                "logRequest(corrId: {}, endpoint: {}, resource: {}, method: {}, scopes: {}, requestBody: {}, state: {}, message: {}, client: {}, user: {})",
                corrId, resource, method, scopes, requestBody, state, message, client, user);
        sendLog(createResourceLog(timestamp, corrId, endpoint, resource, method, scopes, requestBody, state,
                message, ResourceEventType.LOGREQUEST, client, user), ResourceEventType.LOGREQUEST.toString());
    }

    /**
     *
     * @param id TODO
     * @param token
     * @param client
     * @param scope
     * @param state
     * @param message
     * @param authEvent TODO
     * @return
     * @throws org.apigw.monitoring.svc.exception.ApigwMonitoringException
     */
    private String createAuthLog(long timestamp, String id, String token, String client, Set<String> scope,
            String state, String message, String user, String code, AuthEventType authEvent) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (id != null) {
            map.put("id", id);
        } else {
            map.put("id", UUID.randomUUID().toString());
        }

        map.put("timestamp", timestamp);
        map.put("state", state);

        if (token != null) {
            map.put("token", token);
        }
        if (client != null) {
            map.put("client", client);
        }
        if (scope != null) {
            map.put("scope", scope);
        }
        if (message != null) {
            map.put("message", message);
        }
        if (user != null) {
            map.put("user", user);
        }
        if (code != null) {
            map.put("code", code);
        }

        if (authEvent != null) {
            map.put("eventType", authEvent.toString());
        }
        try {
            return mapper.writeValueAsString(map);
        } catch (IOException e) {
            log.error("caught error while trying to create log for event {}: {}",
                    authEvent != null ? authEvent.toString() : null, e);
            throw new ApigwMonitoringException(e);
        }
    }

    private String createResourceLog(long timestamp, String corrId, String endpoint, String resource, String ip,
            String message, Map<String, String> headers, int httpStatus, ResourceEventType resourceEvent) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("id", UUID.randomUUID().toString());
        map.put("timestamp", timestamp);
        map.put("correlationId", corrId);

        if (resource != null) {
            map.put("resource", resource);
        }

        if (endpoint != null) {
            map.put("endpoint", endpoint);
        }

        if (ip != null) {
            map.put("ip", ip);
        }

        if (message != null) {
            map.put("message", message);
        }

        if (httpStatus > 0) {
            map.put("httpStatus", httpStatus);
        }

        if (resourceEvent != null) {
            map.put("eventType", resourceEvent.toString());
        }
        try {
            return mapper.writeValueAsString(map);
        } catch (IOException e) {
            log.error("caught error while trying to create log for event {}: {}",
                    resourceEvent != null ? resourceEvent.toString() : null, e);
            throw new ApigwMonitoringException(e);
        }
    }

    private String createResourceLog(long timestamp, String corrId, String endpoint, String resource, String method,
            Set<String> scopes, String requestBody, String state, String message, ResourceEventType resourceEvent,
            String client, String user) {
        Map<String, Object> resourceLog = new HashMap<>();

        resourceLog.put("id", UUID.randomUUID().toString());
        resourceLog.put("timestamp", timestamp);
        resourceLog.put("correlationId", corrId);
        resourceLog.put("state", state);
        resourceLog.put("method", method);

        if (resource != null) {
            resourceLog.put("resource", resource);
        }
        if (endpoint != null) {
            resourceLog.put("endpoint", endpoint);
        }
        if (scopes != null) {
            resourceLog.put("scopes", scopes);
        }
        if (requestBody != null) {
            resourceLog.put("requestBody", requestBody);
        }
        if (message != null) {
            resourceLog.put("message", message);
        }
        if (resourceEvent != null) {
            resourceLog.put("eventType", resourceEvent.toString());
        }
        if (client != null) {
            resourceLog.put("client", client);
        }
        if (user != null) {
            resourceLog.put("user", user);
        }
        try {
            return mapper.writeValueAsString(resourceLog);
        } catch (IOException e) {
            log.error("caught error while trying to create log for event {}: {}",
                    resourceEvent != null ? resourceEvent.toString() : null, e);
            throw new ApigwMonitoringException(e);
        }
    }

    private void sendLog(final String logObject, String eventName) {
        log.debug("sendLog(log: {}, queue: {})", logObject, monitoringQueue.toString());
        try {
            template.send(monitoringQueue, new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                    TextMessage message = session.createTextMessage(logObject);
                    return message;
                }
            });
        } catch (Exception e) {
            log.error("caught error while trying to send log for event {}:", eventName, e);
            throw new ApigwMonitoringException(e);
        }
    }

    public JmsTemplate getTemplate() {
        return template;
    }

    public void setTemplate(JmsTemplate template) {
        this.template = template;
    }

    public Destination getMonitoringQueue() {
        return monitoringQueue;
    }

    public void setMonitoringQueue(Destination monitoringQueue) {
        this.monitoringQueue = monitoringQueue;
    }
}