Changeset 672
- Timestamp:
- 2008-10-06 15:02:40 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kauri-routing/kauri-routing-impl/src/main/java/org/kauriproject/routing/impl/components/DirectoryRouter.java
r671 r672 12 12 import org.kauriproject.runtime.rapi.ModuleSource.Resource; 13 13 import org.kauriproject.template.service.TemplateService; 14 import org.apache.commons.logging.LogFactory; 15 import org.apache.commons.logging.Log; 14 16 15 17 import java.util.*; 18 import java.util.regex.Pattern; 19 import java.util.regex.Matcher; 16 20 17 21 /** … … 26 30 private TemplateService templateService; 27 31 private Context restletContext; 32 private Log log = LogFactory.getLog(getClass()); 33 34 private static Pattern DOUBLE_EXT = Pattern.compile("^(.*)\\.([^.]*)\\.([^.]+)$"); 28 35 29 36 public DirectoryRouter(String path, Context restletContext, KauriModule module, TemplateService templateService) { … … 41 48 42 49 private void build() { 43 List< String> paths = new ArrayList<String>();44 scan(path, "", pa ths);50 List<Page> pages = new ArrayList<Page>(); 51 scan(path, "", pages); 45 52 46 53 // The "best" matching algorithm of Restlet's Router cannot meaningfully … … 49 56 // therefore we sort the paths on beforehand on length, since Restlet 50 57 // will use the first route with best/maximal score. 51 Collections.sort(pa ths, new Comparator<String>() {52 public int compare( String o1, String o2) {53 if ( o1.length() == o2.length())58 Collections.sort(pages, new Comparator<Page>() { 59 public int compare(Page p1, Page p2) { 60 if (p1.patternSignificantLength == p2.patternSignificantLength) 54 61 return 0; 55 62 else 56 return o1.length() < o2.length()? 1 : -1;63 return p1.patternSignificantLength < p2.patternSignificantLength ? 1 : -1; 57 64 } 58 65 }); 59 66 60 67 Router router = new Router(); 61 for (String path : paths) { 62 String templatePath = "kms:/" + this. path + path; 63 router.attach(path, new TemplateRestlet(templateService, restletContext, templatePath)); 68 for (Page page : pages) { 69 if (log.isDebugEnabled()) { 70 log.info("Attaching URI pattern " + page.uriPattern + " for template " + page.templatePath); 71 } 72 router.attach(page.uriPattern, new TemplateRestlet(templateService, restletContext, page.templatePath)); 64 73 } 65 74 this.router = router; 66 75 } 67 76 68 private void scan(String parent, String mountPath, Collection< String> allPaths) {77 private void scan(String parent, String mountPath, Collection<Page> pages) { 69 78 Resource res = moduleSource.getResource(parent); 70 79 … … 74 83 Resource childRes = moduleSource.getResource(childPath); 75 84 if (childRes.isDirectory()) { 76 scan(childPath, mountPath + "/" + child, allPaths);85 scan(childPath, mountPath + "/" + child, pages); 77 86 } else { 78 allPaths.add(mountPath + "/" + child);87 pages.add(createPage(mountPath + "/" + child)); 79 88 } 80 89 } … … 101 110 } 102 111 } 112 113 private Page createPage(String filePath) { 114 String templatePath = "kms:/" + this.path + filePath; 115 Matcher matcher = DOUBLE_EXT.matcher(filePath); 116 if (matcher.matches()) { 117 String base = matcher.group(1); 118 String publicExt = matcher.group(2); 119 String uriPattern = publicExt.length() > 0 ? base + "." + publicExt : base; 120 return new Page(uriPattern, templatePath); 121 } else { 122 return new Page(filePath, templatePath); 123 } 124 } 125 126 private static class Page { 127 String uriPattern; 128 String templatePath; 129 int patternSignificantLength; 130 131 public Page(String uriPattern, String templatePath) { 132 this.uriPattern = uriPattern; 133 this.templatePath = templatePath; 134 this.patternSignificantLength = uriPattern.replaceAll("\\{[^{}]*}", Matcher.quoteReplacement("{}")).length(); 135 } 136 } 103 137 }
Note: See TracChangeset
for help on using the changeset viewer.