1 package com.atlassian.plugin.servlet;
2
3 /**
4 * Parses resource download URLs for a plugin resource download strategy, and can determine whether a given URL is valid
5 * for that download strategy.
6 * <p/>
7 * The URLs are in the form "/servletPath/strategyPrefix/moduleCompleteKey/resourceName", where the 'servletPath' is
8 * defined as {@link AbstractFileServerServlet#SERVLET_PATH}, the 'strategyPrefix' is defined by a constructor parameter,
9 * the remainder is parsed by this parser into a {@link PluginResource}.
10 * @deprecated Since 2.2
11 */
12 public class ResourceUrlParser
13 {
14 private final String strategyPrefix;
15
16 /**
17 * Create a parser with the given strategy prefix for parsing URLs.
18 * <p/>
19 * For example, a strategy prefix of 'resources' means that this parser will match URLs which include
20 * 'download/resources'. (Where 'download' is defined as {@link AbstractFileServerServlet#SERVLET_PATH}.)
21 *
22 * @param strategyPrefix a String which will be found following 'download' at the start of matching URLs.
23 */
24 public ResourceUrlParser(String strategyPrefix)
25 {
26 this.strategyPrefix = strategyPrefix;
27 }
28
29 /**
30 * Parses resource download URLs for this plugin resource download strategy, returning a {@link PluginResource}.
31 * Returns <tt>null</tt> if the URL does not match (i.e. {@link #matches(String)} returns <tt>false</tt>), or the
32 * final part of the URL cannot be split into a moduleCompleteKey and resourceName (i.e. there's no slash in the
33 * final component).
34 * <p/>
35 * Parsing uses the download strategy prefix to determine where in the URL string to start. The strategy prefix and
36 * one character following it (typically a slash) is skipped, then the remainder is split on the first slash found.
37 *
38 * @param resourceUrl the URL of the resource request
39 * @return a resource which includes the plugin or plugin module's complete key and the resource name, or
40 * <tt>null</tt> if the URL doesn't parse correctly.
41 */
42 public PluginResource parse(String resourceUrl)
43 {
44 if (!matches(resourceUrl))
45 return null;
46
47 int indexOfStrategyPrefix = resourceUrl.indexOf(strategyPrefix);
48 String libraryAndResource = resourceUrl.substring(indexOfStrategyPrefix + strategyPrefix.length() + 1);
49 String[] parts = libraryAndResource.split("/", 2);
50
51 if (parts.length != 2)
52 return null;
53 return new PluginResource(parts[0], parts[1]);
54 }
55
56 /**
57 * Returns true if the provided URL matches a the URL prefix defined for this download strategy.
58 *
59 * @param resourceUrl the URL of the resource request
60 * @return <tt>true</tt> if the URL designates a request for this download strategy, otherwise <tt>false</tt>.
61 */
62 public boolean matches(String resourceUrl)
63 {
64 return resourceUrl.indexOf(AbstractFileServerServlet.SERVLET_PATH + "/" + strategyPrefix) != -1;
65 }
66 }