<commit_before>package nl.us2.cloudpelican.stormprocessor;

/**
 * Created by robin on 07/06/15.
 */
import backtype.storm.Config;
import backtype.storm.messaging.local;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
//import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.storm.http.HttpResponse;
import org.apache.storm.http.auth.AuthScope;
import org.apache.storm.http.auth.Credentials;
import org.apache.storm.http.auth.UsernamePasswordCredentials;
import org.apache.storm.http.client.CredentialsProvider;
import org.apache.storm.http.client.HttpClient;
import org.apache.storm.http.client.methods.HttpGet;
import org.apache.storm.http.impl.client.BasicCredentialsProvider;
import org.apache.storm.http.impl.client.HttpClientBuilder;
import org.apache.storm.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import storm.starter.util.TupleHelpers;
//import backtype.storm.tuple.Values;
//import static backtype.storm.utils.Utils.DEFAULT_STREAM_ID;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;

/**
 *
 * @author robin
 */
public class MatchBolt extends BaseRichBolt {

    OutputCollector _collector;
    HashMap<String, Filter> filters;
    JsonParser jsonParser;
    private boolean localMode = false;
    private String regex;
    private HashMap<String, String> settings;

    private static final Logger LOG = LoggerFactory.getLogger(Main.class);

    public MatchBolt(HashMap<String, String> settings) {
        super();
        filters = null;
        this.settings = settings;
        this.regex = this.settings.get("match_regex");


    }

    public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
        _collector = collector;
        jsonParser = new JsonParser();

        // Local mode
        if (regex != null && !regex.trim().isEmpty()) {
            LOG.info("Setting up local regex " + regex);
            localMode = true;
            JsonObject obj = new JsonObject();
            String fakeId = UUID.randomUUID().toString();
            obj.addProperty("id", fakeId);
            obj.addProperty("regex", regex);
            filters = new HashMap<String, Filter>();
            filters.put(fakeId, new Filter(obj));
            LOG.info("Setup up local regex " + regex);
        }
    }

    public void execute(Tuple tuple) {
        if (TupleHelpers.isTickTuple(tuple)) {
            executeTick();
        } else {
            executeTuple(tuple);
        }
        _collector.ack(tuple);
    }

    public void executeTick() {
        loadFilters();
    }

    protected void loadFilters() {
        // Do not execute in local mode
        if (localMode) {
            return;
        }

        // Init
        if (filters == null) {
            filters = new HashMap<String, Filter>();
        }

        // Load
        try {
            HashMap<String, Filter> tmp = new HashMap<String, Filter>();
            //CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            //credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.get("supervisor_username"), settings.get("supervisor_password")));
            HttpClient client = HttpClientBuilder.create()/*.setDefaultCredentialsProvider(credentialsProvider)*/.build();

            String url = settings.get("supervisor_host") + "filter";
            LOG.info(url);
            HttpGet get = new HttpGet(url);

            HttpResponse resp = client.execute(get);
            String body = EntityUtils.toString(resp.getEntity());
            LOG.info(body);
            JsonObject outer = jsonParser.parse(body).getAsJsonObject();
            JsonArray arr = outer.get("filters").getAsJsonArray();
            for (JsonElement elm : arr) {
                try {
                    JsonObject filter = elm.getAsJsonObject();
                    Filter f = new Filter(filter);
                    if (!filters.containsKey(f.toString())) {
                        LOG.info("Loaded filter " + filter.toString());
                    }
                    tmp.put(f.Id(), f);
                } catch (Exception fe) {
                    LOG.error("Failed to load filter", fe);
                    fe.printStackTrace();
                }
            }

            // Swap
            filters = tmp;
        } catch (Exception e) {
            LOG.error("Failed to load filters", e);<commit_msg>DIY auth header<commit_after>package nl.us2.cloudpelican.stormprocessor;

/**
 * Created by robin on 07/06/15.
 */
import backtype.storm.Config;
import backtype.storm.messaging.local;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
//import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.storm.http.HttpResponse;
import org.apache.storm.http.auth.AuthScope;
import org.apache.storm.http.auth.Credentials;
import org.apache.storm.http.auth.UsernamePasswordCredentials;
import org.apache.storm.http.client.CredentialsProvider;
import org.apache.storm.http.client.HttpClient;
import org.apache.storm.http.client.methods.HttpGet;
import org.apache.storm.http.impl.client.BasicCredentialsProvider;
import org.apache.storm.http.impl.client.HttpClientBuilder;
import org.apache.storm.http.util.EntityUtils;
import org.apache.storm.netty.handler.codec.base64.Base64Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import storm.starter.util.TupleHelpers;
//import backtype.storm.tuple.Values;
//import static backtype.storm.utils.Utils.DEFAULT_STREAM_ID;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;

/**
 *
 * @author robin
 */
public class MatchBolt extends BaseRichBolt {

    OutputCollector _collector;
    HashMap<String, Filter> filters;
    JsonParser jsonParser;
    private boolean localMode = false;
    private String regex;
    private HashMap<String, String> settings;

    private static final Logger LOG = LoggerFactory.getLogger(Main.class);

    public MatchBolt(HashMap<String, String> settings) {
        super();
        filters = null;
        this.settings = settings;
        this.regex = this.settings.get("match_regex");


    }

    public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
        _collector = collector;
        jsonParser = new JsonParser();

        // Local mode
        if (regex != null && !regex.trim().isEmpty()) {
            LOG.info("Setting up local regex " + regex);
            localMode = true;
            JsonObject obj = new JsonObject();
            String fakeId = UUID.randomUUID().toString();
            obj.addProperty("id", fakeId);
            obj.addProperty("regex", regex);
            filters = new HashMap<String, Filter>();
            filters.put(fakeId, new Filter(obj));
            LOG.info("Setup up local regex " + regex);
        }
    }

    public void execute(Tuple tuple) {
        if (TupleHelpers.isTickTuple(tuple)) {
            executeTick();
        } else {
            executeTuple(tuple);
        }
        _collector.ack(tuple);
    }

    public void executeTick() {
        loadFilters();
    }

    protected void loadFilters() {
        // Do not execute in local mode
        if (localMode) {
            return;
        }

        // Init
        if (filters == null) {
            filters = new HashMap<String, Filter>();
        }

        // Load
        try {
            HashMap<String, Filter> tmp = new HashMap<String, Filter>();
            //CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            //credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.get("supervisor_username"), settings.get("supervisor_password")));
            HttpClient client = HttpClientBuilder.create()/*.setDefaultCredentialsProvider(credentialsProvider)*/.build();

            String url = settings.get("supervisor_host") + "filter";
            LOG.info(url);
            HttpGet get = new HttpGet(url);
            String token = new String(Base64.encodeBase64((settings.get("supervisor_username") + ":" + settings.get("supervisor_password")).getBytes()));
            LOG.info(token);
            get.setHeader("Authorization", "Basic " + token);

            HttpResponse resp = client.execute(get);
            String body = EntityUtils.toString(resp.getEntity());
            LOG.info(body);
            JsonObject outer = jsonParser.parse(body).getAsJsonObject();
            JsonArray arr = outer.get("filters").getAsJsonArray();
            for (JsonElement elm : arr) {
                try {
                    JsonObject filter = elm.getAsJsonObject();
                    Filter f = new Filter(filter);
                    if (!filters.containsKey(f.toString())) {
                        LOG.info("Loaded filter " + filter.toString());
                    }
                    tmp.put(f.Id(), f);
                } catch (Exception fe) {
                    LOG.error("Failed to load filter", fe);
                    fe.printStackTrace();
                }
            }

            // Swap
            filters = tmp;
        } catch (Exception e) {
            LOG.error("Failed to load filters", e);
{'fraction_non_alphanumeric': 0.11445421831267492, 'fraction_numerical': 0.0027988804478208716, 'mean_word_length': 2.5068349106203995, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6273275', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2691, 'n_words': 570}