View Javadoc
1   /*
2    * Copyright (c) 2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.services.sms77.filter;
19  
20  import de.kaiserpfalzedv.services.sms77.mapper.Sms77Exception;
21  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22  import feign.InvocationContext;
23  import feign.ResponseInterceptor;
24  import io.micrometer.core.instrument.Counter;
25  import io.micrometer.core.instrument.MeterRegistry;
26  import io.micrometer.core.instrument.Tags;
27  import jakarta.annotation.PostConstruct;
28  import jakarta.annotation.PreDestroy;
29  import jakarta.inject.Inject;
30  import jakarta.inject.Singleton;
31  import lombok.RequiredArgsConstructor;
32  
33  /**
34   * <p>Sms77RequestReportFilter -- Reports the send SMS.</p>
35   *
36   * @author klenkes74 {@literal <rlichti@kaiserpfalz-edv.de>}
37   * @since 1.0.0  2023-01-22
38   */
39  @SuppressFBWarnings(value = {"EI_EXPOSE_REP","EI_EXPOSE_REP2"}, justification = "lombok provided @Getter are created")
40  @Singleton
41  @RequiredArgsConstructor(onConstructor = @__(@Inject))
42  public class Sms77RequestReportFilter implements ResponseInterceptor {
43      private static final String API_REQUESTS_HANDLED_SINCE_START = "sms77.sms-since-start";
44  
45      private final MeterRegistry registry;
46      private Counter requestCounter;
47  
48      @PostConstruct
49      public void registerMetric() {
50          this.requestCounter = this.registry.counter(API_REQUESTS_HANDLED_SINCE_START, Tags.empty());
51      }
52  
53      @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "The field is set in the PostConstruct method of this class.")
54      @PreDestroy
55      public void unregisterMetrics() {
56          assert this.registry != null;
57  
58          this.requestCounter.close();
59      }
60  
61      /**
62       * This filter prevents the external call when there are no credits left for this API.
63       *
64       * @param requestContext request context.
65       * @throws Sms77Exception When the credits for this API has been used and there are no
66       *                                               credits remaining.
67       */
68  
69      @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "The field is set in the PostConstruct method of this class.")
70      @Override
71      public Object intercept(final InvocationContext invocationContext, final Chain chain) throws Exception {
72          assert this.registry != null;
73  
74          this.requestCounter.increment();
75  
76          return chain.next(invocationContext);
77      }
78  }