io.bitsensor.plugins.java.blocking.BlockingManagerImpl Maven / Gradle / Ivy
package io.bitsensor.plugins.java.blocking;
import io.bitsensor.plugins.shaded.com.fasterxml.jackson.core.type.TypeReference;
import io.bitsensor.plugins.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import io.bitsensor.lib.entity.BlockedAttacker;
import io.bitsensor.lib.entity.Identifiable;
import io.bitsensor.lib.entity.util.ProtoUtils;
import io.bitsensor.plugins.java.blocking.BlockingEvent.Type;
import io.bitsensor.plugins.java.core.BitSensorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.rabbit.annotation.RabbitListener;
import io.bitsensor.plugins.shaded.org.springframework.beans.factory.annotation.Autowired;
import io.bitsensor.plugins.shaded.org.springframework.context.ApplicationEventPublisher;
import io.bitsensor.plugins.shaded.org.springframework.context.ApplicationListener;
import io.bitsensor.plugins.shaded.org.springframework.context.event.ContextRefreshedEvent;
import io.bitsensor.plugins.shaded.org.springframework.stereotype.Component;
import io.bitsensor.plugins.shaded.org.springframework.util.concurrent.ListenableFutureCallback;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A simple blocking manager implementation that fetches blacklist from Bitbrain rabbitmq instance and subscribes to
* blacklist changes.
*/
@Component
public class BlockingManagerImpl implements BlockingManager, ApplicationListener {
private static final Logger LOGGER = LoggerFactory.getLogger(BlockingManagerImpl.class);
private final ApplicationEventPublisher publisher;
private final AsyncRabbitTemplate asyncTemplate;
private Map blockedAttackerMap = new ConcurrentHashMap<>();
private ObjectMapper mapper = ProtoUtils.objectMapper();
@Autowired
public BlockingManagerImpl(ApplicationEventPublisher publisher, AsyncRabbitTemplate asyncTemplate) {
this.publisher = publisher;
this.asyncTemplate = asyncTemplate;
}
@Override
public Map getBlacklist() {
return blockedAttackerMap;
}
@Override
public BlockedAttacker getBlocking(String id) {
return blockedAttackerMap.get(id);
}
@Override
public void fetchBlacklist() {
doFetchBlacklist().addCallback(new ListenableFutureCallback