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

When you annotate a method in a controller with this annotation then when a request with the command come, EzyFox Server will call this method. In the below example, secureChat method will handle secureChat 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

If you want to handle an event, you need use this annotation, 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

You can use this annotation for a class that handles exceptions. Example for GlobalExceptionHandler:

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

4. EzyRequestController

You can only use this annotation for controller class, example ChatRequestController:

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

5. EzyRequestData

In fact, EzyFox Server will detect and map request data automatically, but if you want to be clearer you can use 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

Sometimes, maybe the request handler is too long and you don't want to use a controller, so you can use a class to handle a command, you can use EzyRequestListener, example GreetRequestHandler:

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

You can look at Handle EzyFox Server Client Request for many way to handle a request with EzyFox Server

8. EzyTryCatch

You can use this annotation for a method that handles exceptions, example 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

You can take a look how to handle a socket event.