<commit_before>/*
 *   Copyright (C) 2016 R&D Solutions 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 io.hawkcd.core.subscriber;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.log4j.Logger;
import io.hawkcd.core.Message;
import io.hawkcd.core.RequestProcessor;
import io.hawkcd.core.session.ISessionManager;
import io.hawkcd.core.session.SessionFactory;
import io.hawkcd.model.TaskDefinition;
import io.hawkcd.model.dto.WsContractDto;
import io.hawkcd.utilities.deserializers.MaterialDefinitionAdapter;
import io.hawkcd.utilities.deserializers.TaskDefinitionAdapter;
import io.hawkcd.model.MaterialDefinition;
import redis.clients.jedis.JedisPubSub;

/*
* Represents an application process(when running in separate VM)
* or thread ( when running in the same VM) that receives messages broadcasted by publishers
*/
public class Subscriber extends JedisPubSub {

    private Gson jsonConverter;
    private IMessageDispatcher messageDispatcher;
    private IMessageTranslator messageTranslator;
    private IMessageFilter authFilter;
    private RequestProcessor requestProcessor;
    private ISessionManager sessionManager;

    private static final Logger LOGGER = Logger.getLogger(Subscriber.class);

    public Subscriber() {

        this.jsonConverter = new GsonBuilder()
                .registerTypeAdapter(Envelopе.class, new EnvelopeAdapter())
                .registerTypeAdapter(TaskDefinition.class, new TaskDefinitionAdapter())
                .registerTypeAdapter(MaterialDefinition.class, new MaterialDefinitionAdapter())
                .create();

        requestProcessor = new RequestProcessor();
    }

    @Override
    public void onMessage(String channel, String msg) {
        LOGGER.debug(msg);

        Message message = this.jsonConverter.fromJson(msg, Message.class);

        WsContractDto contract = new WsContractDto(message.getServiceCalled(),
                                        "", message.getMethodCalled()
                                          , message.getResultObject()
                                          , message.getResultNotificationType()
                                          , message.getResultMessage());

        SessionFactory.getSessionManager().sendToAllSessions(contract);

    }
}
<commit_msg>Added some logic to be moved to another class #416<commit_after>/*
 *   Copyright (C) 2016 R&D Solutions 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 io.hawkcd.core.subscriber;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.hawkcd.model.PermissionObject;
import io.hawkcd.model.enums.PermissionType;
import org.apache.log4j.Logger;
import io.hawkcd.core.Message;
import io.hawkcd.core.RequestProcessor;
import io.hawkcd.core.session.ISessionManager;
import io.hawkcd.core.session.SessionFactory;
import io.hawkcd.model.TaskDefinition;
import io.hawkcd.model.dto.WsContractDto;
import io.hawkcd.utilities.deserializers.MaterialDefinitionAdapter;
import io.hawkcd.utilities.deserializers.TaskDefinitionAdapter;
import io.hawkcd.model.MaterialDefinition;
import redis.clients.jedis.JedisPubSub;

import java.util.Map;

/*
* Represents an application process(when running in separate VM)
* or thread ( when running in the same VM) that receives messages broadcasted by publishers
*/
public class Subscriber extends JedisPubSub {

    private Gson jsonConverter;
    private IMessageDispatcher messageDispatcher;
    private IMessageTranslator messageTranslator;
    private IMessageFilter authFilter;
    private RequestProcessor requestProcessor;
    private ISessionManager sessionManager;

    private static final Logger LOGGER = Logger.getLogger(Subscriber.class);

    public Subscriber() {

        this.jsonConverter = new GsonBuilder()
                .registerTypeAdapter(Envelopе.class, new EnvelopeAdapter())
                .registerTypeAdapter(TaskDefinition.class, new TaskDefinitionAdapter())
                .registerTypeAdapter(MaterialDefinition.class, new MaterialDefinitionAdapter())
                .create();

        requestProcessor = new RequestProcessor();
    }

    @Override
    public void onMessage(String channel, String msg) {
        LOGGER.debug(msg);

        Message message = this.jsonConverter.fromJson(msg, Message.class);

        WsContractDto contract = new WsContractDto(message.getServiceCalled(),
                                        "", message.getMethodCalled()
                                          , message.getResultObject()
                                          , message.getResultNotificationType()
                                          , message.getResultMessage());

        // TODO: Move logic to translator
        // Set Permission Type for each each Session to be send to and send
        Map<String, PermissionType> permissionTypeByUser = message.getPermissionTypeByUser();
        PermissionObject resultObject = (PermissionObject) message.getResultObject();
        for (String userId : permissionTypeByUser.keySet()) {
            resultObject.setPermissionType(permissionTypeByUser.get(userId));
            contract.setResult(resultObject);
            SessionFactory.getSessionManager().sendToAllSessions(contract);
        }
    }
}

{'fraction_non_alphanumeric': 0.07843754922034966, 'fraction_numerical': 0.0033076074972436605, 'mean_word_length': 3.0809768637532136, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3749224', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1597, 'n_words': 453}