Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Servlet support #70

@jbee

Description

@jbee

Problems that would be solved by silk web

  • connect to servlet API/container
  • map requests to methods (handlers) for URLs
  • convert parameters, headers and cookies to typed method arguments
  • convert method return types to response body
  • map from and to types respecting the given or requested media types (media type dispatch)
  • user auth (redirect to login)
  • error handling (redirect to error page)

Techniques to solve them

  • use lambdas to avoid need to bind to specific annotations (or annotations at all)
  • dispatch by semantic (while one method implements a specific URL the dispatch might be dynamic. One task is to find out what method to use for a URL, another is to map the request to the args and the return value to result body. keep these 3 independent)

To make foreign annotations work they could be bound to a HTTP concept like this:

path(RequestMapping.class, a -> a.path());
param(RequestParam.class, a -> a.value());
param(PathVariable.class,  a -> '{'+ a.value() + ''});
auth(Role.class, a -> a.requires());
controller(Web.class);

Variables in a path are expected as {var-name}. If a param mapping returns a name with {...} it means a path var is meant so we add the curly brackets back to the name given in the annotation.

Using fix path patterns is only the default strategy to map paths to methods. It uses the annotation bound for path to extract the path pattern which also is the methods uniqueId by which mapping is done.

interface PathStrategy {
   String uniqueId(Method webMethod); // returns null if it is not a web method
}

also:

  • annotation based auth (enum -> exception)
  • exception based auth (exception -> redirect)
redirect(UnauthorizedAccess.class).to(Page.LOGIN);
redirect(UnauthorizedAccess.class).to("login.html");
redirect(RuntimeException.class).to(Page.ERROR);

Hooking into Servlet container

Spring hooks into servlet API.

public class MyWebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //...
        ServletRegistration.Dynamic servlet = servletContext.addServlet("springDispatcherServlet",
                                            new DispatcherServlet(ctx));
    }

as described here
https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-mvc-intro.html

Spring being spring this is a layer on top of actual servlet API as described here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/SpringServletContainerInitializer.html

The servlet API mechanism is called ServletContainerInitializer, see http://docs.jboss.org/jbossas/javadoc/7.1.2.Final/javax/servlet/ServletContainerInitializer.html

silk-web contains such a service hook file. It will then load web.java (in default package) where initialization of the web application is given or referenced.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions