View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.mappers;
12  
13  import com.opensymphony.module.sitemesh.Config;
14  import com.opensymphony.module.sitemesh.Decorator;
15  import com.opensymphony.module.sitemesh.DecoratorMapper;
16  import com.opensymphony.module.sitemesh.Page;
17  import com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper;
18  
19  import jakarta.servlet.http.HttpServletRequest;
20  
21  import java.util.Properties;
22  
23  /**
24   * The AjaxDecoratorMapper will exclude all "ajax" requests from being decorated. It will also make
25   * sure that error pages rendered during ajax request execution are not decorated.
26   */
27  public class AjaxDecoratorMapper extends AbstractDecoratorMapper {
28  
29    /** The ajax extension. */
30    private String ajaxExtension = ".ajax";
31  
32    @Override
33    public void init(Config config, Properties properties, DecoratorMapper decoratorMapper)
34        throws InstantiationException {
35  
36      super.init(config, properties, decoratorMapper);
37      if (properties.get("ajaxExtension") != null) {
38        ajaxExtension = (String) properties.get("ajaxExtension");
39      }
40    }
41  
42    @Override
43    public Decorator getDecorator(HttpServletRequest request, Page page) {
44  
45      boolean callMapperChain;
46      String originalUri = (String) request.getAttribute("jakarta.servlet.error.request_uri");
47      if (originalUri != null) {
48        //
49        // cut off the query string
50        //
51        int queryStringIndex = originalUri.indexOf('?');
52        if (queryStringIndex != -1) {
53          originalUri = originalUri.substring(0, queryStringIndex);
54        }
55      }
56      callMapperChain = (originalUri == null || !originalUri.endsWith(ajaxExtension))
57          && !request.getServletPath().endsWith(ajaxExtension);
58  
59      return callMapperChain ? super.getDecorator(request, page) : null;
60    }
61  
62  }