Skip to content
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
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ security.authRequired,true,Sets whether Basic HTTP Authorization headers are req
credentials.file,etc/credentials.json,A json file with array of identity:credential mappings
lookups.hostname.file,etc/hostname.json,Path to username-to-hostname lookup table
lookups.appname.file,etc/appname.json,Path to username-to-appname lookup table
payload.splitRegex, \n (newline), A regex based on which incoming requests will be split into multiple outgoing messages
payload.splitEnabled, false, Sets whether splitting incoming messages by splitRegex is enabled
payload.splitType, none, Sets how to split incoming messages. Supported split types are 'regex' and 'json_array'. Use 'none' for no splitting.
payload.splitType.regex.pattern, \n (newline), A regex based on which incoming requests will be split into multiple outgoing messages
|===

=== Lookup tables
Expand Down
4 changes: 2 additions & 2 deletions etc/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ credentials.file=etc/credentials.json
lookups.hostname.file=etc/hostname.json
lookups.appname.file=etc/appname.json

payload.splitRegex=\n
payload.splitEnabled=false
payload.splitType=none
payload.splitType.regex.pattern=\n
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- JSON processing -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.7</version>
</dependency>
<!-- junit, testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/teragrep/lsh_01/HttpInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.teragrep.lsh_01;

import com.teragrep.lsh_01.config.InternalEndpointUrlConfig;
import com.teragrep.lsh_01.conversion.IMessageHandler;
import com.teragrep.lsh_01.util.LoggingHttpObjectAggregator;
import com.teragrep.lsh_01.util.SslHandlerProvider;
import io.netty.channel.ChannelInitializer;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/teragrep/lsh_01/HttpServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.teragrep.lsh_01;

import com.teragrep.lsh_01.config.InternalEndpointUrlConfig;
import com.teragrep.lsh_01.conversion.IMessageHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/teragrep/lsh_01/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.teragrep.lsh_01.authentication.BasicAuthentication;
import com.teragrep.lsh_01.authentication.BasicAuthenticationFactory;
import com.teragrep.lsh_01.config.*;
import com.teragrep.lsh_01.conversion.*;
import com.teragrep.lsh_01.pool.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -60,18 +61,22 @@ public static void main(String[] args) {
RelpConnectionFactory relpConnectionFactory = new RelpConnectionFactory(relpConfig);
Pool<IManagedRelpConnection> pool = new Pool<>(relpConnectionFactory, new ManagedRelpConnectionStub());

RelpConversion relpConversion = new RelpConversion(
pool,
securityConfig,
basicAuthentication,
lookupConfig,
payloadConfig
);
IMessageHandler conversion = new RelpConversion(pool, securityConfig, basicAuthentication, lookupConfig);

// apply splitting if configured
switch (payloadConfig.splitType) { // payloadConfig is Validateable, no need to check wrong config here
Copy link

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?

Copy link
Author

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.

case "regex":
conversion = new RegexConversion(conversion, payloadConfig.regexPattern);
break;
case "json_array":
conversion = new JsonConversion(conversion);
break;
}

try (
NettyHttpServer server = new NettyHttpServer(
nettyConfig,
relpConversion,
conversion,
null,
200,
internalEndpointUrlConfig
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/teragrep/lsh_01/MessageProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.teragrep.lsh_01.authentication.*;
import com.teragrep.lsh_01.config.InternalEndpointUrlConfig;
import com.teragrep.lsh_01.conversion.IMessageHandler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/teragrep/lsh_01/NettyHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.teragrep.lsh_01.config.InternalEndpointUrlConfig;
import com.teragrep.lsh_01.config.NettyConfig;
import com.teragrep.lsh_01.conversion.IMessageHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/com/teragrep/lsh_01/config/PayloadConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link

Choose a reason for hiding this comment

The 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 src/main/java/com/teragrep/lsh_01/conversion/DefaultPayload.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.lsh_01;
package com.teragrep.lsh_01.conversion;

import com.teragrep.lsh_01.authentication.Subject;

Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/teragrep/lsh_01/conversion/JsonConversion.java
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 src/main/java/com/teragrep/lsh_01/conversion/JsonPayload.java
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);
}
}
Loading