<commit_before>/*
 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
 */

package com.palantir.dropwizard.weblogger;

import static com.google.common.base.Preconditions.checkNotNull;

import com.fasterxml.jackson.databind.SerializationFeature;
import io.dropwizard.jackson.Jackson;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.TimeZone;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Resource for logging analytics events.
 */
@Path("web-logger")
@Consumes(MediaType.APPLICATION_JSON)
public final class WebLoggerResource {

    private static final Logger analyticsLogger = LoggerFactory.getLogger(AnalyticsAppenderFactory.ANALYTICS_LOGGER);

    private final WebLoggerConfiguration config;

    public WebLoggerResource(WebLoggerConfiguration config) {
        checkNotNull(config);

        // serialise dates as ISO 8601
        Jackson.newObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        this.config = config;
    }

    @POST
    public void logContent(String eventJsonString) throws ParseException {

        JSONObject jsonEvent = new JSONObject(eventJsonString);
        Set<String> logLineFields = jsonEvent.keySet();

        boolean validEvent = false;
        String eventType = "";

        for (LoggerEvent event : config.getEvents()) {
            if (event.getFields().containsAll(logLineFields)) {
                validEvent = true;
                eventType = event.getType();
            }
        }

        if (!validEvent) {
            returnFieldsDontMatchError(jsonEvent);
        }

        jsonEvent = addFixedFields(jsonEvent);
        jsonEvent = addEventType(jsonEvent, eventType);

        analyticsLogger.info(jsonEvent.toString());
    }

    private void returnFieldsDontMatchError(JSONObject jsonEvent) {
        String fieldSets = "";
        for (LoggerEvent event : config.getEvents()) {
            fieldSets  += event.getFields().toString() + " ";
        }

        throw new BadRequestException("It's likely that the fields in the log provided don't "
                + "match the server's configuration. Please adjust your log fields or the"
                + " configuration in your <server>.yml file. Possible choices are: "
                + fieldSets + ". You provided: " + jsonEvent.keySet());
    }

    private JSONObject addEventType(JSONObject jsonEvent, String eventType) {
        jsonEvent.put("EventType", eventType);
        return jsonEvent;
    }

    private JSONObject addFixedFields(JSONObject jsonEvent) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss z");
        ft.setTimeZone(TimeZone.getTimeZone("UTC"));
        jsonEvent.put("timestamp", ft.format(new Date()));
        return jsonEvent;
    }
}<commit_msg>fix findbugs issue<commit_after>/*
 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
 */

package com.palantir.dropwizard.weblogger;

import static com.google.common.base.Preconditions.checkNotNull;

import com.fasterxml.jackson.databind.SerializationFeature;
import io.dropwizard.jackson.Jackson;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.TimeZone;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Resource for logging analytics events.
 */
@Path("web-logger")
@Consumes(MediaType.APPLICATION_JSON)
public final class WebLoggerResource {

    private static final Logger analyticsLogger = LoggerFactory.getLogger(AnalyticsAppenderFactory.ANALYTICS_LOGGER);

    private final WebLoggerConfiguration config;

    public WebLoggerResource(WebLoggerConfiguration config) {
        checkNotNull(config);

        // serialise dates as ISO 8601
        Jackson.newObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        this.config = config;
    }

    @POST
    public void logContent(String eventJsonString) throws ParseException {

        JSONObject jsonEvent = new JSONObject(eventJsonString);
        Set<String> logLineFields = jsonEvent.keySet();

        boolean validEvent = false;
        String eventType = "";

        for (LoggerEvent event : config.getEvents()) {
            if (event.getFields().containsAll(logLineFields)) {
                validEvent = true;
                eventType = event.getType();
            }
        }

        if (!validEvent) {
            returnFieldsDontMatchError(jsonEvent);
        }

        jsonEvent = addFixedFields(jsonEvent);
        jsonEvent = addEventType(jsonEvent, eventType);

        analyticsLogger.info(jsonEvent.toString());
    }

    private void returnFieldsDontMatchError(JSONObject jsonEvent) {
        StringBuffer buffer = new StringBuffer();
        for (LoggerEvent event : config.getEvents()) {
            buffer.append(event.getFields().toString() + " ");
        }
        String fieldSets = buffer.toString();

        throw new BadRequestException("It's likely that the fields in the log provided don't "
                + "match the server's configuration. Please adjust your log fields or the"
                + " configuration in your <server>.yml file. Possible choices are: "
                + fieldSets + ". You provided: " + jsonEvent.keySet());
    }

    private JSONObject addEventType(JSONObject jsonEvent, String eventType) {
        jsonEvent.put("EventType", eventType);
        return jsonEvent;
    }

    private JSONObject addFixedFields(JSONObject jsonEvent) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss z");
        ft.setTimeZone(TimeZone.getTimeZone("UTC"));
        jsonEvent.put("timestamp", ft.format(new Date()));
        return jsonEvent;
    }
}
{'fraction_non_alphanumeric': 0.09829683698296837, 'fraction_numerical': 0.0032441200324412004, 'mean_word_length': 3.152188552188552, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2144793', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1645, 'n_words': 384}