001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.audit;
018
019import java.time.Instant;
020import java.time.OffsetDateTime;
021import java.util.List;
022
023import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
024import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * {@link Endpoint} to expose audit events.
030 *
031 * @author Andy Wilkinson
032 * @since 2.0.0
033 */
034@Endpoint(id = "auditevents")
035public class AuditEventsEndpoint {
036
037        private final AuditEventRepository auditEventRepository;
038
039        public AuditEventsEndpoint(AuditEventRepository auditEventRepository) {
040                Assert.notNull(auditEventRepository, "AuditEventRepository must not be null");
041                this.auditEventRepository = auditEventRepository;
042        }
043
044        @ReadOperation
045        public AuditEventsDescriptor events(@Nullable String principal,
046                        @Nullable OffsetDateTime after, @Nullable String type) {
047                List<AuditEvent> events = this.auditEventRepository.find(principal,
048                                getInstant(after), type);
049                return new AuditEventsDescriptor(events);
050        }
051
052        private Instant getInstant(OffsetDateTime offsetDateTime) {
053                return (offsetDateTime != null) ? offsetDateTime.toInstant() : null;
054        }
055
056        /**
057         * A description of an application's {@link AuditEvent audit events}. Primarily
058         * intended for serialization to JSON.
059         */
060        public static final class AuditEventsDescriptor {
061
062                private final List<AuditEvent> events;
063
064                private AuditEventsDescriptor(List<AuditEvent> events) {
065                        this.events = events;
066                }
067
068                public List<AuditEvent> getEvents() {
069                        return this.events;
070                }
071
072        }
073
074}