--- /dev/null
+package dk.cbs.soa.soasi.secret.vault;
+
+public class ProjectHandleException extends RuntimeException {
+
+ private static final long serialVersionUID = -7888921768802904362L;
+
+ public ProjectHandleException(String message) {
+ super(message);
+ }
+
+ public ProjectHandleException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
--- /dev/null
+package dk.cbs.soa.soasi.secret.vault;
+
+public class ProjectNotFoundException extends RuntimeException {
+
+ private static final long serialVersionUID = -1899398910147898420L;
+
+ public ProjectNotFoundException(String message) {
+ super(message);
+ }
+
+ public ProjectNotFoundException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
--- /dev/null
+package dk.cbs.soa.soasi.secret.vault.controller;
+
+public enum ErrorCode {
+ ObjectNotFound,
+ NotNull,
+ InvalidArgument,
+ InvalidState,
+ TypeMismatch,
+ MissingId,
+ DataIntegrityViolationException,
+ InvalidFileFormat,
+ InvalidError,
+ ObjectCannotBeSaved,
+ InsufficientPermissions,
+ SystemError,
+ UniquenessViolationError,
+ InvalidDateRange
+}
--- /dev/null
+package dk.cbs.soa.soasi.secret.vault.controller;
+
+import lombok.Builder;
+import lombok.NonNull;
+import lombok.Value;
+
+@Builder
+@Value
+public class ErrorData {
+ @NonNull
+ private ErrorCode errorCode;
+ private String message;
+ private String objectName;
+ private String propertyName;
+
+}
--- /dev/null
+package dk.cbs.soa.soasi.secret.vault.controller;
+
+import lombok.AllArgsConstructor;
+import lombok.Value;
+
+import java.time.OffsetDateTime;
+import java.util.List;
+
+import static lombok.AccessLevel.PRIVATE;
+
+@Value
+@AllArgsConstructor(access = PRIVATE)
+public class ErrorPayload {
+ private Integer count;
+ private String type;
+ private String traceId;
+ private OffsetDateTime timestamp;
+ private String status;
+ private List<ErrorData> data;
+
+ public static ErrorPayload createErrorPayload(List<ErrorData> data, String traceId){
+ if(data==null || data.isEmpty()){
+ throw new IllegalArgumentException("At least one error object must be provided!");
+ }
+ return new ErrorPayload(data.size(),ErrorData.class.getSimpleName(), traceId, OffsetDateTime.now(), "error", data);
+ }
+}
--- /dev/null
+package dk.cbs.soa.soasi.secret.vault.controller;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+import java.util.random.RandomGenerator;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+import dk.cbs.soa.soasi.secret.vault.ProjectNotFoundException;
+import dk.cbs.soa.soasi.secret.vault.ProjectHandleException;
+
+import lombok.extern.slf4j.Slf4j;
+
+@ControllerAdvice
+@Slf4j
+public class GlobalExceptionHandler {
+
+ @ExceptionHandler(ProjectNotFoundException.class)
+ @ResponseStatus(HttpStatus.NOT_FOUND)
+ public ResponseEntity<ErrorPayload> handleProjectNotFoundException(Exception e) {
+ log.error(" handleException", e);
+ String currentTraceId = getTraceId();
+
+ List<ErrorData> errorDataList = List.of(ErrorData.builder().errorCode(ErrorCode.ObjectNotFound).message(e.getMessage()).build());
+
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorPayload.createErrorPayload(errorDataList, currentTraceId));
+ }
+
+ @ExceptionHandler(ProjectHandleException.class)
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ public ResponseEntity<ErrorPayload> handleProjectHandleException(Exception e) {
+ log.error(" handleException", e);
+ String currentTraceId = getTraceId();
+
+ List<ErrorData> errorDataList = List.of(ErrorData.builder().errorCode(ErrorCode.InvalidState).message(e.getMessage()).build());
+
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorPayload.createErrorPayload(errorDataList, currentTraceId));
+ }
+
+ @ExceptionHandler(Exception.class)
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+ public ResponseEntity<ErrorPayload> handleException(Exception e) {
+ log.error(" handleException", e);
+ String currentTraceId = getTraceId();
+
+ List<ErrorData> errorDataList = List.of(ErrorData.builder().errorCode(ErrorCode.SystemError).message(e.getMessage()).build());
+
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorPayload.createErrorPayload(errorDataList, currentTraceId));
+ }
+
+ private String getTraceId() {
+ RandomGenerator randomId = RandomGenerator.getDefault();
+
+ return String.valueOf(randomId.nextLong());
+ }
+}
}
Optional<Project> p = projectRepository.get(project.getToken());
if (p.isEmpty()) {
- throw new IllegalArgumentException("Invalid token" + project.getToken());
+ throw new IllegalArgumentException("Invalid token: " + project.getToken());
}
model.addAttribute("project", p.get());
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
+import dk.cbs.soa.soasi.secret.vault.ProjectHandleException;
+import dk.cbs.soa.soasi.secret.vault.ProjectNotFoundException;
import dk.cbs.soa.soasi.secret.vault.model.Project;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("token", token);
+ Project p = null;
- Project p = jdbcNamedTemplate.queryForObject(sqlGet, namedParameters, new RowMapper<Project>() {
+ try {
+ p = jdbcNamedTemplate.queryForObject(sqlGet, namedParameters, new RowMapper<Project>() {
+
+ @Override
+ public Project mapRow(ResultSet rs, int rowNum) throws SQLException {
+ return Project.builder()
+ .id(rs.getString("id"))
+ .secret(rs.getString("secret"))
+ .token(rs.getString("token"))
+ .build();
+ }
+
+ });
- @Override
- public Project mapRow(ResultSet rs, int rowNum) throws SQLException {
- return Project.builder()
- .id(rs.getString("id"))
- .secret(rs.getString("secret"))
- .token(rs.getString("token"))
- .build();
- }
-
- });
-
- log.info("Add: " + p.toString());
+ log.info("Add: " + p.toString());
- if (p != null) {
- jdbcTemplate.update(sqlDelete, token);
+ if (p != null) {
+ jdbcTemplate.update(sqlDelete, token);
+ }
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ throw new ProjectNotFoundException(e.getMessage(), e);
}
return Optional.ofNullable(p);
namedParameters.addValue("token", project.getToken());
namedParameters.addValue("id", project.getId());
- jdbcNamedTemplate.update(sqlInsert, namedParameters);
+ try {
+ jdbcNamedTemplate.update(sqlInsert, namedParameters);
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ throw new ProjectHandleException(e.getMessage(), e);
+ }
return Optional.ofNullable(project);
}
namedParameters.addValue("token", project.getToken());
namedParameters.addValue("secret", project.getSecret());
- jdbcNamedTemplate.update(sqlUpdate, namedParameters);
+ try {
+ jdbcNamedTemplate.update(sqlUpdate, namedParameters);
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ throw new ProjectHandleException(e.getMessage(), e);
+ }
}
private UUID getRandomUuid() {
<head>
<meta charset="UTF-8">
<title>GoodBye</title>
+<link rel="stylesheet" href="/style.css">
</head>
<body>
+<div class="info">
<h1>GoodBye</h1>
+</div>
</body>
</html>
\ No newline at end of file
--- /dev/null
+body {
+ background-color: #4967aa;
+}
+
+.parent{
+ position: relative;
+}
+
+.label-bg {
+ background-color: #b1ff96;
+}
+
+th {
+ background-color: #b1ff96;
+}
+
+td {
+ background-color: #b1ff96;
+}
+
+.form, .info {
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+}
+
+.value {
+ width: 400px;
+}
+
+input[type="submit"] {
+ display: block;
+ width: 93%;
+ margin: 0 auto 1.25rem auto;
+ font-size: 1.1rem;
+ background-color: #e01b24;
+ border-color: #e01b24;
+ color: #ffffff;
+ border-radius: 3px;
+ font-family: Tahoma;
+ font-size: .9rem;
+ border: none;
+}
\ No newline at end of file
+<!doctype html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+<meta charset="UTF-8">
+<title>Add project</title>
+<link rel="stylesheet" href="/style.css">
+</head>
+<body>
+<div class="info">
<form action="#" th:action="@{/addproject}" th:object="${project}" method="post">
- <label for="token">Token</label>
+ <label class="label-bg" for="token">Token</label>
<input type="text" th:field="*{token}" id="token" placeholder="Token">
<span th:if="${#fields.hasErrors('token')}" th:errors="*{token}"></span>
<br/><br/>
<input type="submit" value="Add Project">
-</form>
\ No newline at end of file
+</form>
+</div>
+</body>
\ No newline at end of file
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
-<meta charset="UTF-8">
-<title>Project Info</title>
+ <meta charset="UTF-8">
+ <title>Project Info</title>
+ <link rel="stylesheet" href="/style.css">
</head>
<body>
-<table>
-<thead>
-<tr>
-<th>Id</th>
-<th>Token</th>
-<th>Secret</th>
-</tr>
-</thead>
-<tbody>
-<tr>
-<td th:text="${project.id}"/>
-<td th:text="${project.token}"/>
-<td th:text="${project.secret}"/>
-</tr>
-</tbody>
-</table>
+ <table border="1" class="info">
+ <thead>
+ <tr>
+ <th>Key</th>
+ <th class="value">Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Id</td>
+ <td class="value" th:text="${project.id}"></td>
+ </tr>
+ <tr>
+ <td>Token</td>
+ <td class="value" th:text="${project.token}"></td>
+ </tr>
+ <tr>
+ <td>Secret</td>
+ <td class="value" th:text="${project.secret}"></td>
+ </tr>
+ </tbody>
+ </table>
</body>
</html>
+<!doctype html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+<meta charset="UTF-8">
+<title>Create project</title>
+<link rel="stylesheet" href="/style.css">
+</head>
+<body>
+<div class="form">
<form action="#" th:action="@{/showinfo}" th:object="${project}" method="post">
- <label for="id">Id/Name</label>
+ <label class="label-bg" for="id">Id/Name</label>
<input type="text" th:field="*{id}" id="id" placeholder="Id">
<span th:if="${#fields.hasErrors('id')}" th:errors="*{id}"></span>
<br/><br/>
<input type="submit" value="Create Project">
-</form>
\ No newline at end of file
+</form>
+</div>
+</body>
\ No newline at end of file
+<!doctype html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+<meta charset="UTF-8">
+<title>Send secret</title>
+<link rel="stylesheet" href="/style.css">
+</head>
+<body>
+<div class="form">
<form action="#" th:action="@{/secret}" th:object="${project}" method="post">
- <label for="token">Token</label>
+ <label class="label-bg" for="token">Token</label>
<input type="text" th:field="*{token}" id="token" placeholder="Token">
<span th:if="${#fields.hasErrors('token')}" th:errors="*{token}"></span>
<br/>
- <label for="secret">Secret</label>
+ <label class="label-bg" for="secret">Secret</label>
<input type="text" th:field="*{secret}" id="secret" placeholder="Secret">
<span th:if="${#fields.hasErrors('secret')}" th:errors="*{secret}"></span>
<br/><br/>
- <input type="submit" value="Send Secret">
-</form>
\ No newline at end of file
+ <input type="submit" value="Send Secret">
+</form>
+</div>
+</body>
\ No newline at end of file
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
-<meta charset="UTF-8">
-<title>Project Info</title>
+ <meta charset="UTF-8">
+ <title>Project Info</title>
+ <link rel="stylesheet" href="/style.css">
</head>
<body>
-<table>
-<thead>
-<tr>
-<th>Id</th>
-<th>Token</th>
-</tr>
-</thead>
-<tbody>
-<tr>
-<td th:text="${project.id}"/>
-<td th:text="${project.token}"/>
-</tr>
-</tbody>
-</table>
+ <table border="1" class="info">
+ <thead>
+ <tr>
+ <th class="label-bg">Key</th>
+ <th class="value">Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Id</td>
+ <td class="value" th:text="${project.id}" />
+ </tr>
+ <tr>
+ <td>Token</td>
+ <td class="value" th:text="${project.token}" />
+ </tr>
+ </tbody>
+ </table>
</body>
</html>