Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-9036

Sling Models: SlingHttpServletRequestWrapper.adaptTo() unwraps before adapting

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Sling Models
    • None

    Description

      Sling Model:

      @Model(adaptables = SlingHttpServletRequest.class, adapters = MySlingModel.class)
      class MySlingModel { 
        @Inject
        public MySlingModel(@Self SlingHttpServletRequest req) {
          logger.log(req.getResourceBundle().getClass().getName());   
        }
      }
      

      Calling code:

      // req obtained via JSP/HTL
      final SlingHttpServletRequest myWrappedReq = new SlingHttpServletRequestWrapper(req) {
        @Override
        public ResourceBundle getResourceBundle() {
          return new MyCustomResourceBundle();
        }
      };
       
      final MySlingModel myModel = myWrappedReq.adaptTo(MySlingModel.class);
      

      I would expect the log file to contain "MyCustomResourceBundle". Instead it contains "NullResourceBundle". 

      This is because the request is being "unwrapped" when doing the adaptTo() call: It keeps on delegating to the wrapped request. This should not have happened, i.e. how can we otherwise overwrite (parts of) requests and resources? 

      See also: https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/java/org/apache/sling/api/wrappers/SlingHttpServletRequestWrapper.java#L147

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              Henry Kuijpers Henry Kuijpers
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: