public class ChatController {
    @Inject
    WebSocketContext context;
    @Inject
    LanguageManager languageManager;

    public Response chat(Request request) {
        return Response.html("/template/chat.html", new ChatPage(), languageManager.language());
    }

    public Response publish(Request request) {
        final List<Channel<ChatMessage>> channels = context.all();
        for (Channel<ChatMessage> channel : channels) {
            String text = "hello " + channel.context().get("neo") + "! (from all)";
            var message = new ChatMessage();
            message.text = text;
            channel.send(message);
        }

        List<Channel<ChatMessage>> room = context.room("private");
        for (Channel<ChatMessage> channel : room) {
            var message = new ChatMessage();
            message.text = "hello from private";
            channel.send(message);
        }

        return Response.text("done");
    }

    public Response close(Request request) {
        for (Channel<?> channel : context.all()) {
            channel.close();
        }
        return Response.text("done");
    }
} public class ChatPage {
} public class IndexController {
    private final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Inject
    Message message;
    @Inject
    LanguageManager languageManager;

    @Protected(operation = "index")
    public Response index(Request request) {
        IndexPage model = new IndexPage();
        model.name = message.get("key.name", languageManager.language());
        model.imageURL = "https://image.com/image123.jpg";

        Session session = request.session();
//        Optional<String> hello = session.get("hello");
        session.set("hello", "world");
        Response response = Response.html("/template/index.html", model, languageManager.language())
                .header("Cache-Control", "no-store");
        response.cookie(Cookies.TEST, "1+2");
        response.cookie(Cookies.TEST1, "hello \"\" cookies");
        return response;
    }

    public Response submit(Request request) {
        logger.warn("test");
        return Response.text("hello " + request.formParams().getOrDefault("name", "nobody"));
    }

    public Response logout(Request request) {
        request.session().invalidate();
        return Response.text("logout");
    }
} public class IndexPage {
    public String name;
    public String backgroundColor;
    public String imageURL;

    public String welcomeMessage() {
        return "hello " + name;
    }

    public List<String> items() {
        return List.of("a", "b", "c");
    }

    public Boolean selected(String item) {
        return "b".equals(item);
    }
} public class LanguageManager {
    public String language() {
        return "en_US";
    }
} public class UploadController {
    @Inject
    LanguageManager languageManager;

    public Response get(Request request) {
        return Response.html("/template/upload.html", new UploadPage(), languageManager.language());
    }

    public Response post(Request request) {
        MultipartFile file = request.files().get("test");
        return Response.text("uploaded, fileName=" + file.fileName);
    }
} public class UploadPage {
} public class WildcardController {
    public Response wildcard(Request request) {
        return Response.text("catch all url").contentType(ContentType.TEXT_HTML);
    }
} public class AJAXController {
    public Response ajax(Request request) {
        Bean bean = request.bean(Bean.class);
        return Response.text("posted: " + JSON.toJSON(bean));
    }
} public class Bean {
    @Property(name = "name")
    public String name;
    @Property(name = "desc")
    public String description;
    @Property(name = "password")
    public String password;
} public enum ErrorCodes {
    @Property(name = "E_1")
    ERROR_1,
    @Property(name = "E_2")
    ERROR_2
} public class TestInterceptor implements Interceptor {
    private final Logger logger = LoggerFactory.getLogger(TestInterceptor.class);

    @Override
    public Response intercept(Invocation invocation) throws Exception {
        Protected annotation = invocation.annotation(Protected.class);
        if (annotation != null) {
            logger.debug("detected @Protected");
        }
        return invocation.proceed();
    }
} public class ChatListener implements ChannelListener<ChatMessage, ChatMessage> {
    @Override
    public void onConnect(Request request, Channel<ChatMessage> channel) {
        channel.context().put("name", request.session().get("hello").orElse("no session"));
        channel.join("private");
    }

    @Override
    @LimitRate("message")
    public void onMessage(Channel<ChatMessage> channel, ChatMessage message) {
        if ("stop".equals(message.text)) {
            channel.close();
            return;
        }
        ChatMessage response = new ChatMessage();
        response.text = "mirror back: " + message.text + " by " + channel.context().get("name");
        channel.send(response);
    }
} public class ChatMessage {
    @Property(name = "text")
    public String text;
}
import app.web.ChatController;
import app.web.ChatPage;
import app.web.IndexController;
import app.web.IndexPage;
import app.web.LanguageManager;
import app.web.UploadController;
import app.web.UploadPage;
import app.web.WildcardController;
import app.web.ajax.AJAXController;
import app.web.ajax.Bean;
import app.web.ajax.ErrorCodes;
import app.web.interceptor.TestInterceptor;
import app.web.ws.ChatListener;
import app.web.ws.ChatMessage;
import core.framework.api.http.HTTPStatus;
import core.framework.module.Module;
import core.framework.web.Response;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static core.framework.http.HTTPMethod.GET;
import static core.framework.http.HTTPMethod.POST;
package app;




/**
 * @author neo
 */
public class WebModule extends Module {
    @Override
    protected void initialize() {
        http().intercept(bind(TestInterceptor.class));