forked from logstash-plugins/logstash-input-http
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON Array splitting #77
Open
51-code
wants to merge
15
commits into
teragrep:main
Choose a base branch
from
51-code:json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
94f44ca
Refactor regex splitting to a decorator
51-code 94f2db8
Change config to allow splitting with json_array
51-code ffd5cd9
Move regex splitting tests to their own file, refactor the tests
51-code ea2e227
Apply spotless
51-code 15b0a94
Remove duplicate tests from RegexSplittingTest
51-code e6c5e74
Refactor Payload objects and regex splitting
51-code 5e73ff6
Implement json_array splitting
51-code d89c2ea
Rename Payload's take() function to message()
51-code 70377c7
Refactor JsonPayload and RegexPayload to encapsulate Payload instead …
51-code 7982199
Refactor split() function in Payload
51-code cd97a6a
Move config tests from RegexPayloadTest to PayloadConfigTest and add …
51-code d1522e4
Move tests to the correct package
51-code 63caec3
Add object equality test for RegexPayload
51-code c07ac2e
Use Collections.singletonList in DefaultPayload, RegexConversion as f…
51-code e15d3a4
Add secondary ctor to RegexConversion
51-code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,33 +24,43 @@ | |
|
||
public class PayloadConfig implements Validateable { | ||
|
||
public final String splitRegex; | ||
public final boolean splitEnabled; | ||
public final String regexPattern; | ||
public final String splitType; | ||
|
||
public PayloadConfig() { | ||
PropertiesReaderUtilityClass propertiesReader = new PropertiesReaderUtilityClass( | ||
System.getProperty("properties.file", "etc/config.properties") | ||
); | ||
splitRegex = propertiesReader.getStringProperty("payload.splitRegex"); | ||
splitEnabled = propertiesReader.getBooleanProperty("payload.splitEnabled"); | ||
this.regexPattern = propertiesReader.getStringProperty("payload.splitType.regex.pattern"); | ||
this.splitType = propertiesReader.getStringProperty("payload.splitType"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in constructor code is not ideal |
||
} | ||
|
||
@Override | ||
public void validate() { | ||
if (splitEnabled) { | ||
try { | ||
Pattern.compile(splitRegex); | ||
} | ||
catch (PatternSyntaxException e) { | ||
switch (splitType) { | ||
case "regex": | ||
try { | ||
Pattern.compile(regexPattern); | ||
} | ||
catch (PatternSyntaxException e) { | ||
throw new IllegalArgumentException( | ||
"Configuration has an invalid regex (payload.splitType.regex.pattern): " + regexPattern | ||
); | ||
} | ||
break; | ||
case "json_array": | ||
case "none": | ||
break; | ||
default: | ||
throw new IllegalArgumentException( | ||
"Configuration has an invalid regex (payload.splitRegex): " + splitRegex | ||
"Configuration has an invalid splitType: " + splitType | ||
+ ". Has to be 'regex', 'json_array' or 'none'." | ||
); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PayloadConfig{" + "splitRegex=" + splitRegex + ", splitEnabled=" + splitEnabled + '}'; | ||
return "PayloadConfig{" + "splitType=" + splitType + ", splitType.regex.pattern=" + regexPattern + '}'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/teragrep/lsh_01/conversion/DefaultPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
logstash-http-input to syslog bridge | ||
Copyright 2024 Suomen Kanuuna Oy | ||
|
||
Derivative Work of Elasticsearch | ||
Copyright 2012-2015 Elasticsearch | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.teragrep.lsh_01.conversion; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public final class DefaultPayload implements Payload { | ||
|
||
private final String message; | ||
|
||
public DefaultPayload(String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public List<String> messages() { | ||
return Collections.singletonList(message); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object object) { | ||
if (this == object) | ||
return true; | ||
if (object == null) | ||
return false; | ||
if (object.getClass() != this.getClass()) | ||
return false; | ||
final DefaultPayload cast = (DefaultPayload) object; | ||
return message.equals(cast.message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/teragrep/lsh_01/conversion/JsonConversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
logstash-http-input to syslog bridge | ||
Copyright 2024 Suomen Kanuuna Oy | ||
|
||
Derivative Work of Elasticsearch | ||
Copyright 2012-2015 Elasticsearch | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.teragrep.lsh_01.conversion; | ||
|
||
import com.teragrep.lsh_01.authentication.Subject; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Decorator for IMessageHandler that splits messages arriving as an array of JSON objects. | ||
*/ | ||
public final class JsonConversion implements IMessageHandler { | ||
|
||
private final IMessageHandler conversion; | ||
|
||
public JsonConversion(IMessageHandler conversion) { | ||
this.conversion = conversion; | ||
} | ||
|
||
@Override | ||
public boolean onNewMessage(Subject subject, Map<String, String> headers, String body) { | ||
JsonPayload originalPayload = new JsonPayload(new DefaultPayload(body)); | ||
|
||
boolean msgSent = true; | ||
for (String message : originalPayload.messages()) { // each object individually as a String | ||
if (!conversion.onNewMessage(subject, headers, message)) { | ||
msgSent = false; | ||
} | ||
} | ||
|
||
return msgSent; | ||
} | ||
|
||
@Override | ||
public Subject asSubject(String token) { | ||
return conversion.asSubject(token); | ||
} | ||
|
||
@Override | ||
public boolean requiresToken() { | ||
return conversion.requiresToken(); | ||
} | ||
|
||
@Override | ||
public IMessageHandler copy() { | ||
return new JsonConversion(conversion.copy()); | ||
} | ||
|
||
@Override | ||
public Map<String, String> responseHeaders() { | ||
return conversion.responseHeaders(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/com/teragrep/lsh_01/conversion/JsonPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
logstash-http-input to syslog bridge | ||
Copyright 2024 Suomen Kanuuna Oy | ||
|
||
Derivative Work of Elasticsearch | ||
Copyright 2012-2015 Elasticsearch | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.teragrep.lsh_01.conversion; | ||
|
||
import jakarta.json.*; | ||
|
||
import java.io.StringReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A Json array payload splittable into individual json objects. | ||
*/ | ||
public final class JsonPayload implements Payload { | ||
|
||
private final Payload payload; | ||
|
||
public JsonPayload(Payload payload) { | ||
this.payload = payload; | ||
} | ||
|
||
/** | ||
* Splits the array of JSON objects into payloads with one object each. Has a side effect of removing whitespace | ||
* from the payloads because of jsonObject.toString(). | ||
* | ||
* @return list of messages | ||
*/ | ||
@Override | ||
public List<String> messages() { | ||
List<String> allMessages = new ArrayList<>(); | ||
for (String message : payload.messages()) { | ||
JsonReader reader = Json.createReader(new StringReader(message)); | ||
JsonArray payloadMessages = reader.readArray(); | ||
|
||
// transform all json objects into DefaultPayloads and return the list | ||
allMessages.addAll(payloadMessages.getValuesAs(JsonObject::toString)); | ||
} | ||
|
||
return allMessages; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object object) { | ||
if (this == object) | ||
return true; | ||
if (object == null) | ||
return false; | ||
if (object.getClass() != this.getClass()) | ||
return false; | ||
final JsonPayload cast = (JsonPayload) object; | ||
return payload.equals(cast.payload); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could switch-case be replaced with another object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to refactor code for creating a decorator for this in NettyHttpServer and even some other objects that use / forward the RelpConversion object. This was so that I could put the config checking code there so it doesn't clutter the other code. This however seems to be impossible now with objects extending and implementing 3rd party objects that are not immutable. Would need a complete refactoring really.
Would it be terrible to create an object just for creating this Conversion object with the config? Not really OOP style, but the code would be somewhere else than in the Main class.