1 package com.atlassian.plugin.webresource;
2
3 import com.atlassian.plugin.servlet.DownloadException;
4 import com.atlassian.plugin.servlet.DownloadableResource;
5 import static com.atlassian.plugin.servlet.AbstractFileServerServlet.PATH_SEPARATOR;
6 import static com.atlassian.plugin.servlet.AbstractFileServerServlet.SERVLET_PATH;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import java.io.OutputStream;
12 import java.util.Map;
13 import java.util.List;
14 import java.util.ArrayList;
15 import java.util.Collections;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20
21
22
23
24
25
26
27
28
29 public class BatchPluginResource implements DownloadableResource, PluginResource
30 {
31 private static final Log log = LogFactory.getLog(BatchPluginResource.class);
32
33
34
35
36 static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + "batch";
37
38 final private String type;
39 final private String moduleCompleteKey;
40 final private Map<String, String> params;
41 final private String resourceName;
42 final private List<DownloadableResource> resources;
43
44
45
46
47
48
49
50 public BatchPluginResource(String moduleCompleteKey, String type, Map<String, String> params)
51 {
52 this(moduleCompleteKey + "." + type, moduleCompleteKey, type, params);
53 }
54
55
56
57
58
59 private BatchPluginResource(String resourceName, String moduleCompleteKey, String type, Map<String, String> params)
60 {
61 this.resourceName = resourceName;
62 this.moduleCompleteKey = moduleCompleteKey;
63 this.type = type;
64 this.params = params;
65 this.resources = new ArrayList<DownloadableResource>();
66 }
67
68
69
70
71 public boolean isEmpty()
72 {
73 return resources.isEmpty();
74 }
75
76 public void add(DownloadableResource resource)
77 {
78 resources.add(resource);
79 }
80
81 public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
82 {
83 for (DownloadableResource resource : resources)
84 {
85 if (resource.isResourceModified(request, response))
86 return true;
87 }
88 return false;
89 }
90
91 public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
92 {
93 log.debug("Start to serve batch " + toString());
94 for (DownloadableResource resource : resources)
95 {
96 resource.serveResource(request, response);
97 }
98 }
99
100 public void streamResource(OutputStream out)
101 {
102 for (DownloadableResource resource : resources)
103 {
104 resource.streamResource(out);
105 }
106 }
107
108 public String getContentType()
109 {
110 String contentType = params.get("content-type");
111 if (contentType != null)
112 return contentType;
113
114 return null;
115 }
116
117
118
119
120
121
122
123 public static BatchPluginResource parse(String url, Map<String, String> queryParams)
124 {
125 int startIndex = url.indexOf(URL_PREFIX) + URL_PREFIX.length() + 1;
126
127 if (url.indexOf('?') != -1)
128 {
129 url = url.substring(0, url.indexOf('?'));
130 }
131
132 String typeAndModuleKey = url.substring(startIndex);
133 String[] parts = typeAndModuleKey.split("/", 2);
134
135 if (parts.length < 2)
136 return null;
137
138 String moduleKey = parts[0];
139 String resourceName = parts[1];
140 String type = resourceName.substring(resourceName.lastIndexOf('.') + 1);
141
142 return new BatchPluginResource(resourceName, moduleKey, type, queryParams);
143 }
144
145 public static boolean matches(String url)
146 {
147 return url.indexOf(URL_PREFIX) != -1;
148 }
149
150
151
152
153
154
155
156
157
158
159
160 public String getUrl()
161 {
162 StringBuilder sb = new StringBuilder();
163 sb.append(URL_PREFIX).append(PATH_SEPARATOR)
164 .append(moduleCompleteKey).append(PATH_SEPARATOR)
165 .append(resourceName);
166
167 if(params.size() > 0 )
168 {
169 sb.append("?");
170 int count = 0;
171
172 for (Map.Entry<String, String> entry: params.entrySet())
173 {
174 sb.append(entry.getKey()).append("=").append(entry.getValue());
175
176 if(++count < params.size())
177 sb.append("&");
178 }
179 }
180
181 return sb.toString();
182 }
183
184 public String getResourceName()
185 {
186 return resourceName;
187 }
188
189 public Map<String, String> getParams()
190 {
191 return Collections.unmodifiableMap(params);
192 }
193
194 public String getModuleCompleteKey()
195 {
196 return moduleCompleteKey;
197 }
198
199 public boolean isCacheSupported()
200 {
201 return !"false".equals(params.get("cache"));
202 }
203
204 public String getType()
205 {
206 return type;
207 }
208
209 public boolean equals(Object o)
210 {
211 if (this == o) return true;
212 if (o == null || getClass() != o.getClass()) return false;
213
214 BatchPluginResource that = (BatchPluginResource) o;
215
216 if (moduleCompleteKey != null ? !moduleCompleteKey.equals(that.moduleCompleteKey) : that.moduleCompleteKey != null)
217 return false;
218 if (params != null ? !params.equals(that.params) : that.params != null) return false;
219 if (type != null ? !type.equals(that.type) : that.type != null) return false;
220
221 return true;
222 }
223
224 public int hashCode()
225 {
226 int result;
227 result = (type != null ? type.hashCode() : 0);
228 result = 31 * result + (moduleCompleteKey != null ? moduleCompleteKey.hashCode() : 0);
229 result = 31 * result + (params != null ? params.hashCode() : 0);
230 return result;
231 }
232
233 public String toString()
234 {
235 return "[moduleCompleteKey=" + moduleCompleteKey + ", type=" + type + ", params=" + params + "]";
236 }
237 }