KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > CommonMacroHelper


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.view;
25
26 import java.io.IOException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Random JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.riotfamily.common.beans.PropertyUtils;
42 import org.riotfamily.common.util.FormatUtils;
43 import org.riotfamily.common.web.filter.ResourceStamper;
44 import org.riotfamily.common.web.mapping.ReverseHandlerMapping;
45 import org.riotfamily.common.web.util.ServletUtils;
46 import org.springframework.context.ApplicationContext;
47 import org.springframework.util.StringUtils;
48 import org.springframework.web.servlet.support.RequestContextUtils;
49
50 /**
51     * @author Felix Gnass [fgnass at neteye dot de]
52     * @since 6.5
53     */

54 public class CommonMacroHelper {
55
56     private static final Log log = LogFactory.getLog(CommonMacroHelper.class);
57
58     private static Random JavaDoc random = new Random JavaDoc();
59
60     private ApplicationContext ctx;
61     
62     private HttpServletRequest JavaDoc request;
63
64     private HttpServletResponse JavaDoc response;
65
66     private ResourceStamper stamper;
67
68     private List JavaDoc mappings;
69     
70     private Locale JavaDoc requestLocale = null;
71
72     public CommonMacroHelper(ApplicationContext ctx,
73             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
74             ResourceStamper stamper, List JavaDoc mappings) {
75
76         this.ctx = ctx;
77         this.request = request;
78         this.response = response;
79         this.stamper = stamper;
80         this.mappings = mappings;
81     }
82
83     public Random JavaDoc getRandom() {
84         return random;
85     }
86
87     public Locale JavaDoc getLocale() {
88         if (requestLocale == null) {
89             requestLocale = RequestContextUtils.getLocale(request);
90         }
91         return requestLocale;
92     }
93     
94     public String JavaDoc getMessage(String JavaDoc code, List JavaDoc args, String JavaDoc defaultMessage) {
95         return ctx.getMessage(code, args.toArray(), defaultMessage, getLocale());
96     }
97
98     public String JavaDoc resolveAndEncodeUrl(String JavaDoc url) {
99         return ServletUtils.resolveAndEncodeUrl(url, request, response);
100     }
101
102     public String JavaDoc getAbsoluteUrl(String JavaDoc url) {
103         return ServletUtils.getAbsoluteUrlPrefix(request)
104                 .append(request.getContextPath()).append(url).toString();
105     }
106
107     public String JavaDoc getUrlForHandler(String JavaDoc handlerName) {
108         String JavaDoc url = null;
109         Iterator JavaDoc it = mappings.iterator();
110         while (url == null && it.hasNext()) {
111             ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next();
112             url = mapping.getUrlForHandler(handlerName, request);
113         }
114         return url;
115     }
116     
117     public String JavaDoc getUrlForHandlerWithAttribute(String JavaDoc handlerName,
118             Object JavaDoc attribute) {
119         
120         String JavaDoc url = null;
121         Iterator JavaDoc it = mappings.iterator();
122         while (url == null && it.hasNext()) {
123             ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next();
124             url = mapping.getUrlForHandlerWithAttribute(
125                     handlerName, attribute, request);
126         }
127         return url;
128     }
129     
130     public String JavaDoc getUrlForHandlerWithAttributes(String JavaDoc handlerName,
131             Object JavaDoc attributes) {
132         
133         String JavaDoc url = null;
134         Iterator JavaDoc it = mappings.iterator();
135         while (url == null && it.hasNext()) {
136             ReverseHandlerMapping mapping = (ReverseHandlerMapping) it.next();
137             url = mapping.getUrlForHandlerWithAttributes(
138                     handlerName, attributes, request);
139         }
140         return url;
141     }
142         
143     public String JavaDoc getOriginatingRequestUri() {
144         String JavaDoc uri = ServletUtils.getOriginatingRequestUri(request);
145         if (StringUtils.hasText(request.getQueryString())) {
146             uri = uri + "?" + request.getQueryString();
147         }
148         return uri;
149     }
150     
151     public String JavaDoc getPathWithinApplication() {
152         return ServletUtils.getPathWithinApplication(request);
153     }
154     
155     public boolean isExternalUrl(String JavaDoc url) {
156         try {
157             URI JavaDoc uri = new URI JavaDoc(url);
158             if (!uri.isOpaque()) {
159                 if (uri.isAbsolute() && !request.getServerName().equals(
160                         uri.getHost())) {
161
162                     return true;
163                 }
164             }
165         }
166         catch (URISyntaxException JavaDoc e) {
167             log.warn(e.getMessage());
168         }
169         return false;
170     }
171
172     public String JavaDoc include(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
173         request.getRequestDispatcher(url).include(request, response);
174         return "";
175     }
176
177     public String JavaDoc addTimestamp(String JavaDoc s) {
178         return stamper.stamp(s);
179     }
180
181     public List JavaDoc partition(Collection JavaDoc c, String JavaDoc titleProperty) {
182         return PropertyUtils.partition(c, titleProperty);
183     }
184
185     public String JavaDoc getFileExtension(String JavaDoc filename, Collection JavaDoc validExtensions,
186             String JavaDoc defaultExtension) {
187
188         String JavaDoc ext = FormatUtils.getExtension(filename);
189         if (validExtensions.isEmpty() || validExtensions.contains(ext)) {
190             return ext;
191         }
192         return defaultExtension;
193     }
194
195     public String JavaDoc baseName(String JavaDoc path) {
196         int begin = path.lastIndexOf('/') + 1;
197         int end = path.indexOf(';');
198         if (end == -1) {
199             end = path.indexOf('?');
200             if (end == -1) {
201                 end = path.length();
202             }
203         }
204         return path.substring(begin, end);
205     }
206
207     public String JavaDoc formatByteSize(long bytes) {
208         return FormatUtils.formatByteSize(bytes);
209     }
210
211     public String JavaDoc toTitleCase(String JavaDoc s) {
212         return FormatUtils.fileNameToTitleCase(s);
213     }
214
215 }
216
Popular Tags