KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > locator > UrlComponentListLocator


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.components.locator;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.riotfamily.common.web.util.PathCompleter;
29 import org.riotfamily.common.web.util.ServletUtils;
30
31 /**
32  * ComponentListLocator that calls
33  * {@link ServletUtils#getOriginatingPathWithoutServletMapping(HttpServletRequest)}
34  * to determine the lookup-path for a given request.
35  * <p>
36  * Example: If the DispatcherServlet is mapped to <code>*.html</code> and
37  * <code>/myapp</code> is the application's context path, the component-path
38  * for a request to <code>/myapp/foo/bar.html</code> will be
39  * <code>/foo/bar</code>.
40  * </p>
41  * @since 6.5
42  */

43 public class UrlComponentListLocator extends AbstractComponentListLocator {
44
45     public static final String JavaDoc TYPE_URL = "url";
46
47     private PathCompleter pathCompleter;
48
49     private String JavaDoc parameter;
50
51     public UrlComponentListLocator(PathCompleter pathCompleter) {
52         super(TYPE_URL);
53         this.pathCompleter = pathCompleter;
54     }
55
56     public void setParameter(String JavaDoc parameter) {
57         this.parameter = parameter;
58     }
59
60     protected String JavaDoc getPath(HttpServletRequest JavaDoc request) {
61         String JavaDoc path = ServletUtils.getOriginatingPathWithoutServletMapping(request);
62         if (parameter != null) {
63             String JavaDoc value = request.getParameter(parameter);
64             if (value != null) {
65                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(path);
66                 sb.append('?').append(parameter).append('=').append(value);
67                 return sb.toString();
68             }
69         }
70         return path;
71     }
72
73     /**
74      * Returns a substring of the given path starting at zero and ending before
75      * the last slash character. If no slash is found or the only slash is
76      * at the beginning of the path, <code>null</code> is returned.
77      */

78     protected String JavaDoc getParentPath(String JavaDoc path) {
79         int i = path.lastIndexOf('/');
80         if (i > 0) {
81             return path.substring(0, i);
82         }
83         return null;
84     }
85
86     protected String JavaDoc getUrlForPath(String JavaDoc path) {
87         return pathCompleter.addServletMapping(path);
88     }
89 }
90
Popular Tags