refactor: Refined the creation of new `ChatRoom`s
[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.InvalidUsernameException;
4 import de.juplo.kafka.chat.backend.domain.MessageMutationException;
5 import de.juplo.kafka.chat.backend.domain.UnknownChatroomException;
6 import org.springframework.beans.factory.annotation.Value;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.ProblemDetail;
9 import org.springframework.web.bind.annotation.ControllerAdvice;
10 import org.springframework.web.bind.annotation.ExceptionHandler;
11 import org.springframework.web.server.ServerWebExchange;
12 import org.springframework.web.util.UriComponentsBuilder;
13
14 import java.util.Date;
15
16
17 @ControllerAdvice
18 public class ChatBackendControllerAdvice
19 {
20   @Value("${server.context-path:/}")
21   String contextPath;
22
23   @ExceptionHandler(UnknownChatroomException.class)
24   public final ProblemDetail handleException(
25       UnknownChatroomException e,
26       ServerWebExchange exchange,
27       UriComponentsBuilder uriComponentsBuilder)
28   {
29     final HttpStatus status = HttpStatus.NOT_FOUND;
30     ProblemDetail problem = ProblemDetail.forStatus(status);
31
32     problem.setProperty("timestamp", new Date());
33
34     problem.setProperty("requestId", exchange.getRequest().getId());
35
36     problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/unknown-chatroom").build().toUri());
37     StringBuilder stringBuilder = new StringBuilder();
38     stringBuilder.append(status.getReasonPhrase());
39     stringBuilder.append(" - ");
40     stringBuilder.append(e.getMessage());
41     problem.setTitle(stringBuilder.toString());
42
43     stringBuilder.setLength(0);
44     stringBuilder.append("Chatroom unknown: ");
45     stringBuilder.append(e.getChatroomId());
46     problem.setDetail(stringBuilder.toString());
47
48     problem.setProperty("chatroomId", e.getChatroomId());
49
50     return problem;
51   }
52
53   @ExceptionHandler(MessageMutationException.class)
54   public final ProblemDetail handleException(
55       MessageMutationException e,
56       ServerWebExchange exchange,
57       UriComponentsBuilder uriComponentsBuilder)
58   {
59     final HttpStatus status = HttpStatus.BAD_REQUEST;
60     ProblemDetail problem = ProblemDetail.forStatus(status);
61
62     problem.setProperty("timestamp", new Date());
63
64     problem.setProperty("requestId", exchange.getRequest().getId());
65
66     problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/message-mutation").build().toUri());
67     StringBuilder stringBuilder = new StringBuilder();
68     stringBuilder.append(status.getReasonPhrase());
69     stringBuilder.append(" - ");
70     stringBuilder.append(e.getMessage());
71     problem.setTitle(stringBuilder.toString());
72
73     stringBuilder.setLength(0);
74     stringBuilder.append("The existing message with user=");
75     stringBuilder.append(e.getExisting().getUsername());
76     stringBuilder.append(" and id=");
77     stringBuilder.append(e.getExisting().getId());
78     stringBuilder.append(" cannot be mutated!");
79     problem.setDetail(stringBuilder.toString());
80
81     problem.setProperty("existingMessage", MessageTo.from(e.getExisting()));
82
83     problem.setProperty("mutatedText", e.getMutatedText());
84
85     return problem;
86   }
87
88   @ExceptionHandler(InvalidUsernameException.class)
89   public final ProblemDetail handleException(
90       InvalidUsernameException e,
91       ServerWebExchange exchange,
92       UriComponentsBuilder uriComponentsBuilder)
93   {
94     final HttpStatus status = HttpStatus.BAD_REQUEST;
95     ProblemDetail problem = ProblemDetail.forStatus(status);
96
97     problem.setProperty("timestamp", new Date());
98
99     problem.setProperty("requestId", exchange.getRequest().getId());
100
101     problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/invalid-username").build().toUri());
102     StringBuilder stringBuilder = new StringBuilder();
103     stringBuilder.append(status.getReasonPhrase());
104     stringBuilder.append(" - ");
105     stringBuilder.append(e.getMessage());
106     problem.setTitle(stringBuilder.toString());
107
108     stringBuilder.setLength(0);
109     stringBuilder.append("Invalid username: ");
110     stringBuilder.append(e.getUsername());
111     stringBuilder.append(
112         "! A valid username must consist of at at least two letters and " +
113         "must only contain lower case letters a-z, numbers and dashes");
114     problem.setDetail(stringBuilder.toString());
115
116     problem.setProperty("username", e.getUsername());
117
118     return problem;
119   }
120 }