Search in sources :

Example 1 with EzyUserRequestHandler

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!"));
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyResponseFactory(com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory) GreetRequest(com.tvd12.ezyfoxserver.support.test.data.GreetRequest) HelloController(com.tvd12.ezyfoxserver.support.test.controller.HelloController) EzyFeatureCommandManager(com.tvd12.ezyfoxserver.support.manager.EzyFeatureCommandManager) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyRequestCommandManager(com.tvd12.ezyfoxserver.support.manager.EzyRequestCommandManager) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyRequestHandlersImplementer(com.tvd12.ezyfoxserver.support.asm.EzyRequestHandlersImplementer) GreetResponse(com.tvd12.ezyfoxserver.support.test.data.GreetResponse) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyUserSessionEvent(com.tvd12.ezyfoxserver.event.EzyUserSessionEvent) EzyUserRequestHandler(com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler) EzySimpleUserSessionEvent(com.tvd12.ezyfoxserver.event.EzySimpleUserSessionEvent) EzyObjectResponse(com.tvd12.ezyfoxserver.support.command.EzyObjectResponse) Test(org.testng.annotations.Test)

Example 2 with EzyUserRequestHandler

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;
}
Also used : HashMap(java.util.HashMap) EzyRequestControllerProxy(com.tvd12.ezyfoxserver.support.reflect.EzyRequestControllerProxy) EzyUserRequestHandler(com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler) EzyRequestHandlerMethod(com.tvd12.ezyfoxserver.support.reflect.EzyRequestHandlerMethod)

Example 3 with EzyUserRequestHandler

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;
}
Also used : HashMap(java.util.HashMap) EzyDuplicateRequestHandlerException(com.tvd12.ezyfoxserver.support.exception.EzyDuplicateRequestHandlerException) EzyUserRequestHandler(com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler)

Example 4 with EzyUserRequestHandler

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

Aggregations

EzyUserRequestHandler (com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler)4 HashMap (java.util.HashMap)2 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 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)1 EzySimpleUser (com.tvd12.ezyfoxserver.entity.EzySimpleUser)1 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)1 EzySimpleUserSessionEvent (com.tvd12.ezyfoxserver.event.EzySimpleUserSessionEvent)1 EzyUserSessionEvent (com.tvd12.ezyfoxserver.event.EzyUserSessionEvent)1 EzyRequestHandlersImplementer (com.tvd12.ezyfoxserver.support.asm.EzyRequestHandlersImplementer)1 EzyObjectResponse (com.tvd12.ezyfoxserver.support.command.EzyObjectResponse)1 EzyDuplicateRequestHandlerException (com.tvd12.ezyfoxserver.support.exception.EzyDuplicateRequestHandlerException)1 EzyUserRequestException (com.tvd12.ezyfoxserver.support.exception.EzyUserRequestException)1 EzyResponseFactory (com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory)1 EzyUncaughtExceptionHandler (com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)1 EzyFeatureCommandManager (com.tvd12.ezyfoxserver.support.manager.EzyFeatureCommandManager)1 EzyRequestCommandManager (com.tvd12.ezyfoxserver.support.manager.EzyRequestCommandManager)1 EzyRequestControllerProxy (com.tvd12.ezyfoxserver.support.reflect.EzyRequestControllerProxy)1