Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor some Java in the DataFilter class
Fixes #74
- Loading branch information
1 parent
2cdd589
commit 7543d8f
Showing
16 changed files
with
402 additions
and
85 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,13 @@ | |
Gemfile.lock | ||
.bundle | ||
vendor | ||
|
||
# build dirs | ||
build | ||
.gradle | ||
|
||
# Intellij | ||
.idea | ||
*.iml | ||
|
||
gradle.properties |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/org/logstash/filters/DynamicTzInputHandler.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,25 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
import org.logstash.filters.parser.TimestampParser; | ||
|
||
import java.io.IOException; | ||
|
||
class DynamicTzInputHandler implements InputHandler { | ||
private TimestampParser parser; | ||
private String timeZone; | ||
|
||
public DynamicTzInputHandler(TimestampParser parser, String timeZone) { | ||
this.parser = parser; | ||
this.timeZone = timeZone; | ||
} | ||
|
||
public DynamicTzInputHandler(TimestampParser parser) { | ||
this.parser = parser; | ||
} | ||
|
||
public Instant handle(String input, Event event) throws IOException { | ||
return this.parser.parseWithTimeZone(input, event.sprintf(timeZone)); | ||
} | ||
} |
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,17 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
import org.logstash.Timestamp; | ||
|
||
class FieldSetter implements ResultSetter { | ||
private String target; | ||
|
||
FieldSetter(String target) { | ||
this.target = target; | ||
} | ||
|
||
public void set(Event event, Instant instant) { | ||
event.setField(this.target, new Timestamp(instant.getMillis())); | ||
} | ||
} |
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,10 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
|
||
import java.io.IOException; | ||
|
||
interface InputHandler { | ||
Instant handle(String input, Event event) throws IOException; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/logstash/filters/NumericParserExecutor.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,28 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
import org.logstash.filters.parser.TimestampParser; | ||
|
||
import java.io.IOException; | ||
|
||
class NumericParserExecutor implements ParserExecutor { | ||
private TimestampParser parser; | ||
public NumericParserExecutor(TimestampParser parser) { | ||
this.parser = parser; | ||
} | ||
|
||
public Instant execute(Object input, Event event) throws IOException { | ||
if (input instanceof String) { | ||
return parser.parse((String) input); | ||
} else if (input instanceof Long) { | ||
return parser.parse((Long)input); | ||
} else if (input instanceof Integer) { | ||
return parser.parse(((Integer) input).longValue()); | ||
} else if (input instanceof Double) { | ||
return parser.parse((Double) input); | ||
} else { | ||
throw new IllegalArgumentException("Cannot parse date for value of type " + input.getClass().getName()); | ||
} | ||
} | ||
} |
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,7 @@ | ||
package org.logstash.filters; | ||
|
||
public enum ParseExecutionResult { | ||
SUCCESS, | ||
FAIL, | ||
NO_INPUT_FOUND | ||
} |
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,10 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
|
||
import java.io.IOException; | ||
|
||
interface ParserExecutor { | ||
Instant execute(Object input, Event event) throws IOException; | ||
} |
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,8 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
|
||
interface ResultSetter { | ||
void set(Event event, Instant instant); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/logstash/filters/StringInputHandler.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,19 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
import org.logstash.filters.parser.TimestampParser; | ||
|
||
import java.io.IOException; | ||
|
||
class StringInputHandler implements InputHandler { | ||
private TimestampParser parser; | ||
|
||
public StringInputHandler(TimestampParser parser) { | ||
this.parser = parser; | ||
} | ||
|
||
public Instant handle(String input, Event event) throws IOException { | ||
return this.parser.parse(input); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/logstash/filters/TextParserExecutor.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,30 @@ | ||
package org.logstash.filters; | ||
|
||
import org.joda.time.Instant; | ||
import org.logstash.Event; | ||
import org.logstash.filters.parser.TimestampParser; | ||
|
||
import java.io.IOException; | ||
|
||
class TextParserExecutor implements ParserExecutor { | ||
private InputHandler handler; | ||
|
||
public TextParserExecutor(TimestampParser parser, String timeZone) { | ||
if (timeZone != null && timeZone.contains("%{")) { | ||
this.handler = new DynamicTzInputHandler(parser, timeZone); | ||
} else { | ||
this.handler = new StringInputHandler(parser); | ||
} | ||
} | ||
|
||
public Instant execute(Object input, Event event) throws IOException { | ||
if (!(input instanceof String)) { | ||
throw new IllegalArgumentException("Cannot parse date for value of type " + input.getClass().getName()); | ||
} | ||
return this.execute((String) input, event); | ||
} | ||
|
||
private Instant execute(String input, Event event) throws IOException { | ||
return this.handler.handle(input, event); | ||
} | ||
} |
Oops, something went wrong.