Skip to content

Commit

Permalink
Merge pull request #6 from satks/develop
Browse files Browse the repository at this point in the history
JSON Filter Processor
  • Loading branch information
shaikidris authored Dec 23, 2016
2 parents 4e8dc79 + d2b9ebb commit c76018d
Show file tree
Hide file tree
Showing 25 changed files with 1,837 additions and 0 deletions.
94 changes: 94 additions & 0 deletions fabric-components/filter-processor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2016 ANI Technologies Pvt. Ltd.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>fabric-filter-processor</artifactId>

<parent>
<groupId>com.olacabs.fabric</groupId>
<artifactId>fabric-components</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fabric.version>0.4.5</fabric.version>
<mockito.version>1.10.19</mockito.version>
<jway.version>2.1.0</jway.version>
<fge.version>2.2.6</fge.version>
</properties>

<dependencies>
<dependency>
<groupId>com.olacabs.fabric</groupId>
<artifactId>fabric-compute-framework</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.olacabs.fabric</groupId>
<artifactId>fabric-compute-framework</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>${jway.version}</version>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${fge.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 ANI Technologies Pvt. Ltd.
*
* 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.olacabs.fabric.jsonfilter;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.olacabs.fabric.jsonfilter.impl.*;

/**
* TODO javadoc.
*/

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "filter_type")
@JsonSubTypes({@Type(value = MatchFilter.class, name = "match"), @Type(value = InFilter.class, name = "in"),
@Type(value = ExistFilter.class, name = "exist"), @Type(value = BoolFilter.class, name = "bool"),
@Type(value = CompareFilter.class, name = "compare")})
public interface Filter {

JsonProvider JSON_PROVIDER = Configuration.defaultConfiguration().jsonProvider();

boolean filter(String json);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 ANI Technologies Pvt. Ltd.
*
* 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.olacabs.fabric.jsonfilter;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.olacabs.fabric.jsonfilter.dsl.BaseDslParser;
import com.olacabs.fabric.jsonfilter.dsl.DslParser;

/**
* TODO javadoc.
*/
public final class FilterCreator {

private FilterCreator() {}

public static Filter createFilter(File filterFile) throws IOException, ProcessingException {
JsonNode jsonNode = JsonLoader.fromFile(filterFile);
String dsl = jsonNode.toString();
return createFilter(dsl);
}

public static Filter createFilter(String filterDsl) throws IOException, ProcessingException {
DslParser dslparser = BaseDslParser.getInstance();
return dslparser.parse(filterDsl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2016 ANI Technologies Pvt. Ltd.
*
* 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.olacabs.fabric.jsonfilter.dsl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.olacabs.fabric.jsonfilter.Filter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* TODO javadoc.
*/
@Slf4j
public final class BaseDslParser implements DslParser {

private static final String SCHEMAFILE = "dslSchema.json";

private static final ObjectMapper MAPPER = new ObjectMapper();

private JsonSchema schema = null;

private static class BaseDslParserHolder {
private static final BaseDslParser INSTANCE;

static {
try {
INSTANCE = new BaseDslParser();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}

public static BaseDslParser getInstance() throws IOException, ProcessingException {
return BaseDslParserHolder.INSTANCE;
}

private BaseDslParser() throws IOException, ProcessingException {

final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonNode schemaJson = getSchemaFilezfromPath();
schema = factory.getJsonSchema(schemaJson);
}

@Override
public Filter parse(String dsl) throws IOException, ProcessingException {
Filter filter = null;
if (isValid(dsl)) {
JsonNode jsonNode = MAPPER.readTree(dsl);
jsonNode = jsonNode.at("/filter");
filter = MAPPER.readValue(jsonNode.toString(), Filter.class);
}
log.debug(filter != null ? filter.toString() : null);
return filter;
}

private boolean isValid(String dsl) throws ProcessingException, IOException {
JsonNode jsonNode = JsonLoader.fromString(dsl);
ProcessingReport report;
report = schema.validate(jsonNode);

if (!report.isSuccess()) {
throw new RuntimeException("Invalid Dsl :\\n" + report.toString());
}
return report.isSuccess();
}

private JsonNode getSchemaFilezfromPath() throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(SCHEMAFILE);
return JsonLoader.fromReader(new InputStreamReader(inputStream));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2016 ANI Technologies Pvt. Ltd.
*
* 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.olacabs.fabric.jsonfilter.dsl;

import java.io.IOException;

import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.olacabs.fabric.jsonfilter.Filter;

/**
* TODO javadoc.
*/
public interface DslParser {

Filter parse(String dsl) throws IOException, ProcessingException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2016 ANI Technologies Pvt. Ltd.
*
* 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.olacabs.fabric.jsonfilter.impl;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;

import com.olacabs.fabric.jsonfilter.Filter;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* TODO javadoc.
*/
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class BoolFilter implements Filter {

@JsonProperty("must")
private List<Filter> must = Lists.newArrayList();

@JsonProperty("should")
private List<Filter> should = new ArrayList<>();

@JsonProperty("must_not")
private List<Filter> mustNot = new ArrayList<>();

@Override
public boolean filter(String json) {
return (must.isEmpty() || must.stream().allMatch(filter -> filter.filter(json)))
& (should.isEmpty() || should.stream().anyMatch(filter -> filter.filter(json)))
& (mustNot.isEmpty() || mustNot.stream().noneMatch(filter -> filter.filter(json)));
}

}
Loading

0 comments on commit c76018d

Please sign in to comment.