All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.arkalix.core.plugin.HttpJsonEventUnsubscribe Maven / Gradle / Ivy

package se.arkalix.core.plugin;

import se.arkalix.ArConsumer;
import se.arkalix.ArConsumerFactory;
import se.arkalix.ArSystem;
import se.arkalix.description.ServiceDescription;
import se.arkalix.descriptor.EncodingDescriptor;
import se.arkalix.descriptor.TransportDescriptor;
import se.arkalix.net.http.client.HttpClient;
import se.arkalix.net.http.consumer.HttpConsumer;
import se.arkalix.net.http.consumer.HttpConsumerRequest;
import se.arkalix.util.concurrent.Future;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import static se.arkalix.core.plugin.internal.HttpJsonServices.unwrap;
import static se.arkalix.descriptor.EncodingDescriptor.JSON;
import static se.arkalix.descriptor.TransportDescriptor.HTTP;
import static se.arkalix.net.http.HttpMethod.DELETE;

/**
 * A remote {@link ArEventUnsubscribe} service that is communicated with via
 * HTTP/JSON in either secure or insecure mode.
 */
@SuppressWarnings("unused")
public class HttpJsonEventUnsubscribe implements ArConsumer, ArEventUnsubscribe {
    private static final Factory factory = new Factory();

    private final HttpConsumer consumer;

    HttpJsonEventUnsubscribe(final HttpConsumer consumer) {
        this.consumer = consumer;
    }

    /**
     * @return Consumer {@link ArConsumerFactory factory class}.
     */
    public static ArConsumerFactory factory() {
        return factory;
    }

    @Override
    public Future unsubscribe(final String topic, final String subscriberName, final String hostname, final int port) {
        return consumer.send(new HttpConsumerRequest()
            .method(DELETE)
            .uri(service().uri())
            .queryParameter("event_type", topic)
            .queryParameter("system_name", subscriberName)
            .queryParameter("address", hostname)
            .queryParameter("port", Integer.toString(port)))
            .flatMap(response -> unwrap(response, null));
    }

    @Override
    public ServiceDescription service() {
        return consumer.service();
    }

    private static class Factory implements ArConsumerFactory {
        @Override
        public Optional serviceName() {
            return Optional.of("event-unsubscribe");
        }

        @Override
        public Collection serviceTransports() {
            return Collections.singleton(HTTP);
        }

        @Override
        public Collection serviceEncodings() {
            return Collections.singleton(JSON);
        }

        @Override
        public HttpJsonEventUnsubscribe create(
            final ArSystem system,
            final ServiceDescription service,
            final Collection encodings) throws Exception
        {
            return new HttpJsonEventUnsubscribe(new HttpConsumer(HttpClient.from(system), service, encodings));
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy