Search in sources :

Example 81 with EzyArray

use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.

the class EzySendResponseImpl method execute.

@Override
public void execute(EzyResponse response, EzySession recipient, boolean encrypted, boolean immediate, EzyTransportType transportType) {
    boolean success = false;
    EzyResponseApi responseApi = server.getResponseApi();
    EzyArray data = response.serialize();
    EzySimplePackage pack = newPackage(data, encrypted, transportType);
    pack.addRecipient(recipient);
    try {
        responseApi.response(pack, immediate);
        success = true;
    } catch (Exception e) {
        logger.error("send data: {}, to client: {} error", pack.getData(), recipient.getName(), e);
    } finally {
        pack.release();
    }
    boolean debug = server.getSettings().isDebug();
    if (debug && success && !ignoredLogCommands.contains(response.getCommand())) {
        logger.debug("send to: {} data: {}", recipient.getName(), data);
    }
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi)

Example 82 with EzyArray

use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.

the class EzyUserRequestPrototypeController method handle.

public void handle(C context, E event) {
    EzyArray data = event.getData();
    String cmd = data.get(0, String.class);
    EzyPrototypeSupplier supplier = handlers.get(cmd);
    if (supplier == null) {
        logger.warn("has no handler with command: {} from session: {}", cmd, event.getSession().getName());
        return;
    }
    EzyHandler handler = (EzyHandler) supplier.supply(beanContext);
    if (handler instanceof EzyUserAware) {
        ((EzyUserAware) handler).setUser(event.getUser());
    }
    if (handler instanceof EzySessionAware) {
        ((EzySessionAware) handler).setSession(event.getSession());
    }
    if (handler instanceof EzyDataBinding) {
        EzyData params = data.get(1, EzyData.class, null);
        if (params != null) {
            unmarshaller.unwrap(params, handler);
        }
    }
    try {
        preHandle(context, event, cmd, handler);
        handler.handle();
        postHandle(context, event, cmd, handler);
    } catch (EzyBadRequestException e) {
        if (e.isSendToClient()) {
            EzyData errorData = newErrorData(e);
            responseError(context, event, errorData);
        }
        logger.debug("request cmd: {} by session: {} with data: {} error", cmd, event.getSession().getName(), data, e);
        postHandle(context, event, cmd, handler, e);
    } catch (Exception e) {
        postHandle(context, event, cmd, handler, e);
        throw e;
    }
}
Also used : EzyUserAware(com.tvd12.ezyfoxserver.entity.EzyUserAware) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyData(com.tvd12.ezyfox.entity.EzyData) EzyPrototypeSupplier(com.tvd12.ezyfox.bean.EzyPrototypeSupplier) EzyDataBinding(com.tvd12.ezyfox.binding.EzyDataBinding) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException) EzyHandler(com.tvd12.ezyfox.function.EzyHandler) EzySessionAware(com.tvd12.ezyfoxserver.entity.EzySessionAware) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException)

Example 83 with EzyArray

use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.

the class EzyUserRequestSingletonController method handle.

public void handle(C context, E event) {
    EzyArray data = event.getData();
    String cmd = data.get(0, String.class);
    EzyUserRequestHandler handler = requestHandlers.get(cmd);
    if (handler == null) {
        prototypeController.handle(context, event);
        return;
    }
    Object handlerData = data.get(1, EzyData.class, null);
    Class requestDataType = handler.getDataType();
    if (requestDataType != null) {
        handlerData = unmarshaller.unmarshal(handlerData, requestDataType);
    }
    try {
        preHandle(context, event, cmd, handlerData);
        handler.handle(context, event, handlerData);
        postHandle(context, event, cmd, handlerData);
    } catch (Exception e) {
        postHandle(context, event, cmd, handlerData, e);
        if (e instanceof EzyBadRequestException) {
            EzyBadRequestException ex = (EzyBadRequestException) e;
            if (ex.isSendToClient()) {
                EzyData errorData = newErrorData(ex);
                responseError(context, event, errorData);
            }
            logger.debug("request cmd: {} by session: {} with data: {} error", cmd, event.getSession().getName(), data, e);
        } else {
            EzyUncaughtExceptionHandler exceptionHandler = getExceptionHandler(e.getClass());
            if (exceptionHandler == null) {
                throw new EzyUserRequestException(cmd, handlerData, e);
            }
            try {
                exceptionHandler.handleException(context, event, cmd, handlerData, e);
            } catch (Exception ex) {
                throw new EzyUserRequestException(cmd, handlerData, ex);
            }
        }
    }
}
Also used : EzyUserRequestException(com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyUserRequestHandler(com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler) EzyData(com.tvd12.ezyfox.entity.EzyData) EzyUncaughtExceptionHandler(com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException) EzyUserRequestException(com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException) EzyBadRequestException(com.tvd12.ezyfox.core.exception.EzyBadRequestException)

Example 84 with EzyArray

use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.

the class EzyDefaultAppEntryTest method handleClientRequest.

private void handleClientRequest(EzyAppContext context) {
    EzySimpleApplication app = (EzySimpleApplication) context.getApp();
    EzyAppRequestController requestController = app.getRequestController();
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzySimpleUser user = new EzySimpleUser();
    EzyArray data = EzyEntityFactory.newArrayBuilder().append("chat").append(EzyEntityFactory.newObjectBuilder().append("message", "greet")).build();
    EzyUserRequestAppEvent event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("chat").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("no command").append(EzyEntityFactory.newObjectBuilder().append("message", "greet")).build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("noUser").append(EzyEntityFactory.newObjectBuilder().append("message", "greet")).build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("noSession").append(EzyEntityFactory.newObjectBuilder().append("message", "greet")).build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("noDataBinding").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("badRequestSend").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("badRequestNoSend").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
    data = EzyEntityFactory.newArrayBuilder().append("exception").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    try {
        requestController.handle(context, event);
    } catch (Exception e) {
        assert e instanceof IllegalStateException;
    }
    data = EzyEntityFactory.newArrayBuilder().append("app").build();
    event = new EzySimpleUserRequestAppEvent(user, session, data);
    requestController.handle(context, event);
}
Also used : EzySimpleApplication(com.tvd12.ezyfoxserver.EzySimpleApplication) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzySimpleUserRequestAppEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserRequestAppEvent) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyAppRequestController(com.tvd12.ezyfoxserver.app.EzyAppRequestController) EzyUserRequestAppEvent(com.tvd12.ezyfoxserver.event.EzyUserRequestAppEvent)

Example 85 with EzyArray

use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.

the class EzyAbstractResponseApiTest method dataToMessageContentTest.

@Test
public void dataToMessageContentTest() {
    // given
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    InternalResponseApi sut = new InternalResponseApi();
    // when
    Throwable e = Asserts.assertThrows(() -> MethodInvoker.create().object(sut).method("dataToMessageContent").param(EzyArray.class, data).invoke());
    // then
    Asserts.assertEquals(UnsupportedOperationException.class, e.getCause().getCause().getClass());
}
Also used : EzyArray(com.tvd12.ezyfox.entity.EzyArray) Test(org.testng.annotations.Test)

Aggregations

EzyArray (com.tvd12.ezyfox.entity.EzyArray)80 Test (org.testng.annotations.Test)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)30 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)17 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)15 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)14 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)13 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)13 EzySimpleLoginRequest (com.tvd12.ezyfoxserver.request.EzySimpleLoginRequest)13 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)12 EzyObject (com.tvd12.ezyfox.entity.EzyObject)12 EzyLoginController (com.tvd12.ezyfoxserver.controller.EzyLoginController)12 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)11 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)9 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)8 BaseCoreTest (com.tvd12.ezyfoxserver.testing.BaseCoreTest)8 Collection (com.tvd12.dahlia.core.entity.Collection)7 EzyArray (com.tvd12.ezyfoxserver.client.entity.EzyArray)7 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)7 Record (com.tvd12.dahlia.core.entity.Record)5