001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util.health;
019
020
021import java.util.Objects;
022
023import net.jcip.annotations.Immutable;
024
025import com.nimbusds.jose.proc.SecurityContext;
026import com.nimbusds.jose.util.events.Event;
027
028
029/**
030 * Health report.
031 *
032 * @version 2022-08-29
033 * @author Vladimir Dzhuvinov
034 */
035@Immutable
036public class HealthReport <S, C extends SecurityContext> implements Event<S, C> {
037        
038        
039        /**
040         * The event source.
041         */
042        private final S source;
043        
044        
045        /**
046         * The health status.
047         */
048        private final HealthStatus status;
049        
050        
051        /**
052         * The exception in case of a {@link HealthStatus#NOT_HEALTHY}.
053         */
054        private final Exception exception;
055        
056        
057        /**
058         * The report timestamp.
059         */
060        private final long timestamp;
061        
062        
063        /**
064         * The optional context.
065         */
066        private final C context;
067        
068        
069        /**
070         * Creates a new health report.
071         *
072         * @param source    The event source.
073         * @param status    The health status. Must not be {@code null}.
074         * @param timestamp The timestamp, in milliseconds since the Unix
075         *                  epoch.
076         * @param context   The optional context, {@code null} if not
077         *                  specified.
078         */
079        public HealthReport(final S source,
080                            final HealthStatus status,
081                            final long timestamp,
082                            final C context) {
083                this(source, status, null, timestamp, context);
084        }
085        
086        
087        /**
088         * Creates a new health report.
089         *
090         * @param source    The event source.
091         * @param status    The health status. Must not be {@code null}.
092         * @param exception The exception in case of a
093         *                  {@link HealthStatus#NOT_HEALTHY}, {@code null} if
094         *                  not specified.
095         * @param timestamp The timestamp, in milliseconds since the Unix
096         *                  epoch.
097         * @param context   The optional context, {@code null} if not
098         *                  specified.
099         */
100        public HealthReport(final S source,
101                            final HealthStatus status,
102                            final Exception exception,
103                            final long timestamp,
104                            final C context) {
105                Objects.requireNonNull(source);
106                this.source = source;
107                Objects.requireNonNull(status);
108                this.status = status;
109                if (exception != null && HealthStatus.HEALTHY.equals(status)) {
110                        throw new IllegalArgumentException("Exception not accepted for a healthy status");
111                }
112                this.exception = exception;
113                this.timestamp = timestamp;
114                this.context = context;
115        }
116        
117        
118        @Override
119        public S getSource() {
120                return source;
121        }
122        
123        
124        @Override
125        public C getContext() {
126                return context;
127        }
128        
129        
130        /**
131         * Returns the health status.
132         *
133         * @return The health status.
134         */
135        public HealthStatus getHealthStatus() {
136                return status;
137        }
138        
139        
140        /**
141         * Returns the recorded exception in case of a
142         * {@link HealthStatus#NOT_HEALTHY}.
143         *
144         * @return The exception, {@code null} if not specified.
145         */
146        public Exception getException() {
147                return exception;
148        }
149        
150        
151        /**
152         * Returns the timestamp.
153         *
154         * @return The timestamp, in milliseconds since the Unix epoch.
155         */
156        public long getTimestamp() {
157                return timestamp;
158        }
159        
160        
161        @Override
162        public String toString() {
163                final StringBuilder sb = new StringBuilder("HealthReport{");
164                sb.append("source=").append(source);
165                sb.append(", status=").append(status);
166                sb.append(", exception=").append(exception);
167                sb.append(", timestamp=").append(timestamp);
168                sb.append(", context=").append(context);
169                sb.append('}');
170                return sb.toString();
171        }
172}