Mercurial Hosting > nabble
comparison src/nabble/view/lib/UrlMapperImpl.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.view.lib; | |
2 | |
3 import java.io.IOException; | |
4 import java.util.Collections; | |
5 import java.util.Map; | |
6 import java.util.regex.Pattern; | |
7 import javax.servlet.http.HttpServlet; | |
8 import javax.servlet.http.HttpServletRequest; | |
9 import javax.servlet.http.HttpServletResponse; | |
10 import fschmidt.util.servlet.JtpContext; | |
11 import fschmidt.util.servlet.UrlMapper; | |
12 import fschmidt.util.servlet.UrlMapping; | |
13 import nabble.view.web.template.UrlMapperNamespace; | |
14 | |
15 | |
16 public final class UrlMapperImpl implements UrlMapper { | |
17 | |
18 private static class RegexToClass { | |
19 private final Pattern ptn; | |
20 private final Class cls; | |
21 | |
22 public RegexToClass(Class cls) { | |
23 if( !UrlMappable.class.isAssignableFrom(cls) ) | |
24 throw new RuntimeException(cls.getName()+" doesn't implement UrlMappable"); | |
25 this.cls = cls; | |
26 try { | |
27 this.ptn = ((UrlMappable)cls.newInstance()).getUrlPattern(); | |
28 } catch(InstantiationException e) { | |
29 throw new RuntimeException(e); | |
30 } catch(IllegalAccessException e) { | |
31 throw new RuntimeException(e); | |
32 } | |
33 } | |
34 } | |
35 | |
36 private final RegexToClass[] regexToClass; | |
37 | |
38 UrlMapperImpl(final Class[] classes) { | |
39 RegexToClass[] regexToClass = new RegexToClass[classes.length]; | |
40 for( int i=0; i<classes.length; i++ ) { | |
41 regexToClass[i] = new RegexToClass(classes[i]); | |
42 } | |
43 this.regexToClass = regexToClass; | |
44 } | |
45 | |
46 public UrlMapping getUrlMapping(HttpServletRequest request) { | |
47 String url = request.getRequestURL().toString(); | |
48 /* | |
49 if( Jtp.getSite(request) == null && !url.startsWith(Jtp.defaultContextUrl()) ) | |
50 return redir; | |
51 */ | |
52 UrlMapping urlMapping = UrlMapperNamespace.getUrlMapping(request); | |
53 if( urlMapping != null ) | |
54 return urlMapping; | |
55 int i = url.indexOf(';'); | |
56 if( i != -1 ) | |
57 url = url.substring(0,i); | |
58 for (RegexToClass rc : regexToClass) { | |
59 if (rc.ptn.matcher(url).find()) { | |
60 try { | |
61 UrlMappable ubm = (UrlMappable)rc.cls.newInstance(); | |
62 Map<String,String[]> params = ubm.getParameterMapFromUrl(request,url); | |
63 return new UrlMapping(rc.cls,params); | |
64 } catch(IllegalAccessException e) { | |
65 throw new RuntimeException(e); | |
66 } catch(InstantiationException e) { | |
67 throw new RuntimeException(e); | |
68 } | |
69 } | |
70 } | |
71 return null; | |
72 } | |
73 /* | |
74 public static class Redir extends HttpServlet { | |
75 protected void service(HttpServletRequest request,HttpServletResponse response) | |
76 throws IOException | |
77 { | |
78 response.setHeader("Location", "http://www.jmeter-archive.org/missing.html"); | |
79 response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY); | |
80 } | |
81 } | |
82 private static final UrlMapping redir = new UrlMapping(Redir.class,Collections.emptyMap()); | |
83 */ | |
84 } |