1 package com.atlassian.plugin.webresource;
2
3 import com.atlassian.plugin.ModuleDescriptor;
4
5 import java.io.Writer;
6
7 /**
8 * Manage 'css', 'javascript' and other 'resources' that are usually linked at the top of pages using
9 * <code><script></code> and <code><link> tags.
10 * <p/>
11 * By using the WebResourceManager, components can declare dependencies on javascript and css that they would
12 * otherwise have to embed inline (which means that it can't be cached, and are often duplicated in a page).
13 */
14 public interface WebResourceManager
15 {
16 /**
17 * Indicates to that a given plugin web resource is required. All resources called via this method must be
18 * included when {@link #includeResources(Writer)} is called.
19 *
20 * @param moduleCompleteKey The fully qualified plugin web resource module (eg <code>jira.webresources:scriptaculous</code>)
21 * @see #includeResources(Writer)
22 */
23 public void requireResource(String moduleCompleteKey);
24
25 /**
26 * Writes out the resource tags to the previously required resources called via {@link #requireResource(String)}.
27 * If you need it as a String to embed the tags in a template, use {@link #getRequiredResources()}.
28 *
29 * <p/>
30 * Example - if a 'javascript' resource has been required earlier with requireResource(), this method should output:
31 * <pre><code>
32 * <script type="text/javascript" src="$contextPath/scripts/javascript.js"></script>
33 * </code></pre>
34 * Similarly for other supported resources
35 *
36 * @param writer The writer to write the links to
37 */
38 public void includeResources(Writer writer);
39
40 /**
41 * @see {@link #includeResources(Writer)}
42 */
43 public String getRequiredResources();
44
45 /**
46 * Writes the resource tags of the specified resource to the writer.
47 * If you need it as a String to embed the tags in a template, use {@link #getRequiredResources()}.
48 *
49 * @param moduleCompleteKey The fully qualified plugin web resource module (eg <code>jira.webresources:scriptaculous</code>)
50 * @param writer The writer to write the resource tags to.
51 */
52 public void requireResource(String moduleCompleteKey, Writer writer);
53
54 /**
55 * @see {@link #requireResource(String, Writer)}
56 * @since 2.2
57 */
58 public String getResourceTags(String moduleCompleteKey);
59
60 /**
61 * A helper method to return a prefix for 'system' static resources. Generally the implementation will return
62 * <p/>
63 * <pre><code>/s/{build num}/{system counter}/_</code></pre>
64 * <p/>
65 * Note that the servlet context is prepended, and there is no trailing slash.
66 * <p/>
67 * <p/>
68 * Typical usage is to replace:
69 * <p/>
70 * <pre><code><%= request.getContextPath() %>/styles/global.css</code></pre>
71 * with
72 * <pre><code><%= webResourceManager.getStaticResourcePrefix() %>/styles/global.css</code></pre>
73 *
74 * @return A prefix that can be used to prefix 'static system' resources.
75 */
76 public String getStaticResourcePrefix();
77
78 /**
79 * A helper method to return a prefix for 'system' static resources. This method should be used for
80 * resources that change more frequently than system resources, and therefore have their own resource counter.
81 * <p/>
82 * Generally the implementation will return
83 * <p/>
84 * <pre><code>/s/{build num}/{system counter}/{resource counter}/_</code></pre>
85 * <p/>
86 * Note that the servlet context is prepended, and there is no trailing slash.
87 * <p/>
88 * <p/>
89 * Typical usage is to replace:
90 * <p/>
91 * <pre><code><%= request.getContextPath() %>/styles/global.css</code></pre>
92 * with
93 * <pre><code><%= webResourceManager.getStaticResourcePrefix(resourceCounter) %>/styles/global.css</code></pre>
94 *
95 * @param resourceCounter A number that represents the unique version of the resource you require. Every time this
96 * resource changes, you need to increment the resource counter
97 * @return A prefix that can be used to prefix 'static system' resources.
98 */
99 public String getStaticResourcePrefix(String resourceCounter);
100
101 /**
102 * A helper method to return a url for 'plugin' resources. Generally the implementation will return
103 * <p/>
104 * <pre><code>/s/{build num}/{system counter}/{plugin version}/_/download/resources/plugin.key:module.key/resource.name</code></pre>
105 * <p/>
106 * Note that the servlet context is prepended, and there is no trailing slash.
107 * <p/>
108 * <p/>
109 * Typical usage is to replace:
110 * <p/>
111 * <pre><code><%= request.getContextPath() %>/download/resources/plugin.key:module.key/resource.name</code></pre>
112 * with
113 * <pre><code><%= webResourceManager.getStaticPluginResource(descriptor, resourceName) %></code></pre>
114 *
115 * @param moduleCompleteKey complete plugin module key
116 * @return A url that can be used to request 'plugin' resources.
117 */
118 public String getStaticPluginResource(String moduleCompleteKey, String resourceName);
119
120
121 /**
122 * @see {@link #getStaticPluginResource(String, String)}
123 * @param pluginModuleKey complete plugin module key
124 * @return returns the url of this plugin resource
125 */
126 public String getStaticPluginResource(ModuleDescriptor moduleDescriptor, String resourceName);
127
128
129 // Deprecated methods
130
131 /**
132 * @deprecated Use #getStaticPluginResource instead
133 */
134 public String getStaticPluginResourcePrefix(ModuleDescriptor moduleDescriptor, String resourceName);
135
136 /**
137 * Whether resources should be included inline, or at the top of the page. In most cases, you want to leave this
138 * as the default. However, for pages that don't have a decorator, you will not be able to 'delay' including
139 * the resources (css, javascript), and therefore need to include them directly inline.
140 *
141 * @param includeMode If there is no decorator for this request, set this to be {@link #INLINE_INCLUDE_MODE}
142 * @see #DELAYED_INCLUDE_MODE
143 * @see #INLINE_INCLUDE_MODE
144 * @deprecated Since 2.2.
145 */
146 public void setIncludeMode(IncludeMode includeMode);
147
148 /**
149 * @deprecated Since 2.2. Use {@link #writeResourceTags(String, Writer)} instead.
150 */
151 public static final IncludeMode DELAYED_INCLUDE_MODE = new IncludeMode()
152 {
153 public String getModeName()
154 {
155 return "delayed";
156 }
157 };
158
159 /**
160 * @deprecated Since 2.2. Use {@link #requireResource(String)} instead.
161 */
162 public static final IncludeMode INLINE_INCLUDE_MODE = new IncludeMode()
163 {
164 public String getModeName()
165 {
166 return "inline";
167 }
168 };
169
170 /**
171 * @deprecated Since 2.2
172 */
173 public static interface IncludeMode
174 {
175 public String getModeName();
176 }
177
178 }