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

cz.mmsparams.api.utils.MessageFactory Maven / Gradle / Ivy

The newest version!
package cz.mmsparams.api.utils;

import cz.mmsparams.api.exceptions.MmsParamsExceptionBase;
import cz.mmsparams.api.interfaces.IClientBroadcastMessage;
import cz.mmsparams.api.interfaces.IClientInfoProvider;
import cz.mmsparams.api.interfaces.IPhoneInfoProvider;
import cz.mmsparams.api.interfaces.ITestIdProvider;
import cz.mmsparams.api.json.JsonUtilsSafe;
import cz.mmsparams.api.logging.APILoggerFactory;
import cz.mmsparams.api.logging.ApiLogFacade;
import cz.mmsparams.api.logging.ILogger;
import cz.mmsparams.api.websocket.MessageType;
import cz.mmsparams.api.websocket.MessageUtils;
import cz.mmsparams.api.websocket.WebSocketConstants;
import cz.mmsparams.api.websocket.WebSocketMessageBase;
import cz.mmsparams.api.websocket.messages.clientlib.RegisterClientLibMessage;
import cz.mmsparams.api.websocket.messages.errors.GenericErrorResponseMessage;
import cz.mmsparams.api.websocket.messages.registration.RegisterPhoneMessage;
import cz.mmsparams.api.websocket.model.clientlib.ClientInfo;
import cz.mmsparams.api.websocket.model.error.TestErrorModel;

public class MessageFactory
{
    private static final ILogger LOGGER = APILoggerFactory.getLogger(MessageFactory.class);

    private MessageFactory()
    {
    }

    public static  T create(Class clz)
    {
        return create(null, clz);
    }

    public static  T create(ITestIdProvider iTestIdProvider, Class clz)
    {
        try
        {
            Preconditions.checkNotNull(clz, "clz");
            T msg = clz.newInstance();
            msg.setMessageKey(MessageType.getKeyByClass(msg.getClass()));

            if (iTestIdProvider != null)
                msg.setTestID(iTestIdProvider.getTestId());

            return msg;
        }
        catch (Exception ex)
        {
            ApiLogFacade.logEx(LOGGER, "create", ex);
            return null;
        }
    }

    public static  T createServerRecipient(Class clz)
    {
        return createServerRecipient(null, clz);
    }

    public static  T createServerSender(Class clz)
    {
        return createServerSender(null, clz);
    }

    public static  T createServerSender(ITestIdProvider iTestIdProvider, Class clz)
    {
        T msg = create(iTestIdProvider, clz);
        msg.setSenderKey(WebSocketConstants.Server_Recipient_Key);
        return msg;
    }


    public static  T createServerRecipient(ITestIdProvider iTestIdProvider, Class clz)
    {
        return new MessageBuilder()
                .withTestId(iTestIdProvider)
                .withRecipientKeyServer()
                .build(clz);
    }

    public static  T createClientBroadcastMessage(Class clz)
    {
        return new MessageBuilder()
                .withRecipientKeyClientBroadcast()
                .build(clz);
    }

    public static RegisterPhoneMessage createRegisterPhoneMessage(IPhoneInfoProvider iPhoneInfoProvider)
    {
        RegisterPhoneMessage msg = createServerRecipient(RegisterPhoneMessage.class);

        msg.setPhoneInfoModel(iPhoneInfoProvider.getPhoneInfo());

        return msg;
    }

    public static GenericErrorResponseMessage createGenericErrorResponseMessage(String clzTag, MmsParamsExceptionBase ex, WebSocketMessageBase request, String info)
    {
        GenericErrorResponseMessage msg = create(null, GenericErrorResponseMessage.class);

        final TestErrorModel model = new TestErrorModel();
        model.setException(ex);
        if (ex != null)
            model.setTestErrorType(ex.getTestErrorType());
        model.setMessage(info);
        msg.setTestErrorModel(model);
        msg.setWebSocketMessage(JsonUtilsSafe.toJson(request));

        msg = MessageUtils.prepareResponse(msg, request);

        return msg;
    }

    @Deprecated
    public static GenericErrorResponseMessage createGenericErrorResponseMessage(String clzTag, MmsParamsExceptionBase ex, String messageID, String info)
    {
        GenericErrorResponseMessage msg = create(null, GenericErrorResponseMessage.class);

        final TestErrorModel model = new TestErrorModel();
        model.setException(ex);
        model.setMessage(info);
        if (ex != null)
            model.setTestErrorType(ex.getTestErrorType());
        msg.setTestErrorModel(model);
        msg.setMessageID(messageID);

        return msg;
    }

    public static RegisterClientLibMessage createRegisterClientLibMessage(ITestIdProvider iTestIdProvider, IClientInfoProvider iClientInfoProvider)
    {
        return createRegisterClientLibMessage(iTestIdProvider, iClientInfoProvider.getClientInfo());
    }

    public static RegisterClientLibMessage createRegisterClientLibMessage(ITestIdProvider iTestIdProvider, ClientInfo clientInfo)
    {
        Preconditions.checkNotNull(iTestIdProvider, "ITestIdProvider is required!");

        RegisterClientLibMessage msg = createServerRecipient(iTestIdProvider, RegisterClientLibMessage.class);

        msg.setClientInfo(clientInfo);

        return msg;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy