Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Servlet support #70

Open
jbee opened this issue May 8, 2018 · 0 comments
Open

Servlet support #70

jbee opened this issue May 8, 2018 · 0 comments

Comments

@jbee
Copy link
Owner

jbee commented May 8, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant