1
#include "source/extensions/content_parsers/json/config.h"
2

            
3
#include "envoy/registry/registry.h"
4

            
5
#include "source/extensions/content_parsers/json/json_content_parser_impl.h"
6

            
7
namespace Envoy {
8
namespace Extensions {
9
namespace ContentParsers {
10
namespace Json {
11

            
12
ContentParser::ParserFactoryPtr JsonContentParserConfigFactory::createParserFactory(
13
113
    const Protobuf::Message& config, Server::Configuration::ServerFactoryContext& context) {
14
113
  const auto& json_config = MessageUtil::downcastAndValidate<
15
113
      const envoy::extensions::content_parsers::json::v3::JsonContentParser&>(
16
113
      config, context.messageValidationVisitor());
17

            
18
158
  for (const auto& rule_config : json_config.rules()) {
19
156
    const auto& rule = rule_config.rule();
20
156
    if (rule.has_on_present()) {
21
148
      const auto& kv_pair = rule.on_present();
22
148
      if (kv_pair.value_type_case() == envoy::extensions::filters::http::json_to_metadata::v3::
23
148
                                           JsonToMetadata::KeyValuePair::kValue) {
24
1
        if (kv_pair.value().kind_case() == 0) {
25
1
          throw EnvoyException("on_present KeyValuePair with explicit value must have value set");
26
1
        }
27
1
      }
28
148
    }
29
155
    if (rule.has_on_missing()) {
30
4
      const auto& kv_pair = rule.on_missing();
31
4
      if (kv_pair.value_type_case() == envoy::extensions::filters::http::json_to_metadata::v3::
32
4
                                           JsonToMetadata::KeyValuePair::kValue) {
33
3
        if (kv_pair.value().kind_case() == 0) {
34
1
          throw EnvoyException("on_missing KeyValuePair with explicit value must have value set");
35
1
        }
36
3
      }
37
4
    }
38
154
    if (rule.has_on_error()) {
39
7
      const auto& kv_pair = rule.on_error();
40
7
      if (kv_pair.value_type_case() == envoy::extensions::filters::http::json_to_metadata::v3::
41
7
                                           JsonToMetadata::KeyValuePair::kValue) {
42
7
        if (kv_pair.value().kind_case() == 0) {
43
1
          throw EnvoyException("on_error KeyValuePair with explicit value must have value set");
44
1
        }
45
7
      }
46
7
    }
47

            
48
    // Require at least one of on_present, on_missing, or on_error to be set
49
153
    if (!rule.has_on_present() && !rule.has_on_missing() && !rule.has_on_error()) {
50
2
      throw EnvoyException("At least one of on_present, on_missing, or on_error must be specified");
51
2
    }
52
153
  }
53

            
54
108
  return std::make_unique<JsonContentParserFactory>(json_config);
55
113
}
56

            
57
/**
58
 * Static registration for the JSON content parser. @see RegisterFactory.
59
 */
60
REGISTER_FACTORY(JsonContentParserConfigFactory, ContentParser::NamedContentParserConfigFactory);
61

            
62
} // namespace Json
63
} // namespace ContentParsers
64
} // namespace Extensions
65
} // namespace Envoy