use of com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler in project ezyfox-server by youngmonkeys.
the class EzyRequestHandlersImplementerTest method test.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test() {
// given
EzyAppContext context = mock(EzyAppContext.class);
EzySession session = mock(EzyAbstractSession.class);
EzyUser user = new EzySimpleUser();
EzyUserSessionEvent event = new EzySimpleUserSessionEvent(user, session);
EzyRequestHandlerImplementer.setDebug(true);
EzyRequestHandlersImplementer implementer = new EzyRequestHandlersImplementer();
EzyResponseFactory responseFactory = mock(EzyResponseFactory.class);
EzyObjectResponse objectResponse = mock(EzyObjectResponse.class);
when(responseFactory.newObjectResponse()).thenReturn(objectResponse);
when(objectResponse.command("Big/Hello6")).thenReturn(objectResponse);
when(objectResponse.data(new GreetResponse("Hello Dzung!"))).thenReturn(objectResponse);
when(objectResponse.session(any())).thenReturn(objectResponse);
doNothing().when(objectResponse).execute();
implementer.setResponseFactory(responseFactory);
EzyFeatureCommandManager featureCommandManager = new EzyFeatureCommandManager();
EzyRequestCommandManager requestCommandManager = new EzyRequestCommandManager();
implementer.setFeatureCommandManager(featureCommandManager);
implementer.setRequestCommandManager(requestCommandManager);
Map<String, EzyUserRequestHandler> handlers = implementer.implement(Collections.singletonList(new HelloController()));
for (EzyUserRequestHandler handler : handlers.values()) {
handler.handle(context, event, new GreetRequest("Dzung"));
}
EzyRequestHandlerImplementer.setDebug(false);
implementer = new EzyRequestHandlersImplementer();
implementer.setFeatureCommandManager(featureCommandManager);
implementer.setRequestCommandManager(requestCommandManager);
// when
handlers = implementer.implement(Collections.singletonList(new HelloController()));
// then
Asserts.assertTrue(handlers.containsKey("Big/Hello"));
verify(responseFactory, times(1)).newObjectResponse();
verify(objectResponse, times(1)).command("Big/Hello6");
verify(objectResponse, times(1)).data(new GreetResponse("Hello Dzung!"));
}
use of com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler in project ezyfox-server by youngmonkeys.
the class EzyRequestHandlersImplementer method implement.
private Map<String, EzyUserRequestHandler> implement(Object controller) {
Map<String, EzyUserRequestHandler> handlers = new HashMap<>();
EzyRequestControllerProxy proxy = new EzyRequestControllerProxy(controller);
String feature = proxy.getFeature();
for (EzyRequestHandlerMethod method : proxy.getRequestHandlerMethods()) {
EzyRequestHandlerImplementer implementer = newImplementer(proxy, method);
EzyAsmRequestHandler handler = implementer.implement();
String command = handler.getCommand();
handlers.put(command, handler);
requestCommandManager.addCommand(command);
if (proxy.isManagement() || method.isManagement()) {
requestCommandManager.addManagementCommand(command);
}
if (proxy.isPayment() || method.isPayment()) {
requestCommandManager.addPaymentCommand(command);
}
String methodFeature = feature != null ? feature : method.getFeature();
if (EzyStrings.isNotBlank(methodFeature)) {
featureCommandManager.addFeatureCommand(methodFeature, command);
}
}
return handlers;
}
use of com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler in project ezyfox-server by youngmonkeys.
the class EzyRequestHandlersImplementer method implement.
public Map<String, EzyUserRequestHandler> implement(Collection<Object> controllers) {
Map<String, EzyUserRequestHandler> handlers = new HashMap<>();
for (Object controller : controllers) {
Map<String, EzyUserRequestHandler> map = implement(controller);
for (String command : map.keySet()) {
EzyUserRequestHandler handler = map.get(command);
EzyUserRequestHandler old = handlers.put(command, handler);
if (old != null && !allowOverrideCommand) {
throw new EzyDuplicateRequestHandlerException(command, old, handler);
}
}
}
return handlers;
}
use of com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler 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);
}
}
}
}
Aggregations