KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > model > URLModel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.model;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.config.RollerRuntimeConfig;
29 import org.apache.roller.pojos.WebsiteData;
30 import org.apache.roller.ui.core.RequestConstants;
31 import org.apache.roller.ui.rendering.util.WeblogRequest;
32 import org.apache.roller.util.URLUtilities;
33 import org.apache.struts.util.RequestUtils;
34
35
36 /**
37  * Provides access to URL building functionality.
38  *
39  * NOTE: we purposely go against the standard getter/setter bean standard
40  * for methods that take arguments so that users get a consistent way to
41  * access those methods in their templates. i.e.
42  *
43  * $url.category("foo")
44  *
45  * instead of
46  *
47  * $url.getCategory("foo")
48  */

49 public class URLModel implements Model {
50     
51     private static Log log = LogFactory.getLog(URLModel.class);
52     
53     private WebsiteData weblog = null;
54     private String JavaDoc locale = null;
55     
56     /** TODO: remove dependency on pageContext */
57     private PageContext JavaDoc pageContext = null;
58     
59     
60     public URLModel() {}
61     
62     public String JavaDoc getModelName() {
63         return "url";
64     }
65     
66     public void init(Map JavaDoc initData) throws RollerException {
67         
68         // need a weblog request so that we can know the weblog and locale
69
WeblogRequest weblogRequest = (WeblogRequest) initData.get("weblogRequest");
70         if(weblogRequest == null) {
71             throw new RollerException("Expected 'weblogRequest' init param!");
72         }
73         
74         this.weblog = weblogRequest.getWeblog();
75         this.locale = weblogRequest.getLocale();
76         
77         // need page context as well :(
78
pageContext = (PageContext JavaDoc) initData.get("pageContext");
79     }
80     
81     
82     /** Relative URL of Roller, e.g. /roller */
83     public String JavaDoc getSite() {
84         return RollerRuntimeConfig.getRelativeContextURL();
85     }
86     
87     
88     /** Absolute URL of Roller, e.g. http://localhost:8080/roller */
89     public String JavaDoc getAbsoluteSite() {
90         return RollerRuntimeConfig.getAbsoluteContextURL();
91     }
92     
93     
94     /** URL for logging in */
95     public String JavaDoc getLogin() {
96         String JavaDoc returnURL = null;
97         try {
98             returnURL = RequestUtils.computeURL(pageContext,
99                 "login-redirect", null, null, null, null, null, false);
100         } catch (MalformedURLException JavaDoc mue) {
101             log.error("ERROR forming Struts URL: ", mue);
102         }
103         return returnURL;
104     }
105     
106     
107     /** URL for logging out */
108     public String JavaDoc getLogout() {
109         String JavaDoc returnURL = null;
110         try {
111             returnURL = RequestUtils.computeURL(pageContext,
112                 "logout-redirect", null, null, null, null, null, false);
113         } catch (MalformedURLException JavaDoc mue) {
114             log.error("ERROR forming Struts URL: ", mue);
115         }
116         return returnURL;
117     }
118     
119     
120     public String JavaDoc getCommentAuthenticator() {
121         return getSite()+"/CommentAuthenticatorServlet";
122     }
123     
124     
125     public String JavaDoc themeResource(String JavaDoc theme, String JavaDoc filePath) {
126         return getSite()+RollerRuntimeConfig.getProperty("users.themes.path")+"/"+theme+"/"+filePath;
127     }
128         
129     public String JavaDoc themeResource(String JavaDoc theme, String JavaDoc filePath, boolean absolute) {
130         if (absolute) {
131             return getAbsoluteSite()+RollerRuntimeConfig.getProperty("users.themes.path")+"/"+theme+"/"+filePath;
132         }
133         return themeResource(theme, filePath);
134     }
135         
136     public String JavaDoc getHome() {
137         return URLUtilities.getWeblogCollectionURL(weblog, locale, null, null, -1, true);
138     }
139     
140     
141     public String JavaDoc home(int pageNum) {
142         return URLUtilities.getWeblogCollectionURL(weblog, locale, null, null, pageNum, true);
143     }
144     
145     
146     public String JavaDoc home(String JavaDoc customLocale) {
147         return URLUtilities.getWeblogCollectionURL(weblog, customLocale, null, null, -1, true);
148     }
149     
150     
151     public String JavaDoc home(String JavaDoc customLocale, int pageNum) {
152         return URLUtilities.getWeblogCollectionURL(weblog, customLocale, null, null, pageNum, true);
153     }
154     
155     
156     public String JavaDoc entry(String JavaDoc anchor) {
157         return URLUtilities.getWeblogEntryURL(weblog, locale, anchor, true);
158     }
159     
160     public String JavaDoc comment(String JavaDoc anchor, String JavaDoc timeStamp) {
161         return URLUtilities.getWeblogCommentURL(weblog, locale, anchor, timeStamp, true);
162     }
163     
164     
165     public String JavaDoc comments(String JavaDoc anchor) {
166         return URLUtilities.getWeblogCommentsURL(weblog, locale, anchor, true);
167     }
168     
169     
170     public String JavaDoc trackback(String JavaDoc anchor) {
171         return URLUtilities.getWeblogEntryURL(weblog, locale, anchor, true);
172     }
173
174     
175     public String JavaDoc date(String JavaDoc dateString) {
176         return URLUtilities.getWeblogCollectionURL(weblog, locale, null, dateString, -1, true);
177     }
178     
179     
180     public String JavaDoc date(String JavaDoc dateString, int pageNum) {
181         return URLUtilities.getWeblogCollectionURL(weblog, locale, null, dateString, pageNum, true);
182     }
183     
184     
185     public String JavaDoc category(String JavaDoc catPath) {
186         return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, null, -1, true);
187     }
188     
189     
190     public String JavaDoc category(String JavaDoc catPath, int pageNum) {
191         return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, null, pageNum, true);
192     }
193     
194     
195     public String JavaDoc collection(String JavaDoc dateString, String JavaDoc catPath) {
196         return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, dateString, -1, true);
197     }
198     
199     
200     public String JavaDoc collection(String JavaDoc dateString, String JavaDoc catPath, int pageNum) {
201         return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, dateString, pageNum, true);
202     }
203     
204     
205     public String JavaDoc getSearch() {
206         return URLUtilities.getWeblogSearchURL(weblog, locale, null, null, -1, false);
207     }
208     
209     
210     public String JavaDoc search(String JavaDoc query, int pageNum) {
211         return URLUtilities.getWeblogSearchURL(weblog, locale, query, null, pageNum, false);
212     }
213     
214     
215     public String JavaDoc search(String JavaDoc query, String JavaDoc catPath, int pageNum) {
216         return URLUtilities.getWeblogSearchURL(weblog, locale, query, catPath, pageNum, false);
217     }
218     
219     
220     public String JavaDoc page(String JavaDoc pageLink) {
221         return URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, null, null, -1, true);
222     }
223     
224     
225     public String JavaDoc page(String JavaDoc pageLink, String JavaDoc dateString, String JavaDoc catPath, int pageNum) {
226         return URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, catPath, dateString, pageNum, true);
227     }
228     
229     
230     public String JavaDoc resource(String JavaDoc filePath) {
231         return URLUtilities.getWeblogResourceURL(weblog, filePath, true);
232     }
233     
234     
235     public String JavaDoc getRsd() {
236         return URLUtilities.getWeblogRsdURL(weblog, true);
237     }
238     
239     
240     public FeedURLS getFeed() {
241         return new FeedURLS();
242     }
243     
244     
245     /** URL for editing a weblog entry */
246     public String JavaDoc editEntry(String JavaDoc anchor) {
247         String JavaDoc ret = null;
248         Map JavaDoc params = new HashMap JavaDoc();
249         params.put(RequestConstants.ANCHOR, anchor);
250         params.put(RequestConstants.WEBLOG, weblog.getHandle());
251         try {
252             ret = RequestUtils.computeURL(pageContext,
253                 "weblogEdit", null, null, null, params, null, false);
254         } catch (MalformedURLException JavaDoc mue) {
255             log.error("ERROR forming Struts URL: ", mue);
256         }
257         return ret;
258     }
259     
260     
261     /** URL for creating a new weblog entry */
262     public String JavaDoc getCreateEntry() {
263         String JavaDoc returnURL = null;
264         Map JavaDoc params = new HashMap JavaDoc();
265         params.put(RequestConstants.WEBLOG, weblog.getHandle());
266         try {
267             returnURL = RequestUtils.computeURL(pageContext,
268                 "weblogCreate", null, null, null, params, null, false);
269         } catch (MalformedURLException JavaDoc mue) {
270             log.error("ERROR forming Struts URL: ", mue);
271         }
272         return returnURL;
273     }
274     
275     
276     /** URL for editing weblog settings */
277     public String JavaDoc getEditSettings() {
278         String JavaDoc returnURL = null;
279         Map JavaDoc params = new HashMap JavaDoc();
280         params.put(RequestConstants.WEBLOG, weblog.getHandle());
281         try {
282             returnURL = RequestUtils.computeURL(pageContext,
283                 "editWebsite", null, null, null, params, null, false);
284         } catch (MalformedURLException JavaDoc mue) {
285             log.error("ERROR forming Struts URL: ", mue);
286         }
287         return returnURL;
288     }
289     
290     
291     /////// Inner Classes ///////
292

293     public class FeedURLS {
294         
295         public EntryFeedURLS getEntries() {
296             return new EntryFeedURLS();
297         }
298         
299         public CommentFeedURLS getComments() {
300             return new CommentFeedURLS();
301         }
302     }
303     
304     public class EntryFeedURLS {
305         
306         public String JavaDoc getRss() {
307             return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "rss", null, false, true);
308         }
309         
310         public String JavaDoc rss(String JavaDoc catPath, boolean excerpts) {
311             return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "rss", catPath, excerpts, true);
312         }
313         
314         public String JavaDoc getAtom() {
315             return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "atom", null, false, true);
316         }
317         
318         public String JavaDoc atom(String JavaDoc catPath, boolean excerpts) {
319             return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "atom", catPath, excerpts, true);
320         }
321     }
322     
323     public class CommentFeedURLS {
324         
325         public String JavaDoc getRss() {
326             return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "rss", null, false, true);
327         }
328         
329         public String JavaDoc rss(String JavaDoc catPath, boolean excerpts) {
330             return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "rss", catPath, excerpts, true);
331         }
332         
333         public String JavaDoc getAtom() {
334             return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "atom", null, false, true);
335         }
336         
337         public String JavaDoc atom(String JavaDoc catPath, boolean excerpts) {
338             return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "atom", catPath, excerpts, true);
339         }
340         
341     }
342     
343 }
344
Popular Tags