Mercurial Hosting > nabble
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/lib/UrlMapperImpl.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,84 @@ +package nabble.view.lib; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.regex.Pattern; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import fschmidt.util.servlet.JtpContext; +import fschmidt.util.servlet.UrlMapper; +import fschmidt.util.servlet.UrlMapping; +import nabble.view.web.template.UrlMapperNamespace; + + +public final class UrlMapperImpl implements UrlMapper { + + private static class RegexToClass { + private final Pattern ptn; + private final Class cls; + + public RegexToClass(Class cls) { + if( !UrlMappable.class.isAssignableFrom(cls) ) + throw new RuntimeException(cls.getName()+" doesn't implement UrlMappable"); + this.cls = cls; + try { + this.ptn = ((UrlMappable)cls.newInstance()).getUrlPattern(); + } catch(InstantiationException e) { + throw new RuntimeException(e); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + private final RegexToClass[] regexToClass; + + UrlMapperImpl(final Class[] classes) { + RegexToClass[] regexToClass = new RegexToClass[classes.length]; + for( int i=0; i<classes.length; i++ ) { + regexToClass[i] = new RegexToClass(classes[i]); + } + this.regexToClass = regexToClass; + } + + public UrlMapping getUrlMapping(HttpServletRequest request) { + String url = request.getRequestURL().toString(); +/* + if( Jtp.getSite(request) == null && !url.startsWith(Jtp.defaultContextUrl()) ) + return redir; +*/ + UrlMapping urlMapping = UrlMapperNamespace.getUrlMapping(request); + if( urlMapping != null ) + return urlMapping; + int i = url.indexOf(';'); + if( i != -1 ) + url = url.substring(0,i); + for (RegexToClass rc : regexToClass) { + if (rc.ptn.matcher(url).find()) { + try { + UrlMappable ubm = (UrlMappable)rc.cls.newInstance(); + Map<String,String[]> params = ubm.getParameterMapFromUrl(request,url); + return new UrlMapping(rc.cls,params); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } catch(InstantiationException e) { + throw new RuntimeException(e); + } + } + } + return null; + } +/* + public static class Redir extends HttpServlet { + protected void service(HttpServletRequest request,HttpServletResponse response) + throws IOException + { + response.setHeader("Location", "http://www.jmeter-archive.org/missing.html"); + response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY); + } + } + private static final UrlMapping redir = new UrlMapping(Redir.class,Collections.emptyMap()); +*/ +}