View Javadoc
1   package com.atlassian.plugin.refimpl.webresource;
2   
3   import com.atlassian.plugin.refimpl.CurrentHttpRequest;
4   import com.atlassian.plugin.webresource.DefaultResourceBatchingConfiguration;
5   import com.atlassian.plugin.webresource.ResourceBatchingConfiguration;
6   import com.google.common.collect.ImmutableList;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import java.util.List;
10  
11  /**
12   * Config class for batching that detects if the refapp should batch for all browsers, just IE or none at all. Resources
13   * that need to be super-batched are hardcoded within the class.
14   */
15  
16  public class RefAppResourceBatchingConfiguration extends DefaultResourceBatchingConfiguration
17          implements ResourceBatchingConfiguration {
18  
19      private static String SUPER_BATCH_FOR = System.getProperty("super.batch.for");
20      private static final boolean DEV_MODE = Boolean.getBoolean("atlassian.dev.mode");
21  
22      private static final List<String> resources = ImmutableList.of(
23              "com.atlassian.refapp.amd:amd",
24              "com.atlassian.auiplugin:ajs"
25      );
26  
27      @Override
28      public boolean isSuperBatchingEnabled() {
29          return forceBatchingInThisRequest();
30      }
31  
32      @Override
33      public List<String> getSuperBatchModuleCompleteKeys() {
34          return resources;
35      }
36  
37      @Override
38      public boolean isContextBatchingEnabled() {
39          return !DEV_MODE;
40      }
41  
42      @Override
43      public boolean isPluginWebResourceBatchingEnabled() {
44          return !DEV_MODE || forceBatchingInThisRequest();
45      }
46  
47      private boolean forceBatchingInThisRequest() {
48          // Only force batching if SUPER_BATCH_FOR variable is set, always return true if SUPER_BATCH_FOR is set to 'all'
49          if ("all".equalsIgnoreCase(SUPER_BATCH_FOR)) {
50              return true;
51          }
52          //if SUPER_BATCH_FOR is set to 'ie' return true only when the user agent of the request is IE.
53          else if ("ie".equalsIgnoreCase(SUPER_BATCH_FOR)) {
54              HttpServletRequest httpRequest = CurrentHttpRequest.getRequest();
55              if (httpRequest == null) {
56                  return false;
57              }
58              //only batch if the request came from IE (very simple implementation)'
59  
60              String userAgent = httpRequest.getHeader("USER-AGENT");
61  
62              if (userAgent != null) {
63                  return userAgent.contains("Trident");
64              }
65          }
66  
67          //return false (turn super batch off) if variable isn't set
68          return false;
69      }
70  }