public class JsonEventMatcher extends Object
EventInstance
s from Json webhook calls.
This class is configured through the addMatchableEvent(HeaderValue, FieldValue, String)
method, that
allows to specify an EventInstance
type for a given header and field content. Matched requests are reified
into EventInstance
s containing all the fields of the provided payload.
Modifier and Type | Class and Description |
---|---|
static class |
JsonEventMatcher.FieldValue
A pair representing a field value to match.
|
static class |
JsonEventMatcher.HeaderValue
A pair representing a
Header value to match. |
Modifier and Type | Field and Description |
---|---|
protected EventInstanceBuilder |
eventInstanceBuilder
The
EventInstanceBuilder used to reify the received payloads into EventInstance s. |
protected Map<JsonEventMatcher.HeaderValue,Map<JsonEventMatcher.FieldValue,String>> |
matchableEvents
The internal
Map used to store the header and field values to match. |
static String |
PRINT_BUILDER_CONTENT_KEY
The
Configuration key to store whether to print the EventInstanceBuilder content before
building the matched EventInstance . |
protected boolean |
printBuilder
A flag telling whether to print the
EventInstanceBuilder content before building the matched
EventInstance . |
Constructor and Description |
---|
JsonEventMatcher(EventInstanceBuilder eventInstanceBuilder,
Configuration configuration)
Constructs a new
JsonEventMatcher with the provided EventInstanceBuilder . |
Modifier and Type | Method and Description |
---|---|
void |
addMatchableEvent(JsonEventMatcher.HeaderValue headerValue,
JsonEventMatcher.FieldValue fieldValue,
String eventTypeName)
Registers a new
EventInstance to match from the provided headerValue and fieldValue . |
protected void |
convertJsonObjectToOutContext(com.google.gson.JsonObject jsonObject,
EventInstanceBuilder builder)
Converts the provided
jsonObject 's fields into out context values set in the provided builder . |
protected void |
convertJsonObjectToOutContext(String parentKey,
com.google.gson.JsonObject jsonObject,
EventInstanceBuilder builder)
Converts the provided
jsonObject 's field into out context values set in the provided builder . |
protected com.xatkit.intent.EventInstance |
createEventInstance(String eventDefinitionName,
com.google.gson.JsonElement content)
Creates an
EventInstance from the provided eventDefinitionName and content . |
com.xatkit.intent.EventInstance |
match(List<org.apache.http.Header> headers,
com.google.gson.JsonElement content)
Matches the provided
headers and content against the registered EventInstance s. |
public static final String PRINT_BUILDER_CONTENT_KEY
Configuration
key to store whether to print the EventInstanceBuilder
content before
building the matched EventInstance
.
This value is set to false
if not specified.
protected Map<JsonEventMatcher.HeaderValue,Map<JsonEventMatcher.FieldValue,String>> matchableEvents
Map
used to store the header and field values to match.protected EventInstanceBuilder eventInstanceBuilder
EventInstanceBuilder
used to reify the received payloads into EventInstance
s.protected boolean printBuilder
EventInstanceBuilder
content before building the matched
EventInstance
.public JsonEventMatcher(EventInstanceBuilder eventInstanceBuilder, Configuration configuration)
JsonEventMatcher
with the provided EventInstanceBuilder
.eventInstanceBuilder
- the EventInstanceBuilder
used to reify the received payloads into
EventInstance
s.configuration
- the Configuration
used to customize the JsonEventMatcher
NullPointerException
- if the provided eventInstanceBuilder
is null
public void addMatchableEvent(JsonEventMatcher.HeaderValue headerValue, JsonEventMatcher.FieldValue fieldValue, String eventTypeName)
EventInstance
to match from the provided headerValue
and fieldValue
.headerValue
- the JsonEventMatcher.HeaderValue
to match the received request againstfieldValue
- the JsonEventMatcher.FieldValue
to match the received payload againsteventTypeName
- the type of the EventInstance
to returnNullPointerException
- if the provided headerValue
, fieldValue
, or eventTypeName
is null
XatkitException
- if the provided headerValue
and fieldValue
are already associated to
another eventTypeName
, or if calling this method would associate the
JsonEventMatcher.FieldValue.EMPTY_FIELD_VALUE
and specialized JsonEventMatcher.FieldValue
s to the
provided headerValue
.JsonEventMatcher.HeaderValue
,
JsonEventMatcher.FieldValue
,
JsonEventMatcher.FieldValue.EMPTY_FIELD_VALUE
public com.xatkit.intent.EventInstance match(List<org.apache.http.Header> headers, com.google.gson.JsonElement content)
headers
and content
against the registered EventInstance
s.
This method first iterates the provided headers
and look for each registered one if the provided
content
contains a registered field. If at least one header
and one field of the content
are matched the corresponding EventInstance
is returned.
Note:
the current implementation only check top-level field from the providedcontent
. (see #139)headers
- the list containing the Header
s to matchcontent
- the JsonElement
representing the content of the requestEventInstance
if it exists, null
otherwiseNullPointerException
- if the provided headers
or content
is null
protected com.xatkit.intent.EventInstance createEventInstance(String eventDefinitionName, com.google.gson.JsonElement content)
EventInstance
from the provided eventDefinitionName
and content
.
The built EventInstance
is associated to the EventDefinition
matching the given eventDefinitionName
if it exists, and its out contexts are set with the fields of the provided content
JsonElement
.
Note that the built EventInstance
contains all the fields containing literal values of the provided
content
, formatted by the convertJsonObjectToOutContext(JsonObject, EventInstanceBuilder)
method.
eventDefinitionName
- the name of the EventDefinition
to create an
EventInstance
fromcontent
- the JsonElement
to set as the created EventInstance
out contextEventInstance
matching the provided eventDefinitionName
and containing the content
out context values.NullPointerException
- if the provided eventDefinitionName
or content
is null
convertJsonObjectToOutContext(JsonObject, EventInstanceBuilder)
protected void convertJsonObjectToOutContext(com.google.gson.JsonObject jsonObject, EventInstanceBuilder builder)
jsonObject
's fields into out context values set in the provided builder
.
This method formats the jsonObject
fields following this pattern:
"content"
)'_'
, followed by their containing field name (e.g. "content_field1")
EventInstance
's out context
This method is a convenience wrapper for
convertJsonObjectToOutContext(String, JsonObject, EventInstanceBuilder)
with
the empty string as its first parameter.
Note: this method does not handle JsonArray
s for now (see #141)
jsonObject
- the JsonObject
to extract the fields frombuilder
- the EventInstanceBuilder
used to set out context valuesNullPointerException
- if the provided jsonObject
or builder
is null
convertJsonObjectToOutContext(String, JsonObject, EventInstanceBuilder)
protected void convertJsonObjectToOutContext(String parentKey, com.google.gson.JsonObject jsonObject, EventInstanceBuilder builder)
jsonObject
's field into out context values set in the provided builder
.
This method formats the jsonObject
fields following this pattern:
"content"
)'_'
, followed by their containing field name (e.g.
"content_field1")
EventInstance
's out context
Note: this method does not handle JsonArray
s for now (see #141)
parentKey
- the key of the parent of the provided jsonObject
jsonObject
- the JsonObject
to extract the fields frombuilder
- the EventInstanceBuilder
used to set out context valuesNullPointerException
- if the provided parentKey
, jsonObject
, or builder
is null
Copyright © 2020 SOM Research Lab. All rights reserved.