/
ConnectionUnauthorizedException.java
123 lines (107 loc) · 5.07 KB
/
ConnectionUnauthorizedException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.signals.commands.connectivity.exceptions;
import java.net.URI;
import java.text.MessageFormat;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.common.HttpStatus;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonParsableException;
import org.eclipse.ditto.model.connectivity.ConnectionId;
import org.eclipse.ditto.model.connectivity.ConnectivityException;
/**
* Thrown for authorization errors on Connections.
*/
@Immutable
@JsonParsableException(errorCode = ConnectionUnauthorizedException.ERROR_CODE)
public final class ConnectionUnauthorizedException extends DittoRuntimeException implements ConnectivityException {
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = ERROR_CODE_PREFIX + "connection.unauthorized";
private static final String MESSAGE_TEMPLATE =
"The Connection with ID <{0}> could not authenticate successfully. " +
"The underlying driver reported: <{1}>";
private static final String DEFAULT_DESCRIPTION =
"Please verify that the credentials were configured correctly in the connection.";
private static final long serialVersionUID = -4525302146760945435L;
private ConnectionUnauthorizedException(
final DittoHeaders dittoHeaders,
@Nullable final String message, @Nullable final String description,
@Nullable final Throwable cause, @Nullable final URI href) {
super(ERROR_CODE, HttpStatus.UNAUTHORIZED, dittoHeaders, message, description, cause, href);
}
/**
* Constructs a new {@code ConnectionUnauthorizedException}.
*
* @param connectionId the ConnectionId for which no authorization failed.
* @param reason a detail reason why the authorization failed.
* @return the new ConnectionUnauthorizedException.
*/
public static ConnectionUnauthorizedException forConnectionId(final ConnectionId connectionId,
final String reason) {
return new ConnectionUnauthorizedException.Builder(connectionId, reason).build();
}
/**
* Constructs a new {@code ConnectionUnauthorizedException} object with the exception message extracted from the
* given JSON object.
*
* @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from.
* @param dittoHeaders the headers of the command which resulted in this exception.
* @return the new ConnectionUnauthorizedException.
* @throws NullPointerException if any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if this JsonObject did not contain an error message.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
public static ConnectionUnauthorizedException fromJson(final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {
return DittoRuntimeException.fromJson(jsonObject, dittoHeaders, new ConnectionUnauthorizedException.Builder());
}
@Override
public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
return new ConnectionUnauthorizedException.Builder()
.message(getMessage())
.description(getDescription().orElse(null))
.cause(getCause())
.href(getHref().orElse(null))
.dittoHeaders(dittoHeaders)
.build();
}
/**
* A mutable builder with a fluent API for a {@link ConnectionUnauthorizedException}.
*/
@NotThreadSafe
static final class Builder extends DittoRuntimeExceptionBuilder<ConnectionUnauthorizedException> {
private Builder() {
description(DEFAULT_DESCRIPTION);
}
private Builder(final ConnectionId connectionId, final String reason) {
this();
message(MessageFormat.format(MESSAGE_TEMPLATE, String.valueOf(connectionId), reason));
}
@Override
protected ConnectionUnauthorizedException doBuild(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
return new ConnectionUnauthorizedException(dittoHeaders, message, description, cause, href);
}
}
}