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

clients.symphony.api.SignalsClient Maven / Gradle / Ivy

There is a newer version: 1.3.9
Show newest version
package clients.symphony.api;

import clients.ISymClient;
import clients.symphony.api.constants.AgentConstants;
import clients.symphony.api.constants.CommonConstants;
import exceptions.SymClientException;
import exceptions.UnauthorizedException;
import model.*;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

public class SignalsClient extends APIClient {

    private ISymClient botClient;

    public SignalsClient(ISymClient client) {
        botClient = client;
    }

    public List listSignals(int skip, int limit) throws SymClientException {
        List result;
        WebTarget builder
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.LISTSIGNALS);


        if(skip>0){
            builder = builder.queryParam("skip", skip);
        }
        if(limit>0){
            builder = builder.queryParam("limit", limit);
        }
        Response response = builder.request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .get();

        if(response.getStatus() == 204){
            result = new ArrayList<>();
        } else if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
                return listSignals(skip,limit);
            }
            return null;
        } else {
            result = response.readEntity(SignalList.class);
        }
        return result;

    }

    public Signal getSignal(String id) throws SymClientException {

        Response response
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.GETSIGNAL.replace("{id}", id))
                .request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .get();
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
                return getSignal(id);
            }
            return null;
        } else {
            return response.readEntity(Signal.class);
        }
    }

    public Signal createSignal(Signal signal) throws SymClientException {
        Response response
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.CREATESIGNAL)
                .request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .post(Entity.entity(signal, MediaType.APPLICATION_JSON));
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
                return createSignal(signal);
            }
            return null;
        } else {
            return response.readEntity(Signal.class);
        }
    }

    public Signal updateSignal(Signal signal) throws SymClientException {
        Response response
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.UPDATESIGNAL.replace("{id}", signal.getId()))
                .request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .post(Entity.entity(signal, MediaType.APPLICATION_JSON));
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
                return updateSignal(signal);
            }
            return null;
        } else {
            return response.readEntity(Signal.class);
        }
    }

    public void deleteSignal (String id) throws SymClientException {
        Response response
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.DELETESIGNAL.replace("{id}", id))
                .request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .post(null);
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
               deleteSignal(id);
            }
        }
    }

    public SignalSubscriptionResult subscribeSignal (String id, boolean self, List uids, boolean pushed) throws SymClientException {
        Response response;
        if (self) {
            response
                    = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                    .path(AgentConstants.SUBSCRIBESIGNAL.replace("{id}", id))
                    .request(MediaType.APPLICATION_JSON)
                    .header("sessionToken", botClient.getSymAuth().getSessionToken())
                    .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                    .post(null);

        } else {
            response
                    = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                    .path(AgentConstants.SUBSCRIBESIGNAL.replace("{id}", id))
                    .queryParam("pushed", pushed)
                    .request(MediaType.APPLICATION_JSON)
                    .header("sessionToken", botClient.getSymAuth().getSessionToken())
                    .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                    .post(Entity.entity(uids, MediaType.APPLICATION_JSON));
        }
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex) {
                subscribeSignal(id,self,uids,pushed);
            }
        }
        return response.readEntity(SignalSubscriptionResult.class);
    }

    public SignalSubscriptionResult unsubscribeSignal (String id, boolean self, List uids) throws SymClientException {
        Response response;
        if (self) {
            response
                    = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                    .path(AgentConstants.UNSUBSCRIBESIGNAL.replace("{id}", id))
                    .request(MediaType.APPLICATION_JSON)
                    .header("sessionToken", botClient.getSymAuth().getSessionToken())
                    .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                    .post(null);

        } else {
            response
                    = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                    .path(AgentConstants.UNSUBSCRIBESIGNAL.replace("{id}", id))
                    .request(MediaType.APPLICATION_JSON)
                    .header("sessionToken", botClient.getSymAuth().getSessionToken())
                    .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                    .post(Entity.entity(uids, MediaType.APPLICATION_JSON));

        }
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
                return null;
            } catch (UnauthorizedException ex) {
                return unsubscribeSignal(id,self,uids);
            }
        }
        return response.readEntity(SignalSubscriptionResult.class);
    }

    public SignalSubscriberList getSignalSubscribers(String id, int skip, int limit) throws SymClientException {
        WebTarget builder
                = botClient.getAgentClient().target(CommonConstants.HTTPSPREFIX + botClient.getConfig().getAgentHost() + ":" + botClient.getConfig().getAgentPort())
                .path(AgentConstants.GETSUBSCRIBERS.replace("{id}", id));

        if(skip>0){
            builder = builder.queryParam("skip", skip);
        }
        if(limit>0){
            builder = builder.queryParam("limit", limit);
        }
        Response response = builder.request(MediaType.APPLICATION_JSON)
                .header("sessionToken",botClient.getSymAuth().getSessionToken())
                .header("keyManagerToken", botClient.getSymAuth().getKmToken())
                .get();

        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            try {
                handleError(response, botClient);
            } catch (UnauthorizedException ex){
                return getSignalSubscribers(id, skip, limit);
            }
            return null;
        } else {
            return response.readEntity(SignalSubscriberList.class);
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy