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

Support extraction of Fields from JSON data source #182

Open
hemanthkilari opened this issue May 21, 2018 · 2 comments
Open

Support extraction of Fields from JSON data source #182

hemanthkilari opened this issue May 21, 2018 · 2 comments

Comments

@hemanthkilari
Copy link

Support for extraction of Fields from JSON data source in Ranger can make it powerful in streamlining data across the Requests mentioned in Berserker configuration. I have already Implemented support for it. Attached the Implementation. Please provide your comments.

@hemanthkilari
Copy link
Author

package io.smartcat.ranger.core;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jayway.jsonpath.JsonPath;

/**

  • @author hkilari
  •     UserDefined Value Implementation to allow Berserker User define a Custom Value in
    
  •     berserker ranger configuration. The Custom Value will be retrieved from the data source
    
  •     configured by User based on his query config
    

*/
public class UserDefinedValue extends Value {

private Value<String> jsonName;
private Value<String> filter;
private Value<String> regExpression;
private List<String> finalContent;
private long contentlastUpdationTime;
private Value<String> refreshInterval;
private Value<String> defaultRefreshInterval = new Value<String>() {
};

public UserDefinedValue(Value<String> regExpression, Value<String> refreshInterval, Value<String> filter,
        Value<String> jsonName) {
    defaultRefreshInterval.val = "600";
    if (jsonName == null) {
        throw new IllegalArgumentException("datasource cannot be null.");
    }
    this.jsonName = jsonName;
    this.filter = filter;
    this.regExpression = regExpression;
    this.refreshInterval = refreshInterval == null ? defaultRefreshInterval : refreshInterval;
    this.finalContent = new ArrayList<>();
}

@Override
public void reset() {
    jsonName.reset();
    filter.reset();
    regExpression.reset();
    refreshInterval.reset();
    super.reset();

}

@Override
protected void eval() {
    if (isContentExpired()) {
        getDataFromJson();
    }
    val = getRandomValue();
}

private boolean isContentExpired() {
    //Checks if the finalContent to be  refreshed based on used defined refreshInterval.
    if (System.currentTimeMillis() - contentlastUpdationTime > Long.parseLong(refreshInterval.val) * 1000) {
        return true;
    }
    return false;
}

private String getRandomValue() {
    if (finalContent != null && !finalContent.isEmpty())
        return extractFiled(finalContent.get(new Random().nextInt(finalContent.size())).toString(),
                regExpression.get());
    else
        return "";
}

private void getDataFromJson() {
    try {
        //check If Source is a http service
        if (jsonName.get().contains("http")) {
            getDataFromHttpRequest();
        } else {
            //Source treated as a local json file
            InputStream in =
                    Thread.currentThread().getContextClassLoader().getResourceAsStream(jsonName.get() + ".json");
            if (in == null) {
                in = new FileInputStream(jsonName.get() + ".json");
            }
            if (in != null) {
                finalContent = JsonPath.read(in, filter.get());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Retrieves data from a http servcie endpoint
 */
private void getDataFromHttpRequest() {
    try {
        InputStream in;
        URL obj = new URL(jsonName.val);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("accept", "application/json");
        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            in = con.getInputStream();
            finalContent = JsonPath.read(in, filter.get());
            in.close();
            contentlastUpdationTime = System.currentTimeMillis();
        } else {
            System.out.println("GET request not worked");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * @param latestCommandOutput
 * @param regExpression
 * @return
 */
private String extractFiled(String inputString, String regExpression) {
    if (regExpression != null && !"".equals(regExpression.trim())) {
        Pattern p = Pattern.compile(regExpression);
        Matcher m = p.matcher(inputString);
        if (m.find()) {
            if (m.groupCount() > 0)
                return m.group(1);
        }
        return null;
    }
    return inputString;
}

}

@vajda
Copy link

vajda commented May 21, 2018

Thanks for your effort. But it is fairly hard to read code from the comment. Can you please branch from dev and create a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants