Search in sources :

Example 1 with EzyHandler

use of com.tvd12.ezyfox.function.EzyHandler 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 2 with EzyHandler

use of com.tvd12.ezyfox.function.EzyHandler in project ezyfox-server by youngmonkeys.

the class EzyUserRequestPrototypeControllerTest method test.

@Test
public void test() {
    // given
    EzyPrototypeFactory prototypeFactory = mock(EzyPrototypeFactory.class);
    when(prototypeFactory.getSuppliers(EzyRequestListener.class)).thenReturn(Collections.emptyList());
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    when(beanContext.getPrototypeFactory()).thenReturn(prototypeFactory);
    InternalController sut = new InternalController.Builder().beanContext(beanContext).build();
    // when
    EzyAppContext appContext = mock(EzyAppContext.class);
    EzyUserRequestEvent event = mock(EzyUserRequestEvent.class);
    String cmd = RandomUtil.randomShortAlphabetString();
    EzyHandler handler = mock(EzyHandler.class);
    sut.postHandle(appContext, event, cmd, handler);
    // then
    verify(beanContext, times(1)).getPrototypeFactory();
    verify(beanContext, times(1)).getSingleton("unmarshaller", EzyUnmarshaller.class);
}
Also used : EzyPrototypeFactory(com.tvd12.ezyfox.bean.EzyPrototypeFactory) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyUserRequestEvent(com.tvd12.ezyfoxserver.event.EzyUserRequestEvent) EzyHandler(com.tvd12.ezyfox.function.EzyHandler) Test(org.testng.annotations.Test)

Aggregations

EzyHandler (com.tvd12.ezyfox.function.EzyHandler)2 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)1 EzyPrototypeFactory (com.tvd12.ezyfox.bean.EzyPrototypeFactory)1 EzyPrototypeSupplier (com.tvd12.ezyfox.bean.EzyPrototypeSupplier)1 EzyDataBinding (com.tvd12.ezyfox.binding.EzyDataBinding)1 EzyBadRequestException (com.tvd12.ezyfox.core.exception.EzyBadRequestException)1 EzyArray (com.tvd12.ezyfox.entity.EzyArray)1 EzyData (com.tvd12.ezyfox.entity.EzyData)1 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)1 EzySessionAware (com.tvd12.ezyfoxserver.entity.EzySessionAware)1 EzyUserAware (com.tvd12.ezyfoxserver.entity.EzyUserAware)1 EzyUserRequestEvent (com.tvd12.ezyfoxserver.event.EzyUserRequestEvent)1 Test (org.testng.annotations.Test)1