KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > mapping > AbstractReverseHandlerMapping


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.mapping;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import org.riotfamily.common.beans.MapWrapper;
33 import org.springframework.beans.BeanWrapperImpl;
34 import org.springframework.web.servlet.handler.AbstractHandlerMapping;
35
36 /**
37  * @author Felix Gnass [fgnass at neteye dot de]
38  * @since 6.5
39  */

40 public abstract class AbstractReverseHandlerMapping
41         extends AbstractHandlerMapping implements ReverseHandlerMapping {
42     
43     protected abstract List JavaDoc getPatternsForHandler(String JavaDoc beanName,
44             HttpServletRequest JavaDoc request);
45     
46     protected String JavaDoc addServletMappingIfNecessary(String JavaDoc path,
47             HttpServletRequest JavaDoc request) {
48         
49         return path;
50     }
51     
52     protected AttributePattern getPatternForHandler(String JavaDoc handlerName,
53             HttpServletRequest JavaDoc request, int numberOfWildcards) {
54         
55         List JavaDoc patterns = getPatternsForHandler(handlerName, request);
56         if (patterns == null || patterns.isEmpty()) {
57             return null;
58         }
59         Iterator JavaDoc 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 JavaDoc("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 JavaDoc handlerName,
75             HttpServletRequest JavaDoc request) {
76         
77         List JavaDoc patterns = getPatternsForHandler(handlerName, request);
78         if (patterns == null || patterns.isEmpty()) {
79             return null;
80         }
81         if (patterns.size() > 1) {
82             throw new IllegalArgumentException JavaDoc("Ambigious mapping - more than "
83                     + "one pattern is registered for hander " + handlerName);
84         }
85         return (AttributePattern) patterns.get(0);
86     }
87     
88     public String JavaDoc getUrlForHandler(String JavaDoc handlerName,
89             HttpServletRequest JavaDoc 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 JavaDoc getUrlForHandlerWithAttribute(String JavaDoc handlerName,
99             Object JavaDoc attribute, HttpServletRequest JavaDoc request) {
100         
101         AttributePattern p = getPatternForHandler(handlerName, request, 1);
102         if (p == null) {
103             return null;
104         }
105         String JavaDoc url = p.fillInAttribute(attribute);
106         return addServletMappingIfNecessary(url, request);
107     }
108     
109     public String JavaDoc getUrlForHandlerWithAttributes(String JavaDoc handlerName,
110             Object JavaDoc attributes, HttpServletRequest JavaDoc request) {
111         
112         if (attributes == null) {
113             return getUrlForHandler(handlerName, request);
114         }
115         if (attributes instanceof Map JavaDoc) {
116             return getUrlForHandlerWithMap(handlerName,
117                     (Map JavaDoc) attributes, request);
118         }
119         return getUrlForHandlerWithBean(handlerName, attributes, request);
120     }
121     
122     protected String JavaDoc getUrlForHandlerWithMap(String JavaDoc beanName,
123             Map JavaDoc attributes, HttpServletRequest JavaDoc request) {
124         
125         List JavaDoc patterns = getPatternsForHandler(beanName, request);
126         if (patterns == null || patterns.isEmpty()) {
127             return null;
128         }
129         Iterator JavaDoc it = patterns.iterator();
130         while (it.hasNext()) {
131             AttributePattern p = (AttributePattern) it.next();
132             if (p.matches(attributes)) {
133                 String JavaDoc path = p.fillInAttributes(new MapWrapper(attributes));
134                 return addServletMappingIfNecessary(path, request);
135             }
136         }
137         return null;
138     }
139     
140     protected String JavaDoc getUrlForHandlerWithBean(String JavaDoc beanName,
141             Object JavaDoc bean, HttpServletRequest JavaDoc request) {
142         
143         AttributePattern p = getPatternForHandler(beanName, request);
144         if (p != null) {
145             String JavaDoc path = p.fillInAttributes(new BeanWrapperImpl(bean));
146             return addServletMappingIfNecessary(path, request);
147         }
148         return null;
149     }
150
151 }
152
Popular Tags