View Javadoc

1   package com.atlassian.plugin.servlet.download.plugin;
2   
3   import java.util.Map;
4   import java.util.concurrent.ConcurrentHashMap;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import com.atlassian.plugin.ModuleDescriptor;
10  import com.atlassian.plugin.event.PluginEventListener;
11  import com.atlassian.plugin.event.PluginEventManager;
12  import com.atlassian.plugin.event.events.PluginModuleDisabledEvent;
13  import com.atlassian.plugin.event.events.PluginModuleEnabledEvent;
14  import com.atlassian.plugin.servlet.DownloadException;
15  import com.atlassian.plugin.servlet.DownloadStrategy;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  /**
21   * A download strategy which maintains a list of {@link DownloadStrategyModuleDescriptor}s
22   * and delegates to them in order.
23   *
24   * @see DownloadStrategyModuleDescriptor
25   * @see DownloadStrategy
26   * @since 2.2.0
27   */
28  public class PluggableDownloadStrategy implements DownloadStrategy
29  {
30      private static final Logger log = LoggerFactory.getLogger(PluggableDownloadStrategy.class);
31      private final Map<String, DownloadStrategy> strategies = new ConcurrentHashMap<String, DownloadStrategy>();
32  
33      public PluggableDownloadStrategy(final PluginEventManager pluginEventManager)
34      {
35          pluginEventManager.register(this);
36      }
37  
38      public boolean matches(final String urlPath)
39      {
40          for (final DownloadStrategy strategy : strategies.values())
41          {
42              if (strategy.matches(urlPath))
43              {
44                  log.debug("Matched plugin download strategy: {}", strategy.getClass().getName());
45                  return true;
46              }
47          }
48          return false;
49      }
50  
51      public void serveFile(final HttpServletRequest request, final HttpServletResponse response) throws DownloadException
52      {
53          for (final DownloadStrategy strategy : strategies.values())
54          {
55              if (strategy.matches(request.getRequestURI().toLowerCase()))
56              {
57                  strategy.serveFile(request, response);
58                  return;
59              }
60          }
61          throw new DownloadException(
62              "Found plugin download strategy during matching but not when trying to serve. Enable debug logging for more information.");
63      }
64  
65      public void register(final String key, final DownloadStrategy strategy)
66      {
67          if (strategies.containsKey(key))
68          {
69              log.warn("Replacing existing download strategy with module key: {}", key);
70          }
71          strategies.put(key, strategy);
72      }
73  
74      public void unregister(final String key)
75      {
76          strategies.remove(key);
77      }
78  
79      @PluginEventListener
80      public void pluginModuleEnabled(final PluginModuleEnabledEvent event)
81      {
82          final ModuleDescriptor<?> module = event.getModule();
83          if (!(module instanceof DownloadStrategyModuleDescriptor))
84          {
85              return;
86          }
87  
88          register(module.getCompleteKey(), (DownloadStrategy) module.getModule());
89      }
90  
91      @PluginEventListener
92      public void pluginModuleDisabled(final PluginModuleDisabledEvent event)
93      {
94          final ModuleDescriptor<?> module = event.getModule();
95          if (!(module instanceof DownloadStrategyModuleDescriptor))
96          {
97              return;
98          }
99  
100         unregister(module.getCompleteKey());
101     }
102 }