Record Class ServletMapping

java.lang.Object
java.lang.Record
eu.rarogsoftware.rarog.platform.api.plugins.web.servlets.ServletMapping
Record Components:
pathMapping - ant path to match urls that will be handled by provided servlet
order - order of servlet used to define overriding order (negative values are also supported but not recommended)
servlet - servlet to be invoked when path is matched
initParameters - parameters to be passed to Servlet.init(ServletConfig) when servlet is initiated
All Implemented Interfaces:
FeatureDescriptor

public record ServletMapping(String pathMapping, int order, jakarta.servlet.http.HttpServlet servlet, Map<String,String> initParameters) extends Record implements FeatureDescriptor
Servlet mapping, which allows to associate the URL pattern with the servlet. It is advanced mapping that allow not only to define url mapping but also define order of servlets. Plugin defined servlets are designed to allow overriding and passing through.

Overriding works by utilizing order parameter. Servlets will lesser value of order are executed earlier, and when two servlets have the same order, then the one with more specific path matcher is executed as first. So it means if plugin with path mapping `/**` and order 1 will execute before all servlets with order 10. It allows to override paths defined by other servlets, which can be useful for use cases like: overriding login form, overriding MFA mechanism, etc. It is powerful tool, so consider using other tools before using servlets override.

Passing through is supplemental functionality to overriding. If plugin defined servlet throws NotHandledException, then request will be passed to the next servlet that matches request. For example if servlet matching `/login/**` throws NotHandledException then DispatcherServlet will be invoked. If all servlets matching request will throw NotHandledException then 404 Not Found error will be returned.

DispatcherServlet provided by spring, that supports all spring based controllers (including ones defined by plugins) is defined as mapping `/**` with order 10, and it passes through all requests that don't match any controller. So it is possible to define servlets that matches all not mapped requests by defining servlet mapping `/**` with order 11.

There are implemented some security mechanisms that affects all requests like authentication or CSRF protection. To enable some degree of control over these mechanisms provided servlet may implement control interfaces that are available in package eu.rarogsoftware.rarog.platform.api.plugins.web.servlets and their name ends with `Aware`, like AuthorizationManagerAware or CsrfProtectionAware. Example:

 public class OverridingServlet extends HttpServlet implements AuthorizationManagerAware, CsrfProtectionAware {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         resp.setStatus(HttpServletResponse.SC_OK);
         resp.getWriter().write("Overriding servlet response");
     }

     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         resp.setStatus(HttpServletResponse.SC_OK);
         resp.getWriter().write("Overriding servlet response");
     }

     @Override
     public Optional getAccessDecisionVoter(HttpServletRequest request) {
         return Optional.of(new AccessDecisionVoter() {
             @Override
             public boolean supports(ConfigAttribute attribute) {
                 return true;
             }

             @Override
             public boolean supports(Class clazz) {
                 return true;
             }

             @Override
             public int vote(Authentication authentication, Object object, Collection collection) {
                 return ACCESS_GRANTED;
             }
         });
     }

     @Override
     public Optional shouldDisableCsrfProtection(HttpServletRequest request) {
         return Optional.of(true);
     }
 }
 

NOTE: org.springframework.security.web.util.matcher.AntPathRequestMatcher is used to match the patterns.

See Also:
  • Field Details

  • Constructor Details

    • ServletMapping

      public ServletMapping(String pathMapping, int order, jakarta.servlet.http.HttpServlet servlet, Map<String,String> initParameters)
      Creates an instance of a ServletMapping record class.
      Parameters:
      pathMapping - the value for the pathMapping record component
      order - the value for the order record component
      servlet - the value for the servlet record component
      initParameters - the value for the initParameters record component
    • ServletMapping

      public ServletMapping(String pathMapping, int order, jakarta.servlet.http.HttpServlet servlet)
    • ServletMapping

      public ServletMapping(String pathMapping, jakarta.servlet.http.HttpServlet servlet)
  • Method Details

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • pathMapping

      public String pathMapping()
      Returns the value of the pathMapping record component.
      Returns:
      the value of the pathMapping record component
    • order

      public int order()
      Returns the value of the order record component.
      Returns:
      the value of the order record component
    • servlet

      public jakarta.servlet.http.HttpServlet servlet()
      Returns the value of the servlet record component.
      Returns:
      the value of the servlet record component
    • initParameters

      public Map<String,String> initParameters()
      Returns the value of the initParameters record component.
      Returns:
      the value of the initParameters record component