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

Last change on this file since 28971 was 28971, checked in by kjdon, 10 years ago

removing servletcontext gsdocument attribute. just create a new document when we need one.

  • Property svn:executable set to *
File size: 15.5 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 = XMLConverter.newDOM();
130
131 Element metaMessage = gsDoc.createElement(GSXML.MESSAGE_ELEM);
132 Element metaRequest = GSXML.createBasicRequest(gsDoc, GSXML.REQUEST_TYPE_PROCESS, collection + "/" + METADATA_RETRIEVAL_SERVICE, new UserContext());
133 metaMessage.appendChild(metaRequest);
134
135 Element paramList = gsDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
136 metaRequest.appendChild(paramList);
137
138 Element param = gsDoc.createElement(GSXML.PARAM_ELEM);
139 paramList.appendChild(param);
140
141 param.setAttribute(GSXML.NAME_ATT, "metadata");
142 param.setAttribute(GSXML.VALUE_ATT, "contains");
143
144 Element docList = gsDoc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
145 metaRequest.appendChild(docList);
146
147 Element doc = gsDoc.createElement(GSXML.DOC_NODE_ELEM);
148 docList.appendChild(doc);
149
150 doc.setAttribute(GSXML.NODE_ID_ATT, dir);
151
152 Element metaResponse = (Element) gsRouter.process(metaMessage);
153
154 NodeList metadataList = metaResponse.getElementsByTagName(GSXML.METADATA_ELEM);
155 if (metadataList.getLength() == 0)
156 {
157 _logger.error("Could not find the document related to this url");
158 }
159 else
160 {
161 Element metadata = (Element) metadataList.item(0);
162 String document = metadata.getTextContent();
163
164 //Get the security info for this collection
165 Element securityMessage = gsDoc.createElement(GSXML.MESSAGE_ELEM);
166 Element securityRequest = GSXML.createBasicRequest(gsDoc, GSXML.REQUEST_TYPE_SECURITY, collection, new UserContext());
167 securityMessage.appendChild(securityRequest);
168 if (document != null && !document.equals(""))
169 {
170 securityRequest.setAttribute(GSXML.NODE_OID, document);
171 }
172
173 Element securityResponse = (Element) GSXML.getChildByTagName(gsRouter.process(securityMessage), GSXML.RESPONSE_ELEM);
174 ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
175
176 if (!groups.contains(""))
177 {
178 boolean found = false;
179 for (String group : groups)
180 {
181 if (((HttpServletRequest) request).isUserInRole(group))
182 {
183 found = true;
184 break;
185 }
186 }
187
188 if (!found)
189 {
190 return;
191 }
192 }
193 }
194 }
195 else if (url.contains(INTERFACE_PATH))
196 {
197 String fileURL = url.replaceFirst(request.getServletContext().getContextPath(), "");
198 File requestedFile = new File(request.getServletContext().getRealPath(fileURL));
199 if (!requestedFile.exists())
200 {
201 int interfaceNameStart = fileURL.indexOf(INTERFACE_PATH) + INTERFACE_PATH.length();
202 int interfaceNameEnd = fileURL.indexOf("/", interfaceNameStart);
203 String interfaceName = fileURL.substring(interfaceNameStart, interfaceNameEnd);
204 String interfacesDir = fileURL.substring(0, interfaceNameStart);
205 File interfaceConfigFile = new File(request.getServletContext().getRealPath(interfacesDir + interfaceName + "/interfaceConfig.xml"));
206
207 if (interfaceConfigFile.exists())
208 {
209 Document interfaceConfigDoc = XMLConverter.getDOM(interfaceConfigFile);
210
211 String baseInterface = interfaceConfigDoc.getDocumentElement().getAttribute("baseInterface");
212 if (baseInterface.length() > 0)
213 {
214 File baseInterfaceFile = new File(request.getServletContext().getRealPath(fileURL.replace("/" + interfaceName + "/", "/" + baseInterface + "/")));
215 if (baseInterfaceFile.exists())
216 {
217 ServletOutputStream out = response.getOutputStream();
218 out.write(FileUtils.readFileToByteArray(baseInterfaceFile));
219 out.flush();
220 out.close();
221 return;
222 }
223 }
224 }
225 }
226 }
227 else
228 {
229 //If we have a jsessionid on the end of our URL we want to ignore it
230 int index;
231 if ((index = url.indexOf(";jsessionid")) != -1)
232 {
233 url = url.substring(0, index);
234 }
235 String[] segments = url.split("/");
236 for (int i = 0; i < segments.length; i++)
237 {
238 String[] additionalParameters = null;
239 String[] defaultParamValues = null;
240
241 //COLLECTION
242 if (segments[i].equals(COLLECTION_PATH) && (i + 1) < segments.length)
243 {
244 gRequest.setParameter(GSParams.COLLECTION, segments[i + 1]);
245 }
246 //DOCUMENT
247 else if (segments[i].equals(DOCUMENT_PATH) && (i + 1) < segments.length)
248 {
249 gRequest.setParameter(GSParams.DOCUMENT, segments[i + 1]);
250
251 additionalParameters = new String[] { GSParams.ACTION };
252 defaultParamValues = new String[] { "d" };
253 //additionalParameters = new String[] { GSParams.ACTION, GSParams.DOCUMENT_TYPE };
254 //defaultParamValues = new String[] { "d", "hierarchy" };
255 }
256 //PAGE
257 else if (segments[i].equals(PAGE_PATH) && (i + 1) < segments.length)
258 {
259 gRequest.setParameter(GSParams.SUBACTION, segments[i + 1]);
260
261 additionalParameters = new String[] { GSParams.ACTION };
262 defaultParamValues = new String[] { "p" };
263 }
264 //SYSTEM
265 else if (segments[i].equals(SYSTEM_PATH) && (i + 1) < segments.length)
266 {
267 String sa = segments[i + 1];
268 if (sa.equals(SYSTEM_SUBACTION_CONFIGURE) || sa.equals(SYSTEM_SUBACTION_RECONFIGURE))
269 {
270 sa = "c";
271 }
272 else if (sa.equals(SYSTEM_SUBACTION_ACTIVATE))
273 {
274 sa = "a";
275 }
276 else if (sa.equals(SYSTEM_SUBACTION_DEACTIVATE))
277 {
278 sa = "d";
279 }
280
281 if (sa.equals("c") && (i + 2) < segments.length)
282 {
283 gRequest.setParameter(GSParams.SYSTEM_CLUSTER, segments[i + 2]);
284 }
285
286 if (sa.equals("a") && (i + 2) < segments.length)
287 {
288 gRequest.setParameter(GSParams.SYSTEM_MODULE_TYPE, "collection");
289 gRequest.setParameter(GSParams.SYSTEM_MODULE_NAME, segments[i + 2]);
290 }
291
292 if (sa.equals("d") && (i + 2) < segments.length)
293 {
294 gRequest.setParameter(GSParams.SYSTEM_CLUSTER, segments[i + 2]);
295 }
296
297 gRequest.setParameter(GSParams.SUBACTION, sa);
298
299 additionalParameters = new String[] { GSParams.ACTION };
300 defaultParamValues = new String[] { "s" };
301 }
302 //ADMIN
303 else if (segments[i].equals("admin") && (i + 1) < segments.length)
304 {
305 String pageName = segments[i + 1];
306
307 gRequest.setParameter("s1.authpage", pageName);
308
309 additionalParameters = new String[] { GSParams.ACTION, GSParams.REQUEST_TYPE, GSParams.SUBACTION, GSParams.SERVICE };
310 defaultParamValues = new String[] { "g", "r", "authen", "Authentication" };
311 }
312 //BROWSE
313 else if (segments[i].equals("browse") && (i + 1) < segments.length)
314 {
315 String cl = "";
316 for (int j = 1; (i + j) < segments.length; j++)
317 {
318 String currentSegment = segments[i + j].replace("CL", "").replace("cl", "");
319 if (currentSegment.contains("."))
320 {
321 String[] subsegments = currentSegment.split("\\.");
322 for (String subsegment : subsegments)
323 {
324 subsegment = subsegment.replace("CL", "").replace("cl", "");
325
326 if (cl.length() > 0)
327 {
328 cl += ".";
329 }
330
331 if (subsegment.length() > 0)
332 {
333 cl += subsegment;
334 }
335 }
336 continue;
337 }
338 if (!currentSegment.matches("^(CL|cl)?\\d+$"))
339 {
340 continue;
341 }
342
343 if (cl.length() > 0)
344 {
345 cl += ".";
346 }
347
348 cl += currentSegment;
349 }
350
351 gRequest.setParameter("cl", "CL" + cl);
352
353 additionalParameters = new String[] { GSParams.ACTION, GSParams.REQUEST_TYPE, GSParams.SERVICE };
354 defaultParamValues = new String[] { "b", "s", "ClassifierBrowse" };
355 }
356 //QUERY
357 else if (segments[i].equals("search"))
358 {
359 String serviceName = "";
360 if ((i + 1) < segments.length)
361 {
362 serviceName = segments[i + 1];
363 gRequest.setParameter("s", serviceName);
364
365 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE };
366 defaultParamValues = new String[] { "q", "", "d" };
367 }
368 if ((i + 2) < segments.length)
369 {
370 if (serviceName.equals("TextQuery") || serviceName.equals("RawQuery"))
371 {
372 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.index", "s1.startPage" };
373 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "1" };
374
375 gRequest.setParameter("s1.query", segments[i + 2]);
376 }
377 else if (serviceName.equals("FieldQuery"))
378 {
379 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.fqf", "s1.startPage" };
380 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "1" };
381
382 gRequest.setParameter("s1.fqv", segments[i + 2]);
383 }
384 else if (serviceName.equals("AdvancedFieldQuery"))
385 {
386 additionalParameters = new String[] { GSParams.ACTION, GSParams.SUBACTION, GSParams.REQUEST_TYPE, "s1.maxDocs", "s1.hitsPerPage", "s1.level", "s1.sortBy", "s1.fqf", "s1.fqk", "s1.startPage" };
387 defaultParamValues = new String[] { "q", "", "rd", "100", "20", "Sec", "rank", "ZZ", "0", "1" };
388
389 gRequest.setParameter("s1.fqv", segments[i + 2]);
390 }
391 }
392 }
393 if (additionalParameters != null)
394 {
395 for (int j = 0; j < additionalParameters.length; j++)
396 {
397 if (gRequest.getParameter(additionalParameters[j]) == null)
398 {
399 gRequest.setParameter(additionalParameters[j], defaultParamValues[j]);
400 }
401 }
402 }
403 }
404 }
405
406 chain.doFilter(gRequest, response);
407 }
408 else
409 {
410 //Will this ever happen?
411 System.err.println("The request was not an HttpServletRequest");
412 }
413 }
414
415 private boolean isURLRestricted(String url)
416 {
417 for (String restrictedURL : _restrictedURLs)
418 {
419 if (url.matches(".*" + restrictedURL + ".*"))
420 {
421 return true;
422 }
423 }
424
425 return false;
426 }
427
428 private class GSHttpServletRequestWrapper extends HttpServletRequestWrapper
429 {
430 private HashMap<String, String[]> _newParams = new HashMap<String, String[]>();
431
432 public GSHttpServletRequestWrapper(ServletRequest request)
433 {
434 super((HttpServletRequest) request);
435 }
436
437 public void setParameter(String paramName, String[] paramValues)
438 {
439 _newParams.put(paramName, paramValues);
440 }
441
442 public void setParameter(String paramName, String paramValue)
443 {
444 _newParams.put(paramName, new String[] { paramValue });
445 }
446
447 public String getParameter(String paramName)
448 {
449 if (super.getParameter(paramName) != null)
450 {
451 return super.getParameter(paramName);
452 }
453 else
454 {
455 if (_newParams.get(paramName) != null && _newParams.get(paramName)[0] != null)
456 {
457 return _newParams.get(paramName)[0];
458 }
459 return null;
460 }
461 }
462
463 public String[] getParameterValues(String paramName)
464 {
465 if (super.getParameterValues(paramName) != null)
466 {
467 return super.getParameterValues(paramName);
468 }
469 else
470 {
471 return _newParams.get(paramName);
472 }
473 }
474
475 public Map<String, String[]> getParameterMap()
476 {
477 HashMap<String, String[]> returnMap = new HashMap<String, String[]>();
478 returnMap.putAll(super.getParameterMap());
479 returnMap.putAll(_newParams);
480 return returnMap;
481 }
482 }
483}
Note: See TracBrowser for help on using the repository browser.