/
PolicyTooLargeException.java
executable file
·99 lines (85 loc) · 3.86 KB
/
PolicyTooLargeException.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.policies;
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.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
/**
* This exception indicates the content of a Policy is too large to be processed by a backend.
*/
@Immutable
public final class PolicyTooLargeException extends DittoRuntimeException implements PolicyException {
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = PolicyException.ERROR_CODE_PREFIX + "policy.toolarge";
private static final String MESSAGE_TEMPLATE =
"The size of ''{0}'' kB exceeds the maximal allowed Policy size of ''{1}'' kB.";
private static final String DEFAULT_DESCRIPTION = "Reduce the Policy size in the bounds of the specified limit";
private static final long serialVersionUID = 2434234324327234489L;
private PolicyTooLargeException(final DittoHeaders dittoHeaders, @Nullable final String message,
@Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) {
super(ERROR_CODE, HttpStatusCode.REQUEST_ENTITY_TOO_LARGE, dittoHeaders, message, description, cause, href);
}
/**
* A mutable builder for a {@code PolicyTooLargeException}.
*
* @param actualBytes the actual amount of bytes which were too much (gt {@code maxBytes})
* @param maxBytes the maximal allowed amount of bytes
* @return the builder.
*/
public static Builder newBuilder(final long actualBytes, final long maxBytes) {
return new Builder(actualBytes, maxBytes);
}
/**
* Deserialize from a JSON object.
*
* @param jsonObject the JSON object to deserialize.
* @param dittoHeaders the headers.
* @return an instance of this class.
* @throws NullPointerException if any argument is {@code null}.
*/
public static PolicyTooLargeException fromJson(final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {
return new Builder()
.loadJson(jsonObject)
.dittoHeaders(dittoHeaders)
.message(readMessage(jsonObject))
.description(readDescription(jsonObject).orElse(DEFAULT_DESCRIPTION))
.href(readHRef(jsonObject).orElse(null))
.build();
}
/**
* A mutable builder with a fluent API for a {@link PolicyTooLargeException}.
*/
@NotThreadSafe
public static final class Builder extends DittoRuntimeExceptionBuilder<PolicyTooLargeException> {
private Builder() {
description(DEFAULT_DESCRIPTION);
}
private Builder(final long actualBytes, final long maxBytes) {
this();
message(MessageFormat.format(MESSAGE_TEMPLATE, actualBytes / 1024.0, maxBytes / 1024.0));
}
@Override
protected PolicyTooLargeException doBuild(final DittoHeaders dittoHeaders, @Nullable final String message,
@Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) {
return new PolicyTooLargeException(dittoHeaders, message, description, cause, href);
}
}
}