KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > util > PathCompleter


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.util;
25
26 import javax.servlet.ServletContext JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.beans.factory.InitializingBean;
31 import org.springframework.util.Assert;
32 import org.springframework.web.context.ServletContextAware;
33
34
35 /**
36  * Service class that can be used to add the servlet-prefix or suffix to a
37  * path without having a request object.
38  *
39  * @author Felix Gnass [fgnass at neteye dot de]
40  * @since 6.4
41  */

42 public class PathCompleter implements ServletContextAware, InitializingBean {
43
44     private static final Log log = LogFactory.getLog(PathCompleter.class);
45
46     private String JavaDoc servletName;
47
48     private String JavaDoc servletMapping;
49
50     private ServletContext JavaDoc servletContext;
51
52     private boolean prefixMapping;
53     
54     private String JavaDoc servletPrefix;
55
56     private boolean suffixMapping;
57     
58     private String JavaDoc servletSuffix;
59
60     public void setServletContext(ServletContext JavaDoc servletContext) {
61         this.servletContext = servletContext;
62     }
63
64     public void setServletMapping(String JavaDoc servletMapping) {
65         this.servletMapping = servletMapping;
66     }
67
68     public void setServletName(String JavaDoc servletName) {
69         this.servletName = servletName;
70     }
71
72     public void afterPropertiesSet() throws Exception JavaDoc {
73         if (servletMapping == null) {
74             Assert.notNull(servletName, "Either servletMapping or "
75                     + "servletName must be set.");
76
77             servletMapping = ServletUtils.getServletMapping(servletName,
78                     servletContext);
79
80             Assert.notNull(servletMapping, "Could not determine mapping "
81                     + "for servlet '" + servletName + "'.");
82
83             log.info("Servlet '" + servletName + "' is mapped to "
84                     + servletMapping + " in web.xml");
85         }
86                 
87         int i = servletMapping.indexOf('*');
88         if (i == 0) {
89             suffixMapping = true;
90             servletSuffix = servletMapping.substring(1);
91             log.info("Servlet suffix: '" + servletSuffix + "'");
92         }
93         if (i > 0) {
94             prefixMapping = true;
95             servletPrefix = servletMapping.substring(0, i - 1);
96             log.info("Servlet prefix: '" + servletPrefix + "'");
97         }
98     }
99
100     public boolean isPrefixMapping() {
101         return this.prefixMapping;
102     }
103     
104     public boolean isSuffixMapping() {
105         return this.suffixMapping;
106     }
107     
108     public String JavaDoc addServletMapping(String JavaDoc path) {
109         if (suffixMapping) {
110             int i = path.indexOf('?');
111             if (i != -1) {
112                 return path.substring(0, i) + servletSuffix + path.substring(i);
113             }
114             else {
115                 return path + servletSuffix;
116             }
117         }
118         if (prefixMapping) {
119             return servletPrefix + path;
120         }
121         return path;
122     }
123
124     public void addServletMapping(StringBuffer JavaDoc path) {
125         if (prefixMapping) {
126             path.insert(0, servletPrefix);
127         }
128         else if (suffixMapping) {
129             int i = path.indexOf("?");
130             if (i != -1) {
131                 path.insert(i, servletSuffix);
132             }
133             else {
134                 path.append(servletSuffix);
135             }
136         }
137     }
138
139 }
140
Popular Tags