View Javadoc

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