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 com.nimbusds.jose.proc.SecurityContext;
022import com.nimbusds.jose.util.events.Event;
023import net.jcip.annotations.Immutable;
024
025import java.util.Objects;
026
027
028/**
029 * Health report.
030 *
031 * @version 2022-08-29
032 * @author Vladimir Dzhuvinov
033 */
034@Immutable
035public class HealthReport <S, C extends SecurityContext> implements Event<S, C> {
036        
037        
038        /**
039         * The event source.
040         */
041        private final S source;
042        
043        
044        /**
045         * The health status.
046         */
047        private final HealthStatus status;
048        
049        
050        /**
051         * The exception in case of a {@link HealthStatus#NOT_HEALTHY}.
052         */
053        private final Exception exception;
054        
055        
056        /**
057         * The report timestamp.
058         */
059        private final long timestamp;
060        
061        
062        /**
063         * The optional context.
064         */
065        private final C context;
066        
067        
068        /**
069         * Creates a new health report.
070         *
071         * @param source    The event source.
072         * @param status    The health status. Must not be {@code null}.
073         * @param timestamp The timestamp, in milliseconds since the Unix
074         *                  epoch.
075         * @param context   The optional context, {@code null} if not required.
076         */
077        public HealthReport(final S source,
078                            final HealthStatus status,
079                            final long timestamp,
080                            final C context) {
081                this(source, status, null, timestamp, context);
082        }
083        
084        
085        /**
086         * Creates a new health report.
087         *
088         * @param source    The event source.
089         * @param status    The health status. Must not be {@code null}.
090         * @param exception The exception in case of a
091         *                  {@link HealthStatus#NOT_HEALTHY}, {@code null} if
092         *                  not specified.
093         * @param timestamp The timestamp, in milliseconds since the Unix
094         *                  epoch.
095         * @param context   The optional context, {@code null} if not required.
096         */
097        public HealthReport(final S source,
098                            final HealthStatus status,
099                            final Exception exception,
100                            final long timestamp,
101                            final C context) {
102                Objects.requireNonNull(source);
103                this.source = source;
104                Objects.requireNonNull(status);
105                this.status = status;
106                if (exception != null && HealthStatus.HEALTHY.equals(status)) {
107                        throw new IllegalArgumentException("Exception not accepted for a healthy status");
108                }
109                this.exception = exception;
110                this.timestamp = timestamp;
111                this.context = context;
112        }
113        
114        
115        @Override
116        public S getSource() {
117                return source;
118        }
119        
120        
121        @Override
122        public C getContext() {
123                return context;
124        }
125        
126        
127        /**
128         * Returns the health status.
129         *
130         * @return The health status.
131         */
132        public HealthStatus getHealthStatus() {
133                return status;
134        }
135        
136        
137        /**
138         * Returns the recorded exception in case of a
139         * {@link HealthStatus#NOT_HEALTHY}.
140         *
141         * @return The exception, {@code null} if not specified.
142         */
143        public Exception getException() {
144                return exception;
145        }
146        
147        
148        /**
149         * Returns the timestamp.
150         *
151         * @return The timestamp, in milliseconds since the Unix epoch.
152         */
153        public long getTimestamp() {
154                return timestamp;
155        }
156        
157        
158        @Override
159        public String toString() {
160                final StringBuilder sb = new StringBuilder("HealthReport{");
161                sb.append("source=").append(source);
162                sb.append(", status=").append(status);
163                sb.append(", exception=").append(exception);
164                sb.append(", timestamp=").append(timestamp);
165                sb.append(", context=").append(context);
166                sb.append('}');
167                return sb.toString();
168        }
169}