fix: Fixed generated problem-details for mutated messages
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / api / ChatBackendControllerAdvice.java
1 package de.juplo.kafka.chat.backend.api;
2
3 import de.juplo.kafka.chat.backend.domain.MessageMutationException;
4 import org.springframework.beans.factory.annotation.Value;
5 import org.springframework.http.HttpStatus;
6 import org.springframework.http.ProblemDetail;
7 import org.springframework.web.bind.annotation.ControllerAdvice;
8 import org.springframework.web.bind.annotation.ExceptionHandler;
9 import org.springframework.web.server.ServerWebExchange;
10 import org.springframework.web.util.UriComponentsBuilder;
11
12 import java.util.Date;
13
14
15 @ControllerAdvice
16 public class ChatBackendControllerAdvice
17 {
18   @Value("${server.context-path:/}")
19   String contextPath;
20
21   @ExceptionHandler(UnknownChatroomException.class)
22   public final ProblemDetail handleException(
23       UnknownChatroomException e,
24       ServerWebExchange exchange,
25       UriComponentsBuilder uriComponentsBuilder)
26   {
27     final HttpStatus status = HttpStatus.NOT_FOUND;
28     ProblemDetail problem = ProblemDetail.forStatus(status);
29
30     problem.setProperty("timestamp", new Date());
31
32     problem.setProperty("requestId", exchange.getRequest().getId());
33
34     problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/unknown-chatroom").build().toUri());
35     StringBuilder stringBuilder = new StringBuilder();
36     stringBuilder.append(status.getReasonPhrase());
37     stringBuilder.append(" - ");
38     stringBuilder.append(e.getMessage());
39     problem.setTitle(stringBuilder.toString());
40
41     stringBuilder.setLength(0);
42     stringBuilder.append("Chatroom unknown: ");
43     stringBuilder.append(e.getChatroomId());
44     problem.setDetail(stringBuilder.toString());
45
46     problem.setProperty("chatroomId", e.getChatroomId());
47
48     return problem;
49   }
50
51   @ExceptionHandler(MessageMutationException.class)
52   public final ProblemDetail handleException(
53       MessageMutationException e,
54       ServerWebExchange exchange,
55       UriComponentsBuilder uriComponentsBuilder)
56   {
57     final HttpStatus status = HttpStatus.BAD_REQUEST;
58     ProblemDetail problem = ProblemDetail.forStatus(status);
59
60     problem.setProperty("timestamp", new Date());
61
62     problem.setProperty("requestId", exchange.getRequest().getId());
63
64     problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/message-mutation").build().toUri());
65     StringBuilder stringBuilder = new StringBuilder();
66     stringBuilder.append(status.getReasonPhrase());
67     stringBuilder.append(" - ");
68     stringBuilder.append(e.getMessage());
69     problem.setTitle(stringBuilder.toString());
70
71     stringBuilder.setLength(0);
72     stringBuilder.append("The existing message with user=");
73     stringBuilder.append(e.getExisting().getUsername());
74     stringBuilder.append(" and id=");
75     stringBuilder.append(e.getExisting().getId());
76     stringBuilder.append(" cannot be mutated!");
77     problem.setDetail(stringBuilder.toString());
78
79     problem.setProperty("mutatedMessage", MessageTo.from(e.getMutated()));
80
81     problem.setProperty("existingMessage", MessageTo.from(e.getExisting()));
82
83     return problem;
84   }
85 }