-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractEnforcementReloaded.java
115 lines (101 loc) · 5.13 KB
/
AbstractEnforcementReloaded.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
/*
* Copyright (c) 2022 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.policies.enforcement;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
/**
* Abstract implementation of {@link EnforcementReloaded} providing common functionality of all entity specific
* enforcement implementations.
*
* @param <S> the type of the Signal to enforce/authorize.
* @param <R> the type of the CommandResponse to filter.
*/
public abstract class AbstractEnforcementReloaded<S extends Signal<?>, R extends CommandResponse<?>>
implements EnforcementReloaded<S, R> {
protected static final ThreadSafeDittoLogger LOGGER =
DittoLoggerFactory.getThreadSafeLogger(AbstractEnforcementReloaded.class);
/**
* Reports an error differently based on type of the error. If the error is of type
* {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException}, it is returned as is
* (without modification), otherwise it is wrapped inside a {@link DittoInternalErrorException}.
*
* @param hint hint about the nature of the error.
* @param throwable the error.
* @param dittoHeaders the DittoHeaders to use for the DittoRuntimeException.
* @return DittoRuntimeException suitable for transmission of the error.
*/
public static DittoRuntimeException reportError(final String hint, @Nullable final Throwable throwable,
final DittoHeaders dittoHeaders) {
final Throwable error = throwable == null
? new NullPointerException("Result and error are both null")
: throwable;
final var dre = DittoRuntimeException.asDittoRuntimeException(
error, cause -> reportUnexpectedError(hint, cause, dittoHeaders));
LOGGER.withCorrelationId(dittoHeaders)
.info("{} - {}: {}", hint, dre.getClass().getSimpleName(), dre.getMessage());
return dre;
}
/**
* Reports an error or a response based on type of the error and whether a response was present or not.
* If the error is of type {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException}, it is returned as
* is (without modification), otherwise it is wrapped inside a {@link DittoInternalErrorException}.
*
* @param hint hint about the nature of the error.
* @param response the (optional) response.
* @param error the (optional) error.
* @param dittoHeaders the DittoHeaders to use for the DittoRuntimeException.
* @return DittoRuntimeException suitable for transmission of the error.
*/
public static DittoRuntimeException reportErrorOrResponse(final String hint, @Nullable final Object response,
@Nullable final Throwable error, final DittoHeaders dittoHeaders) {
if (error != null) {
return reportError(hint, error, dittoHeaders);
} else if (response instanceof Throwable throwable) {
return reportError(hint, throwable, dittoHeaders);
} else if (response != null) {
return reportUnknownResponse(hint, response, dittoHeaders);
} else {
return reportError(hint, new NullPointerException("Response and error were null."), dittoHeaders);
}
}
/**
* Reports an unknown response as a DittoInternalErrorException.
*
* @param hint hint about the nature of the error.
* @param response the unknown response.
* @param dittoHeaders the DittoHeaders to use for the DittoRuntimeException.
* @return DittoInternalErrorException
*/
protected static DittoRuntimeException reportUnknownResponse(final String hint, final Object response,
final DittoHeaders dittoHeaders) {
LOGGER.withCorrelationId(dittoHeaders)
.error("Unexpected response {}: <{}>", hint, response);
return DittoInternalErrorException.newBuilder().dittoHeaders(dittoHeaders).build();
}
private static DittoRuntimeException reportUnexpectedError(final String hint, final Throwable error,
final DittoHeaders dittoHeaders) {
LOGGER.withCorrelationId(dittoHeaders)
.error("Unexpected error {} - {}: {}", hint, error.getClass().getSimpleName(),
error.getMessage(), error);
return DittoInternalErrorException.newBuilder()
.cause(error)
.dittoHeaders(dittoHeaders)
.build();
}
}