View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.util.concurrent.NotNull;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import java.io.UnsupportedEncodingException;
9   import java.net.URLEncoder;
10  import java.util.Map;
11  import java.util.TreeMap;
12  
13  public class ResourceUtils
14  {
15      private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);
16      /**
17       * @since 2.13
18       */
19      public static final String STATIC_HASH = new String("_statichash");
20  
21      /**
22       * Determines the type (css/js) of the resource from the given path.
23       * @param path - the path to use
24       * @return the type of resource
25       */
26      public static String getType(@NotNull String path)
27      {
28          int index = path.lastIndexOf('.');
29          if (index > -1 && index < path.length())
30              return path.substring(index + 1);
31  
32          return "";
33      }
34  
35      public static void addParamsToUrl(final StringBuilder sb, final Map<String, String> params)
36      {
37          if (params.size() > 0)
38          {
39              sb.append("?");
40              int count = 0;
41  
42              for (final Map.Entry<String, String> entry : params.entrySet())
43              {
44                  try
45                  {
46                      sb.append(URLEncoder.encode(entry.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
47  
48                      if (++count < params.size())
49                      {
50                          sb.append("&");
51                      }
52                  }
53                  catch (final UnsupportedEncodingException e)
54                  {
55                      log.error("Could not encode parameter to url for [" + entry.getKey() + "] with value [" + entry.getValue() + "]", e);
56                  }
57              }
58          }
59      }
60  
61      /**
62       * Returns a Map of query parameters from the request. If there are multiple values for the same
63       * query parameter, the first value is used.
64       *
65       * @see {@link javax.servlet.ServletRequest#getParameterMap()}
66       */
67      public static Map<String, String> getQueryParameters(HttpServletRequest request)
68      {
69          Map<String, String> result = new TreeMap<String, String>();
70          Map<String, String[]> parameters = request.getParameterMap();
71  
72          for (Map.Entry<String, String[]> entry : parameters.entrySet())
73          {
74              if (entry.getValue() != null && entry.getValue().length > 0)
75                  result.put(entry.getKey(), entry.getValue()[0]);
76          }
77  
78          final String hash = (String) request.getAttribute(STATIC_HASH);
79          if (hash != null)
80          {
81              result.put(STATIC_HASH, hash);
82          }
83  
84          return result;
85      }
86  
87      /**
88       * Given a parameter map, determine if a web resource is cacheable.
89       *
90       * This determines whether we store in the file cache or not, as well as dictating 304 behaviour.
91       *
92       * @param params
93       * @return if the PluginResourceDownload.STATIC_HASH is set and cache does not equal false.
94       */
95      public static boolean canRequestedResourcesContentBeAssumedConstant(Map<String, String> params) {
96          final boolean nocache = "false".equals(params.get("cache"));
97          final boolean nohash = !params.containsKey(STATIC_HASH);
98          return !(nohash || nocache);
99      }
100 }