-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageCommandSizeValidator.java
55 lines (46 loc) · 1.97 KB
/
MessageCommandSizeValidator.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
/*
* Copyright (c) 2017 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.messages.model.signals.commands;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandSizeValidator;
import org.eclipse.ditto.messages.model.MessagePayloadSizeTooLargeException;
/**
* Command size validator for message commands.
*/
public final class MessageCommandSizeValidator
extends AbstractCommandSizeValidator<MessagePayloadSizeTooLargeException> {
@Nullable private static MessageCommandSizeValidator instance;
private MessageCommandSizeValidator(@Nullable final Long maxSize) {
super(maxSize);
}
@Override
protected MessagePayloadSizeTooLargeException newInvalidSizeException(final long maxSize, final long actualSize,
final DittoHeaders headers) {
return MessagePayloadSizeTooLargeException.newBuilder(actualSize, maxSize).dittoHeaders(headers).build();
}
/**
* The singleton instance for this command size validator. Will be initialized at first call.
*
* @return Singleton instance
*/
public static MessageCommandSizeValidator getInstance() {
if (null == instance) {
final long maxSize =
Long.parseLong(System.getProperty(DittoSystemProperties.DITTO_LIMITS_MESSAGES_MAX_SIZE_BYTES, DEFAULT_LIMIT));
instance = (maxSize > 0) ? new MessageCommandSizeValidator(maxSize) : new MessageCommandSizeValidator(null);
}
return instance;
}
}