source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/URLFilter.java@ 28280

Last change on this file since 28280 was 28280, checked in by sjm84, 11 years ago

Possible fix to an issue I was having where parts of some files were overwriting parts of other files

  • Property svn:executable set to *
File size: 15.7 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.Map;
8
9import javax.servlet.Filter;
10import javax.servlet.FilterChain;
11import javax.servlet.FilterConfig;
12import javax.servlet.ServletException;
13import javax.servlet.ServletOutputStream;
14import javax.servlet.ServletRequest;
15import javax.servlet.ServletResponse;
16import javax.servlet.http.HttpServletRequest;
17import javax.servlet.http.HttpServletRequestWrapper;
18
19import org.apache.commons.io.FileUtils;
20import org.apache.log4j.Logger;
21import org.greenstone.gsdl3.util.GSParams;
22import org.greenstone.gsdl3.util.GSXML;
23import org.greenstone.gsdl3.util.UserContext;
24import org.greenstone.gsdl3.util.XMLConverter;
25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
27import org.w3c.dom.NodeList;
28
29public class URLFilter implements Filter
30{
31 private FilterConfig _filterConfig = null;
32 private static Logger _logger = Logger.getLogger(org.greenstone.gsdl3.core.URLFilter.class.getName());
33
34 //Restricted URLs
35 protected static final String SITECONFIG_URL = "sites/[^/]+/siteConfig.xml";
36 protected static final String USERS_DB_URL = "etc/usersDB/.*";
37 protected static final ArrayList<String> _restrictedURLs;
38 static
39 {
40 ArrayList<String> restrictedURLs = new ArrayList<String>();
41 restrictedURLs.add(SITECONFIG_URL);
42 restrictedURLs.add(USERS_DB_URL);
43 _restrictedURLs = restrictedURLs;
44 }
45
46 //Constants
47 protected static final String DOCUMENT_PATH = "document";
48 protected static final String COLLECTION_PATH = "collection";
49 protected static final String PAGE_PATH = "page";
50 protected static final String SYSTEM_PATH = "system";
51
52 protected static final String METADATA_RETRIEVAL_SERVICE = "DocumentMetadataRetrieve";
53 protected static final String ASSOCIATED_FILE_PATH = "/index/assoc/";
54 protected static final String COLLECTION_FILE_PATH = "/collect/";
55 protected static final String INTERFACE_PATH = "/interfaces/";
56
57 protected static final String SYSTEM_SUBACTION_CONFIGURE = "configure";
58 protected static final String SYSTEM_SUBACTION_RECONFIGURE = "reconfigure";
59 protected static final String SYSTEM_SUBACTION_ACTIVATE = "activate";
60 protected static final String SYSTEM_SUBACTION_DEACTIVATE = "deactivate";
61
62 public void init(FilterConfig filterConfig) throws ServletException
63 {
64 this._filterConfig = filterConfig;
65 }
66
67 public void destroy()
68 {
69 this._filterConfig = null;
70 }
71
72 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
73 {
74 if (request instanceof HttpServletRequest)
75 {
76 HttpServletRequest hRequest = ((HttpServletRequest) request);
77 GSHttpServletRequestWrapper gRequest = new GSHttpServletRequestWrapper(hRequest);
78
79 String url = hRequest.getRequestURI().toString();
80
81 if (isURLRestricted(url))
82 {
83 response.getWriter().println("Access to this page is forbidden.");
84 return;
85 }
86
87 //If the user is trying to access a collection file we need to run a security check
88 if (url.contains(ASSOCIATED_FILE_PATH))
89 {
90 String dir = null;
91 int dirStart = url.indexOf(ASSOCIATED_FILE_PATH) + ASSOCIATED_FILE_PATH.length();
92 int dirEnd = -1;
93 if (dirStart < url.length() && url.indexOf("/", dirStart) != -1)
94 {
95 dirEnd = url.indexOf("/", dirStart);
96 }
97 if (dirEnd != -1)
98 {
99 dir = url.substring(dirStart, dirEnd);
100 }
101 if (dir == null)
102 {
103 return;
104 }
105
106 String collection = null;
107 int colStart = url.indexOf(COLLECTION_FILE_PATH) + COLLECTION_FILE_PATH.length();
108 int colEnd = -1;
109 if (colStart < url.length() && url.indexOf("/", colStart) != -1)
110 {
111 colEnd = url.indexOf("/", colStart);
112 }
113 if (colEnd != -1)
114 {
115 collection = url.substring(colStart, colEnd);
116 }
117 if (collection == null)
118 {
119 return;
120 }
121
122 MessageRouter gsRouter = (MessageRouter) request.getServletContext().getAttribute("GSRouter");
123 if (gsRouter == null)
124 {
125 _logger.error("Receptionist is null, stopping filter");
126 return;
127 }
128
129 Document gsDoc = (Document) request.getServletContext().getAttribute("GSDocument");
130 if (gsDoc == null)
131 {
132 _logger.error("Document is null, stopping filter");
133 return;
134 }
135
136 Element metaMessage = gsDoc.createElement(GSXML.MESSAGE_ELEM);
137 Element metaRequest = GSXML.createBasicRequest(gsDoc, GSXML.REQUEST_TYPE_PROCESS, collection + "/" + METADATA_RETRIEVAL_SERVICE, new UserContext());
138 metaMessage.appendChild(metaRequest);
139
140 Element paramList = gsDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
141 metaRequest.appendChild(paramList);
142
143 Element param = gsDoc.createElement(GSXML.PARAM_ELEM);
144 paramList.appendChild(param);
145
146 param.setAttribute(GSXML.NAME_ATT, "metadata");
147 param.setAttribute(GSXML.VALUE_ATT, "contains");
148
149 Element docList = gsDoc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
150 metaRequest.appendChild(docList);
151
152 Element doc = gsDoc.createElement(GSXML.DOC_NODE_ELEM);
153 docList.appendChild(doc);
154
155 doc.setAttribute(GSXML.NODE_ID_ATT, dir);
156
157 Element metaResponse = (Element) gsRouter.process(metaMessage);
158
159 NodeList metadataList = metaResponse.getElementsByTagName(GSXML.METADATA_ELEM);
160 if (metadataList.getLength() == 0)
161 {
162 _logger.error("Could not find the document related to this url");
163 }
164 else
165 {
166 Element metadata = (Element) metadataList.item(0);
167 String document = metadata.getTextContent();
168
169 //Get the security info for this collection
170 Element securityMessage = gsDoc.createElement(GSXML.MESSAGE_ELEM);
171 Element securityRequest = GSXML.createBasicRequest(gsDoc, GSXML.REQUEST_TYPE_SECURITY, collection, new UserContext());
172 securityMessage.appendChild(securityRequest);
173 if (document != null && !document.equals(""))
174 {
175 securityRequest.setAttribute(GSXML.NODE_OID, document);
176 }
177
178 Element securityResponse = (Element) GSXML.getChildByTagName(gsRouter.process(securityMessage), GSXML.RESPONSE_ELEM);
179 ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
180
181 if (!groups.contains(""))
182 {
183 boolean found = false;
184 for (String group : groups)
185 {
186 if (((HttpServletRequest) request).isUserInRole(group))
187 {
188 found = true;
189 break;
190 }
191 }
192
193 if (!found)
194 {
195 return;
196 }
197 }
198 }
199 }
200 else if (url.contains(INTERFACE_PATH))
201 {
202 String fileURL = url.replaceFirst(request.getServletContext().getContextPath(), "");
203 File requestedFile = new File(request.getServletContext().getRealPath(fileURL));
204 if (!requestedFile.exists())
205 {
206 int interfaceNameStart = fileURL.indexOf(INTERFACE_PATH) + INTERFACE_PATH.length();
207 int interfaceNameEnd = fileURL.indexOf("/", interfaceNameStart);
208 String interfaceName = fileURL.substring(interfaceNameStart, interfaceNameEnd);
209 String interfacesDir = fileURL.substring(0, interfaceNameStart);
210 File interfaceConfigFile = new File(request.getServletContext().getRealPath(interfacesDir + interfaceName + "/interfaceConfig.xml"));
211
212 if (interfaceConfigFile.exists())
213 {
214 XMLConverter xmlC = new XMLConverter();
215 Document interfaceConfigDoc = xmlC.getDOM(interfaceConfigFile);
216
217 String baseInterface = interfaceConfigDoc.getDocumentElement().getAttribute("baseInterface");
218 if (baseInterface.length() > 0)
219 {
220 File baseInterfaceFile = new File(request.getServletContext().getRealPath(fileURL.replace("/" + interfaceName + "/", "/" + baseInterface + "/")));
221 if (baseInterfaceFile.exists())
222 {
223 ServletOutputStream out = response.getOutputStream();
224 out.write(FileUtils.readFileToByteArray(baseInterfaceFile));
225 out.flush();
226 out.close();
227 return;
228 }
229 }
230 }
231 }
232 }
233 else
234 {
235 //If we have a jsessionid on the end of our URL we want to ignore it
236 int index;
237 if ((index = url.indexOf(";jsessionid")) != -1)
238 {
239 url = url.substring(0, index);
240 }
241 String[] segments = url.split("/");
242 for (int i = 0; i < segments.length; i++)
243 {
244 String[] additionalParameters = null;
245 String[] defaultParamValues = null;
246
247 //COLLECTION
248 if (segments[i].equals(COLLECTION_PATH) && (i + 1) < segments.length)
249 {
250 gRequest.setParameter(GSParams.COLLECTION, segments[i + 1]);
251 }
252 //DOCUMENT
253 else if (segments[i].equals(DOCUMENT_PATH) && (i + 1) < segments.length)
254 {
255 gRequest.setParameter(GSParams.DOCUMENT, segments[i + 1]);
256
257 additionalParameters = new String[] { GSParams.ACTION };
258 defaultParamValues = new String[] { "d" };
259 //additionalParameters = new String[] { GSParams.ACTION, GSParams.DOCUMENT_TYPE };
260 //defaultParamValues = new String[] { "d", "hierarchy" };
261 }
262 //PAGE
263 else if (segments[i].equals(PAGE_PATH) && (i + 1) < segments.length)
264 {
265 gRequest.setParameter(GSParams.SUBACTION, segments[i + 1]);
266
267 additionalParameters = new String[] { GSParams.ACTION };
268 defaultParamValues = new String[] { "p" };
269 }
270 //SYSTEM
271 else if (segments[i].equals(SYSTEM_PATH) && (i + 1) < segments.length)
272 {
273 String sa = segments[i + 1];
274 if (sa.equals(SYSTEM_SUBACTION_CONFIGURE) || sa.equals(SYSTEM_SUBACTION_RECONFIGURE))
275 {
276 sa = "c";
277 }
278 else if (sa.equals(SYSTEM_SUBACTION_ACTIVATE))
279 {
280 sa = "a";
281 }
282 else if (sa.equals(SYSTEM_SUBACTION_DEACTIVATE))
283 {
284 sa = "d";
285 }
286
287 if (sa.equals("c") && (i + 2) < segments.length)
288 {
289 gRequest.setParameter(GSParams.SYSTEM_CLUSTER, segments[i + 2]);
290 }
291
292 if (sa.equals("a") && (i + 2) < segments.length)
293 {
294 gRequest.setParameter(GSParams.SYSTEM_MODULE_TYPE, "collection");
295 gRequest.setParameter(GSParams.SYSTEM_MODULE_NAME, segments[i + 2]);
296 }
297
298 if (sa.equals("d") && (i + 2) < segments.length)
299 {
300 gRequest.setParameter(GSParams.SYSTEM_CLUSTER, segments[i + 2]);
301 }
302
303 gRequest.setParameter(GSParams.SUBACTION, sa);
304
305 additionalParameters = new String[] { GSParams.ACTION };
306 defaultParamValues = new String[] { "s" };
307 }
308 //ADMIN
309 else if (segments[i].equals("admin") && (i + 1) < segments.length)
310 {
311 String pageName = segments[i + 1];
312
313 gRequest.setParameter("s1.authpage", pageName);
314
315 additionalParameters = new String[] { GSParams.ACTION, GSParams.REQUEST_TYPE, GSParams.SUBACTION, GSParams.SERVICE };
316 defaultParamValues = new String[] { "g", "r", "authen", "Authentication" };
317 }
318 //BROWSE
319 else if (segments[i].equals("browse") && (i + 1) < segments.length)
320 {
321 String cl = "";
322 for (int j = 1; (i + j) < segments.length; j++)
323 {
324 String currentSegment = segments[i + j].replace("CL", "").replace("cl", "");
325 if (currentSegment.contains("."))
326 {
327 String[] subsegments = currentSegment.split("\\.");
328 for (String subsegment : subsegments)
329 {
330 subsegment = subsegment.replace("CL", "").replace("cl", "");
331
332 if (cl.length() > 0)
333 {
334 cl += ".";
335 }
336
337 if (subsegment.length() > 0)
338 {
339 cl += subsegment;
340 }
341 }
342 continue;
343 }
344 if (!currentSegment.matches("^(CL|cl)?\\d+$"))
345 {
346 continue;
347 }
348
349 if (cl.length() > 0)
350 {
351 cl += ".";
352 }
353
354 cl += currentSegment;
355 }
356
357 gRequest.setParameter("cl", "CL" + cl);
358
359 additionalParameters = new String[] { GSParams.ACTION, GSParams.REQUEST_TYPE, GSParams.SERVICE };
360 defaultParamValues = new String[] { "b", "s", "ClassifierBrowse" };
361 }
362 //QUERY
363 else if (segments[i].equals("search"))
364 {
365 String serviceName = "";
366 if ((i + 1) < segments.length)
367 {
368 serviceName = segments[i + 1];
369 gRequest.setParameter("s", serviceName);
370
371 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE };
372 defaultParamValues = new String[] { "q", "", "d" };
373 }
374 if ((i + 2) < segments.length)
375 {
376 if (serviceName.equals("TextQuery") || serviceName.equals("RawQuery"))
377 {
378 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.index", "s1.startPage" };
379 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "1" };
380
381 gRequest.setParameter("s1.query", segments[i + 2]);
382 }
383 else if (serviceName.equals("FieldQuery"))
384 {
385 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.fqf", "s1.startPage" };
386 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "1" };
387
388 gRequest.setParameter("s1.fqv", segments[i + 2]);
389 }
390 else if (serviceName.equals("AdvancedFieldQuery"))
391 {
392 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.fqf", "s1.fqk", "s1.startPage" };
393 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "0", "1" };
394
395 gRequest.setParameter("s1.fqv", segments[i + 2]);
396 }
397 }
398 }
399 if (additionalParameters != null)
400 {
401 for (int j = 0; j < additionalParameters.length; j++)
402 {
403 if (gRequest.getParameter(additionalParameters[j]) == null)
404 {
405 gRequest.setParameter(additionalParameters[j], defaultParamValues[j]);
406 }
407 }
408 }
409 }
410 }
411
412 chain.doFilter(gRequest, response);
413 }
414 else
415 {
416 //Will this ever happen?
417 System.err.println("The request was not an HttpServletRequest");
418 }
419 }
420
421 private boolean isURLRestricted(String url)
422 {
423 for (String restrictedURL : _restrictedURLs)
424 {
425 if (url.matches(".*" + restrictedURL + ".*"))
426 {
427 return true;
428 }
429 }
430
431 return false;
432 }
433
434 private class GSHttpServletRequestWrapper extends HttpServletRequestWrapper
435 {
436 private HashMap<String, String[]> _newParams = new HashMap<String, String[]>();
437
438 public GSHttpServletRequestWrapper(ServletRequest request)
439 {
440 super((HttpServletRequest) request);
441 }
442
443 public void setParameter(String paramName, String[] paramValues)
444 {
445 _newParams.put(paramName, paramValues);
446 }
447
448 public void setParameter(String paramName, String paramValue)
449 {
450 _newParams.put(paramName, new String[] { paramValue });
451 }
452
453 public String getParameter(String paramName)
454 {
455 if (super.getParameter(paramName) != null)
456 {
457 return super.getParameter(paramName);
458 }
459 else
460 {
461 if (_newParams.get(paramName) != null && _newParams.get(paramName)[0] != null)
462 {
463 return _newParams.get(paramName)[0];
464 }
465 return null;
466 }
467 }
468
469 public String[] getParameterValues(String paramName)
470 {
471 if (super.getParameterValues(paramName) != null)
472 {
473 return super.getParameterValues(paramName);
474 }
475 else
476 {
477 return _newParams.get(paramName);
478 }
479 }
480
481 public Map<String, String[]> getParameterMap()
482 {
483 HashMap<String, String[]> returnMap = new HashMap<String, String[]>();
484 returnMap.putAll(super.getParameterMap());
485 returnMap.putAll(_newParams);
486 return returnMap;
487 }
488 }
489}
Note: See TracBrowser for help on using the repository browser.