Skip to content

Commit f1479e9

Browse files
committed
new version 0.0.5
1 parent 2a951ee commit f1479e9

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

RELEASE_NOTE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### <code>0.0.5</code> :calendar: 05/02/2025
66
**Improvements**
77
* Refactoring of `InfoProxyController` and add `DateUtils` dependency.
8+
* Add `StartupListener` listener to log (display) the current version of *Billing Proxy* at startup.
9+
* Include exception handling with `ControllerExceptionHandler.java` class.
810

911
**BugFixing**
1012
* Set `org.apache.coyote.http11: INFO` to avoid the `Error parsing HTTP request header`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package it.eng.dome.billing.proxy.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.client.RestTemplate;
6+
7+
@Configuration
8+
public class AppConfig {
9+
10+
@Bean
11+
public RestTemplate restTemplate() {
12+
return new RestTemplate();
13+
}
14+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package it.eng.dome.billing.proxy.exception;
2+
3+
import java.net.ConnectException;
4+
import java.net.SocketTimeoutException;
5+
import java.net.UnknownHostException;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.core.Ordered;
10+
import org.springframework.core.annotation.Order;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.web.bind.annotation.ControllerAdvice;
14+
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
import org.springframework.web.client.HttpClientErrorException;
16+
import org.springframework.web.client.HttpServerErrorException;
17+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
18+
import it.eng.dome.brokerage.exception.ErrorResponse;
19+
import jakarta.servlet.http.HttpServletRequest;
20+
21+
@Order(Ordered.HIGHEST_PRECEDENCE)
22+
@ControllerAdvice
23+
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {
24+
25+
private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);
26+
27+
@ExceptionHandler(IllegalArgumentException.class)
28+
protected ResponseEntity<Object> handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException ex) {
29+
return buildResponseEntity(new ErrorResponse(request, HttpStatus.BAD_REQUEST, ex));
30+
}
31+
32+
@ExceptionHandler(IllegalStateException.class)
33+
protected ResponseEntity<Object> handleIllegalStateException(HttpServletRequest request, IllegalStateException ex) {
34+
return buildResponseEntity(new ErrorResponse(request,HttpStatus.BAD_REQUEST, ex));
35+
}
36+
37+
@ExceptionHandler(ConnectException.class)
38+
protected ResponseEntity<Object> handleConnectionException(HttpServletRequest request, ConnectException ex) {
39+
return buildResponseEntity(new ErrorResponse(request,HttpStatus.SERVICE_UNAVAILABLE, ex));
40+
}
41+
42+
@ExceptionHandler(UnknownHostException.class)
43+
protected ResponseEntity<Object> handleConnectionException(HttpServletRequest request, UnknownHostException ex) {
44+
return buildResponseEntity(new ErrorResponse(request,HttpStatus.SERVICE_UNAVAILABLE, ex));
45+
}
46+
47+
@ExceptionHandler(HttpClientErrorException.class)
48+
protected ResponseEntity<Object> handleBadRequest(HttpServletRequest request, HttpClientErrorException ex) {
49+
return buildResponseEntity(new ErrorResponse(request, HttpStatus.BAD_REQUEST, ex));
50+
}
51+
52+
@ExceptionHandler(HttpServerErrorException.class)
53+
protected ResponseEntity<Object> handleInternalServerError(HttpServletRequest request, HttpServerErrorException ex) {
54+
return buildResponseEntity(new ErrorResponse(request, HttpStatus.INTERNAL_SERVER_ERROR, ex));
55+
}
56+
57+
@ExceptionHandler(SocketTimeoutException.class)
58+
protected ResponseEntity<Object> handleSocketTimeoutError(HttpServletRequest request, SocketTimeoutException ex) {
59+
return buildResponseEntity(new ErrorResponse(request, HttpStatus.REQUEST_TIMEOUT, ex));
60+
}
61+
62+
@ExceptionHandler(it.eng.dome.tmforum.tmf620.v4.ApiException.class)
63+
protected ResponseEntity<Object> handleApiException(HttpServletRequest request, it.eng.dome.tmforum.tmf620.v4.ApiException ex) {
64+
return buildResponseEntity(new ErrorResponse(request, HttpStatus.SERVICE_UNAVAILABLE, ex));
65+
}
66+
67+
private ResponseEntity<Object> buildResponseEntity(ErrorResponse errorResponse) {
68+
logger.error("{} - {}", errorResponse.getStatus(), errorResponse.getMessage());
69+
return new ResponseEntity<>(errorResponse, errorResponse.getStatus());
70+
}
71+
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package it.eng.dome.billing.proxy.listener;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.boot.context.event.ApplicationReadyEvent;
7+
import org.springframework.context.event.EventListener;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.client.RestTemplate;
10+
import it.eng.dome.billing.proxy.model.InfoProxy;
11+
12+
@Component
13+
public class StartupListener {
14+
15+
private static final Logger logger = LoggerFactory.getLogger(StartupListener.class);
16+
17+
private final String INFO_PATH = "/proxy/info";
18+
private final RestTemplate restTemplate;
19+
20+
@Value("${server.port}")
21+
private int serverPort;
22+
23+
@Value("${server.servlet.context-path}")
24+
private String contextPath;
25+
26+
public StartupListener(RestTemplate restTemplate) {
27+
this.restTemplate = restTemplate;
28+
}
29+
30+
@EventListener(ApplicationReadyEvent.class)
31+
public void onApplicationReady() {
32+
33+
String path = contextPath + INFO_PATH;
34+
String url = "http://localhost:" + serverPort + path.replaceAll("//+", "/");
35+
36+
logger.info("Listener GET call to {}", url);
37+
try {
38+
InfoProxy response = restTemplate.getForObject(url, InfoProxy.class);
39+
logger.info("Started the {} version: {} ", response.getName(), response.getVersion());
40+
41+
} catch (Exception e) {
42+
logger.error("Error calling {}: {}", url, e.getMessage());
43+
}
44+
}
45+
46+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package it.eng.dome.billing.proxy.model;
2+
3+
public class InfoProxy {
4+
5+
public String name;
6+
public String version;
7+
public String release_time;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public String getVersion() {
14+
return version;
15+
}
16+
17+
public String getRelease_time() {
18+
return release_time;
19+
}
20+
}

0 commit comments

Comments
 (0)