-
Notifications
You must be signed in to change notification settings - Fork 214
/
JsonFieldSelectorValidator.java
66 lines (57 loc) · 2.43 KB
/
JsonFieldSelectorValidator.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
/*
* 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.base.model.headers;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelectorInvalidException;
import org.eclipse.ditto.json.JsonParseOptions;
import org.eclipse.ditto.json.JsonRuntimeException;
/**
* This validator parses a CharSequence to a {@link org.eclipse.ditto.json.JsonFieldSelector}.
* If parsing fails, a {@link org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException} with detailed description is thrown.
*
* @since 3.0.0
*/
@Immutable
final class JsonFieldSelectorValidator extends AbstractHeaderValueValidator {
/**
* Don't configure URL decoding as JsonParseOptions because Pekko-Http already decodes the fields-param and we would
* decode twice.
*/
private static final JsonParseOptions JSON_FIELD_SELECTOR_PARSE_OPTIONS = JsonFactory.newParseOptionsBuilder()
.withoutUrlDecoding()
.build();
private JsonFieldSelectorValidator() {
super(String.class::equals);
}
static JsonFieldSelectorValidator getInstance() {
return new JsonFieldSelectorValidator();
}
@Override
protected void validateValue(final HeaderDefinition definition, final CharSequence value) {
try {
JsonFactory.newFieldSelector(value.toString(), JSON_FIELD_SELECTOR_PARSE_OPTIONS);
} catch (final JsonFieldSelectorInvalidException e) {
throw DittoHeaderInvalidException.newInvalidTypeBuilder(definition, value,
"field selector")
.description(getDescription(e))
.cause(e)
.build();
}
}
private static String getDescription(final JsonRuntimeException jsonRuntimeException) {
final String message = jsonRuntimeException.getMessage();
return jsonRuntimeException.getDescription().map(description -> message + " " + description).orElse(message);
}
}