<commit_before>/*
 * Copyright 2013 dc-square GmbH
 *
 *  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.dcsquare.hivemq.plugin.accesslog;

import com.dcsquare.hivemq.spi.PluginEntryPoint;
import com.dcsquare.hivemq.spi.callback.CallbackPriority;
import com.dcsquare.hivemq.spi.callback.events.*;
import com.dcsquare.hivemq.spi.callback.exception.InvalidSubscriptionException;
import com.dcsquare.hivemq.spi.callback.exception.OnPublishReceivedException;
import com.dcsquare.hivemq.spi.callback.exception.RefusedConnectionException;
import com.dcsquare.hivemq.spi.callback.registry.CallbackRegistry;
import com.dcsquare.hivemq.spi.message.CONNECT;
import com.dcsquare.hivemq.spi.message.PUBLISH;
import com.dcsquare.hivemq.spi.message.SUBSCRIBE;
import com.dcsquare.hivemq.spi.message.UNSUBSCRIBE;
import com.dcsquare.hivemq.spi.security.ClientData;
import com.dcsquare.hivemq.spi.util.PathUtils;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.LoggingLevel;
import org.pmw.tinylog.labellers.TimestampLabeller;
import org.pmw.tinylog.policies.DailyPolicy;
import org.pmw.tinylog.writers.RollingFileWriter;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * This is the main class of the plugin, which adds the callbacks to necessary events, which should be logged.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class AccessLog extends PluginEntryPoint {

    org.slf4j.Logger log = LoggerFactory.getLogger(AccessLog.class);

    /**
     * In the constructor the callbacks and the logging configuration are initialized.
     *
     * @param callbackRegistry injected HiveMQ Callback Registry
     */
    @Inject
    public AccessLog(CallbackRegistry callbackRegistry) {

        try {
            Configurator.defaultConfig()
                    .writer(new RollingFileWriter(new File(PathUtils.getHiveMQLogFolder(), "hivemq-access.log").getAbsolutePath(), 10, new TimestampLabeller("yyyMMdd"), new DailyPolicy()))
                    .level(LoggingLevel.INFO)
                    .formatPattern("{date:yyyy-MM-dd HH:mm:ss}  {message}")
                    .activate();

            callbackRegistry.addCallback(onConnectCallback());
            callbackRegistry.addCallback(disconnectCallback());
            callbackRegistry.addCallback(onSubscribe());
            callbackRegistry.addCallback(onPublish());
            callbackRegistry.addCallback(onUnsubscribeCallback());
        } catch (IOException e) {
            log.error("An error occured while configuring the AccessLogger. No Callbacks registered", e);
        }
    }

    /**
     * @return Anonymous class implementing OnUnsubscribeCallback
     */
    private OnUnsubscribeCallback onUnsubscribeCallback() {
        return new OnUnsubscribeCallback() {
            @Override
            public void onUnsubscribe(UNSUBSCRIBE unsubscribe, ClientData clientData) {
                Logger.info("{0} UNSUBSCRIBED FROM {1}", clientData.getClientId(), Arrays.toString(unsubscribe.getTopics().toArray()));
            }
        };
    }

    /**
     * @return Anonymous class implementing OnPublishReceivedCallback
     */
    private OnPublishReceivedCallback onPublish() {
        return new OnPublishReceivedCallback() {

            @Override
            public void onPublishReceived(PUBLISH publish, ClientData clientData) throws OnPublishReceivedException {
                String s = null;
                try {
                    s = new String(publish.getPayload(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Logger.info("{0} PUBLISHED MESSAGE TO {1} PAYLOAD: {2}", clientData.getClientId(), publish.getTopic(), s);
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }
        };

    }

    /**
     * @return Anonymous class implementing OnSubscribeCallback
     */
    private OnSubscribeCallback onSubscribe() {
        return new OnSubscribeCallback() {

            @Override
            public void onSubscribe(SUBSCRIBE subscribe, ClientData clientData) throws InvalidSubscriptionException {
                Logger.info("{0} SUBSCRIBED TO {1}", clientData.getClientId(), Arrays.toString(subscribe.getTopics().toArray()));
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }
        };
    }

    /**
     * @return Anonymous class implementing OnDisconnectCallback
     */
    private OnDisconnectCallback disconnectCallback() {
        return new OnDisconnectCallback() {

            @Override
            public void onDisconnect(ClientData clientData, boolean b) {
                Logger.info("{0} DISCONNECTED", clientData.getClientId());
            }

        };
    }

    /**
     * @return Anonymous class implementing OnConnectCallback
     */
    private OnConnectCallback onConnectCallback() {
        return new OnConnectCallback() {

            @Override
            public void onConnect(CONNECT connect, ClientData clientData) throws RefusedConnectionException {
                Logger.info("{0} CONNECTED", connect.getClientIdentifier());
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }


        };
    }
}<commit_msg>Removed exception handling when converting payload and using Guava Charsets instead<commit_after>/*
 * Copyright 2013 dc-square GmbH
 *
 *  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.dcsquare.hivemq.plugin.accesslog;

import com.dcsquare.hivemq.spi.PluginEntryPoint;
import com.dcsquare.hivemq.spi.callback.CallbackPriority;
import com.dcsquare.hivemq.spi.callback.events.*;
import com.dcsquare.hivemq.spi.callback.exception.InvalidSubscriptionException;
import com.dcsquare.hivemq.spi.callback.exception.OnPublishReceivedException;
import com.dcsquare.hivemq.spi.callback.exception.RefusedConnectionException;
import com.dcsquare.hivemq.spi.callback.registry.CallbackRegistry;
import com.dcsquare.hivemq.spi.message.CONNECT;
import com.dcsquare.hivemq.spi.message.PUBLISH;
import com.dcsquare.hivemq.spi.message.SUBSCRIBE;
import com.dcsquare.hivemq.spi.message.UNSUBSCRIBE;
import com.dcsquare.hivemq.spi.security.ClientData;
import com.dcsquare.hivemq.spi.util.PathUtils;
import com.google.common.base.Charsets;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.LoggingLevel;
import org.pmw.tinylog.labellers.TimestampLabeller;
import org.pmw.tinylog.policies.DailyPolicy;
import org.pmw.tinylog.writers.RollingFileWriter;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * This is the main class of the plugin, which adds the callbacks to necessary events, which should be logged.
 *
 * @author <NAME>
 * @author Christian Goetz
 */
public class AccessLog extends PluginEntryPoint {

    org.slf4j.Logger log = LoggerFactory.getLogger(AccessLog.class);

    /**
     * In the constructor the callbacks and the logging configuration are initialized.
     *
     * @param callbackRegistry injected HiveMQ Callback Registry
     */
    @Inject
    public AccessLog(CallbackRegistry callbackRegistry) {

        try {
            Configurator.defaultConfig()
                    .writer(new RollingFileWriter(new File(PathUtils.getHiveMQLogFolder(), "hivemq-access.log").getAbsolutePath(), 10, new TimestampLabeller("yyyMMdd"), new DailyPolicy()))
                    .level(LoggingLevel.INFO)
                    .formatPattern("{date:yyyy-MM-dd HH:mm:ss}  {message}")
                    .activate();

            callbackRegistry.addCallback(onConnectCallback());
            callbackRegistry.addCallback(disconnectCallback());
            callbackRegistry.addCallback(onSubscribe());
            callbackRegistry.addCallback(onPublish());
            callbackRegistry.addCallback(onUnsubscribeCallback());
        } catch (IOException e) {
            log.error("An error occured while configuring the AccessLogger. No Callbacks registered", e);
        }
    }

    /**
     * @return Anonymous class implementing OnUnsubscribeCallback
     */
    private OnUnsubscribeCallback onUnsubscribeCallback() {
        return new OnUnsubscribeCallback() {
            @Override
            public void onUnsubscribe(UNSUBSCRIBE unsubscribe, ClientData clientData) {
                Logger.info("{0} UNSUBSCRIBED FROM {1}", clientData.getClientId(), Arrays.toString(unsubscribe.getTopics().toArray()));
            }
        };
    }

    /**
     * @return Anonymous class implementing OnPublishReceivedCallback
     */
    private OnPublishReceivedCallback onPublish() {
        return new OnPublishReceivedCallback() {

            @Override
            public void onPublishReceived(PUBLISH publish, ClientData clientData) throws OnPublishReceivedException {
                String s = new String(publish.getPayload(), Charsets.UTF_8);
                Logger.info("{0} PUBLISHED MESSAGE TO {1} PAYLOAD: {2}", clientData.getClientId(), publish.getTopic(), s);
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }
        };

    }

    /**
     * @return Anonymous class implementing OnSubscribeCallback
     */
    private OnSubscribeCallback onSubscribe() {
        return new OnSubscribeCallback() {

            @Override
            public void onSubscribe(SUBSCRIBE subscribe, ClientData clientData) throws InvalidSubscriptionException {
                Logger.info("{0} SUBSCRIBED TO {1}", clientData.getClientId(), Arrays.toString(subscribe.getTopics().toArray()));
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }
        };
    }

    /**
     * @return Anonymous class implementing OnDisconnectCallback
     */
    private OnDisconnectCallback disconnectCallback() {
        return new OnDisconnectCallback() {

            @Override
            public void onDisconnect(ClientData clientData, boolean b) {
                Logger.info("{0} DISCONNECTED", clientData.getClientId());
            }

        };
    }

    /**
     * @return Anonymous class implementing OnConnectCallback
     */
    private OnConnectCallback onConnectCallback() {
        return new OnConnectCallback() {

            @Override
            public void onConnect(CONNECT connect, ClientData clientData) throws RefusedConnectionException {
                Logger.info("{0} CONNECTED", connect.getClientIdentifier());
            }

            @Override
            public int priority() {
                return CallbackPriority.MEDIUM;
            }
        };
    }
}
{'fraction_non_alphanumeric': 0.09234588332643773, 'fraction_numerical': 0.0036408771203971864, 'mean_word_length': 2.7733374960974086, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1115594', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3207, 'n_words': 756}