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;
}
}
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);
}
Aggregations