1 24 package org.riotfamily.common.web.mapping; 25 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.riotfamily.common.beans.MapWrapper; 33 import org.springframework.beans.BeanWrapperImpl; 34 import org.springframework.web.servlet.handler.AbstractHandlerMapping; 35 36 40 public abstract class AbstractReverseHandlerMapping 41 extends AbstractHandlerMapping implements ReverseHandlerMapping { 42 43 protected abstract List getPatternsForHandler(String beanName, 44 HttpServletRequest request); 45 46 protected String addServletMappingIfNecessary(String path, 47 HttpServletRequest request) { 48 49 return path; 50 } 51 52 protected AttributePattern getPatternForHandler(String handlerName, 53 HttpServletRequest request, int numberOfWildcards) { 54 55 List patterns = getPatternsForHandler(handlerName, request); 56 if (patterns == null || patterns.isEmpty()) { 57 return null; 58 } 59 Iterator it = patterns.iterator(); 60 while (it.hasNext()) { 61 AttributePattern p = (AttributePattern) it.next(); 62 if (p.getNumberOfWildcards() != numberOfWildcards) { 63 it.remove(); 64 } 65 } 66 if (patterns.size() != 1) { 67 throw new IllegalArgumentException ("Exactly one mapping with " 68 + numberOfWildcards + " wildcards required for hander " 69 + handlerName); 70 } 71 return (AttributePattern) patterns.get(0); 72 } 73 74 protected AttributePattern getPatternForHandler(String handlerName, 75 HttpServletRequest request) { 76 77 List patterns = getPatternsForHandler(handlerName, request); 78 if (patterns == null || patterns.isEmpty()) { 79 return null; 80 } 81 if (patterns.size() > 1) { 82 throw new IllegalArgumentException ("Ambigious mapping - more than " 83 + "one pattern is registered for hander " + handlerName); 84 } 85 return (AttributePattern) patterns.get(0); 86 } 87 88 public String getUrlForHandler(String handlerName, 89 HttpServletRequest request) { 90 91 AttributePattern p = getPatternForHandler(handlerName, request, 0); 92 if (p == null) { 93 return null; 94 } 95 return addServletMappingIfNecessary(p.toString(), request); 96 } 97 98 public String getUrlForHandlerWithAttribute(String handlerName, 99 Object attribute, HttpServletRequest request) { 100 101 AttributePattern p = getPatternForHandler(handlerName, request, 1); 102 if (p == null) { 103 return null; 104 } 105 String url = p.fillInAttribute(attribute); 106 return addServletMappingIfNecessary(url, request); 107 } 108 109 public String getUrlForHandlerWithAttributes(String handlerName, 110 Object attributes, HttpServletRequest request) { 111 112 if (attributes == null) { 113 return getUrlForHandler(handlerName, request); 114 } 115 if (attributes instanceof Map ) { 116 return getUrlForHandlerWithMap(handlerName, 117 (Map ) attributes, request); 118 } 119 return getUrlForHandlerWithBean(handlerName, attributes, request); 120 } 121 122 protected String getUrlForHandlerWithMap(String beanName, 123 Map attributes, HttpServletRequest request) { 124 125 List patterns = getPatternsForHandler(beanName, request); 126 if (patterns == null || patterns.isEmpty()) { 127 return null; 128 } 129 Iterator it = patterns.iterator(); 130 while (it.hasNext()) { 131 AttributePattern p = (AttributePattern) it.next(); 132 if (p.matches(attributes)) { 133 String path = p.fillInAttributes(new MapWrapper(attributes)); 134 return addServletMappingIfNecessary(path, request); 135 } 136 } 137 return null; 138 } 139 140 protected String getUrlForHandlerWithBean(String beanName, 141 Object bean, HttpServletRequest request) { 142 143 AttributePattern p = getPatternForHandler(beanName, request); 144 if (p != null) { 145 String path = p.fillInAttributes(new BeanWrapperImpl(bean)); 146 return addServletMappingIfNecessary(path, request); 147 } 148 return null; 149 } 150 151 } 152 | Popular Tags |