001package systems.dmx.accesscontrol; 002 003import javax.servlet.http.HttpServletRequest; 004 005import java.util.ArrayList; 006import java.util.List; 007 008 009 010class AnonymousAccessFilter { 011 012 // ---------------------------------------------------------------------------------------------- Instance Variables 013 014 private List<String> prefixesRead; 015 private List<String> prefixesWrite; 016 017 // ---------------------------------------------------------------------------------------------------- Constructors 018 019 AnonymousAccessFilter(String settingRead, String settingWrite) { 020 this.prefixesRead = initPrefixes(settingRead); 021 this.prefixesWrite = initPrefixes(settingWrite); 022 } 023 024 // ----------------------------------------------------------------------------------------- Package Private Methods 025 026 boolean isAnonymousAccessAllowed(HttpServletRequest request) { 027 return request.getMethod().equals("GET") ? 028 prefixMatch(request, prefixesRead) : 029 prefixMatch(request, prefixesWrite); 030 } 031 032 // --- 033 034 String dumpReadSetting() { 035 return dumpSetting(prefixesRead); 036 } 037 038 String dumpWriteSetting() { 039 return dumpSetting(prefixesWrite); 040 } 041 042 // ------------------------------------------------------------------------------------------------- Private Methods 043 044 private List<String> initPrefixes(String setting) { 045 if (setting.equals("ALL")) { 046 return null; 047 } else if (setting.equals("NONE")) { 048 return new ArrayList(); 049 } else { 050 String[] p = setting.split(",\\s*"); // ignore whitespace after comma 051 List<String> prefixes = new ArrayList(); 052 for (int i = 0; i < p.length; i++) { 053 prefixes.add(p[i]); 054 } 055 return prefixes; 056 } 057 } 058 059 private boolean prefixMatch(HttpServletRequest request, List<String> prefixes) { 060 if (prefixes == null) { 061 return true; 062 } 063 for (String prefix : prefixes) { 064 if (request.getRequestURI().startsWith(prefix)) { 065 return true; 066 } 067 } 068 return false; 069 } 070 071 private String dumpSetting(List<String> prefixes) { 072 if (prefixes == null) { 073 return "ALL"; 074 } else if (prefixes.isEmpty()) { 075 return "NONE"; 076 } else { 077 return prefixes.toString(); 078 } 079 } 080}