EzyFox Server Annotations

Updated at 1689065597000

Supported Annotations

EzyFox Server
provides a list of annotations:
#AnnotationScopesDescription
1EzyDoHandleMethodMaps a request command to a controller's method
2EzyEventHandlerClassMaps an event to a controller
3EzyExceptionHandlerClassIndicates the annotated class will handle exceptions
4EzyRequestControllerMethodIndicates the annotated class is controller
5EzyRequestDataParameterMaps a request data to a java parameter
6EzyRequestInterceptorClassIndicate the annotated class will intercept requests
7EzyRequestListenerClassMap a request command to a handler class
8EzyTryCatchMethodIndicates the annotated method will handle the exception in TryCatch

1. EzyDoHandle

below examplesecureChatsecureChat
command:
@EzyRequestController
public class ChatRequestController extends EzyLoggable {
    @EzyDoHandle("secureChat")
    public void secureChat(EzyUser user, ChatRequest request) {
        responseFactory.newObjectResponse()
            .encrypted()
            .command(Commands.SECURE_CHAT)
            .param("secure-message", greeting.greet(request.getWho()))
            .user(user)
            .execute();
    }
}

2. EzyEventHandler

example for SERVER_READY event
:
@EzyEventHandler(SERVER_READY) // refer EzyEventType
public class ServerReadyController
    extends EzyAbstractAppEventController<EzyServerReadyEvent> {
    @Override
    public void handle(EzyAppContext ctx, EzyServerReadyEvent event) {
        logger.info("hello-world app: fire custom app ready, node name: {}", appConfig.getNodeName());
    }
}

3. EzyExceptionHandler

GlobalExceptionHandler
:
@EzyExceptionHandler
public class GlobalExceptionHandler extends EzyLoggable {
    private final EzyResponseFactory responseFactory;
    @EzyTryCatch(InvalidChatRequestException.class)
    public void handle(InvalidChatRequestException e) {}
}

4. EzyRequestController

ChatRequestController
:
@EzyRequestController
public class ChatRequestController extends EzyLoggable {
    @EzyDoHandle(Commands.SECURE_CHAT)
    public void secureChat(EzyUser user, ChatRequest request) {}
}

5. EzyRequestData

EyRequestData
like this:
@EzyDoHandle("hello")
public void handleHelloRequest(
    EzyContext context,
    String cmd,
    @EzyRequestData Hello data
) {
    // handle the hello request
}

6. EzyRequestInteceptor

You can use this annotation for a class that intercepts client requests, example:
@EzyRequestInterceptor
public class AppRequestInterceptor
    extends EzyLoggable
    implements EzyUserRequestInterceptor<EzyAppContext> {
    @Override
    public void preHandle(
        EzyAppContext context,
        EzyUserSessionEvent event,
        String command,
        Object data
    ) {
        logger.info("user: {} request command: {}", event.getUser(), command);
    }
}

7. EzyRequestListener

EzyRequestListenerGreetRequestHandler
:
@Setter
@EzyPrototype
@EzyObjectBinding(write = false)
@EzyRequestListener(GREET)
public class GreetRequestHandler
    extends ClientRequestHandler
    implements EzyDataBinding {
    private String who;
    @EzyAutoBind
    private Greeting greeting;
    @Override
    protected void execute() throws EzyBadRequestException {
        responseFactory.newObjectResponse()
            .command(GREET)
            .param("message", greeting.greet(who))
            .session(session)
            .execute();
    }
}
Handle EzyFox Server Client Request
for many way to handle a request with EzyFox Server

8. EzyTryCatch

GlobalExceptionHandler.handle
@EzyTryCatch(InvalidChatRequestException.class)
public void handle(
    InvalidChatRequestException e,
    EzyUserSessionEvent event,
    String command,
    Object request
) {
    logger.info("invalid chat request, command: {}, data: {}", command, request, e);
    responseFactory.newObjectResponse()
        .command(EzyResponseCommands.ERROR)
        .data(EzyEntityObjects.newObject("request", "invalid"))
        .session(event.getSession())
        .execute();
}

Next Step


how to handle a socket event
.

Table Of Contents