/
DittoHeadersTooLargeException.java
118 lines (101 loc) · 4.62 KB
/
DittoHeadersTooLargeException.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
/*
* Copyright (c) 2019 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.model.base.exceptions;
import static org.eclipse.ditto.model.base.common.HttpStatusCode.REQUEST_HEADER_FIELDS_TOO_LARGE;
import java.net.URI;
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.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonParsableException;
/**
* Thrown when Ditto headers are too large to communicate within the cluster.
*/
@Immutable
@JsonParsableException(errorCode = DittoHeadersTooLargeException.ERROR_CODE)
public final class DittoHeadersTooLargeException extends DittoRuntimeException {
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = "headers.too.large";
private static final String DEFAULT_MESSAGE = "The headers are too large.";
private DittoHeadersTooLargeException(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
super(ERROR_CODE, REQUEST_HEADER_FIELDS_TOO_LARGE, dittoHeaders, message, description, cause, href);
}
/**
* A mutable builder for a {@code DittoHeadersTooLargeException} due to size limitation.
*
* @param maxSize maximum size of the headers.
* @return the builder.
*/
public static DittoRuntimeExceptionBuilder<DittoHeadersTooLargeException> newSizeLimitBuilder(final int maxSize) {
final String description =
String.format("The number of bytes exceeded the maximum allowed value %d.", maxSize);
return new Builder().description(description);
}
/**
* A mutable builder for a {@code DittoHeadersTooLargeException} due to auth subjects limitation.
*
* @param actualAuthSubjectCount actual number of authorization subjects.
* @param maxAuthSubjectsCount maximum number of authorization subjects.
* @return the builder.
*/
public static DittoRuntimeExceptionBuilder<DittoHeadersTooLargeException> newAuthSubjectsLimitBuilder(
final int actualAuthSubjectCount, final int maxAuthSubjectsCount) {
final String description =
String.format("The number of authorization subjects %d exceeded the maximum allowed value %d.",
actualAuthSubjectCount, maxAuthSubjectsCount);
return new Builder().description(description);
}
/**
* Constructs a new {@code DittoHeadersTooLargeException} 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 DittoHeadersTooLargeException.
* @throws NullPointerException if any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link
* JsonFields#MESSAGE} field.
*/
public static DittoHeadersTooLargeException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new Builder()
.dittoHeaders(dittoHeaders)
.message(readMessage(jsonObject))
.description(readDescription(jsonObject).orElse(null))
.href(readHRef(jsonObject).orElse(null))
.build();
}
/**
* A mutable builder with a fluent API for a {@link DittoHeadersTooLargeException}.
*/
@NotThreadSafe
private static final class Builder extends DittoRuntimeExceptionBuilder<DittoHeadersTooLargeException> {
private Builder() {
message(DEFAULT_MESSAGE);
}
@Override
protected DittoHeadersTooLargeException doBuild(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
return new DittoHeadersTooLargeException(dittoHeaders, message, description, cause, href);
}
}
}