001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.model;
020:
021: import java.net.MalformedURLException;
022: import java.util.Arrays;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import javax.servlet.jsp.PageContext;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.config.RollerRuntimeConfig;
031: import org.apache.roller.pojos.WebsiteData;
032: import org.apache.roller.ui.core.RequestConstants;
033: import org.apache.roller.ui.rendering.util.WeblogRequest;
034: import org.apache.roller.util.URLUtilities;
035: import org.apache.struts.util.RequestUtils;
036:
037: /**
038: * Provides access to URL building functionality.
039: *
040: * NOTE: we purposely go against the standard getter/setter bean standard
041: * for methods that take arguments so that users get a consistent way to
042: * access those methods in their templates. i.e.
043: *
044: * $url.category("foo")
045: *
046: * instead of
047: *
048: * $url.getCategory("foo")
049: */
050: public class URLModel implements Model {
051:
052: private static Log log = LogFactory.getLog(URLModel.class);
053:
054: private WebsiteData weblog = null;
055: private String locale = null;
056:
057: /** TODO: remove dependency on pageContext */
058: private PageContext pageContext = null;
059:
060: public URLModel() {
061: }
062:
063: public String getModelName() {
064: return "url";
065: }
066:
067: public void init(Map initData) throws RollerException {
068:
069: // need a weblog request so that we can know the weblog and locale
070: WeblogRequest weblogRequest = (WeblogRequest) initData
071: .get("weblogRequest");
072: if (weblogRequest == null) {
073: throw new RollerException(
074: "Expected 'weblogRequest' init param!");
075: }
076:
077: this .weblog = weblogRequest.getWeblog();
078: this .locale = weblogRequest.getLocale();
079:
080: // need page context as well :(
081: pageContext = (PageContext) initData.get("pageContext");
082: }
083:
084: /** Relative URL of Roller, e.g. /roller */
085: public String getSite() {
086: return RollerRuntimeConfig.getRelativeContextURL();
087: }
088:
089: /** Absolute URL of Roller, e.g. http://localhost:8080/roller */
090: public String getAbsoluteSite() {
091: return RollerRuntimeConfig.getAbsoluteContextURL();
092: }
093:
094: /** URL for logging in */
095: public String getLogin() {
096: String returnURL = null;
097: try {
098: returnURL = RequestUtils.computeURL(pageContext,
099: "login-redirect", null, null, null, null, null,
100: false);
101: } catch (MalformedURLException mue) {
102: log.error("ERROR forming Struts URL: ", mue);
103: }
104: return returnURL;
105: }
106:
107: /** URL for logging out */
108: public String getLogout() {
109: String returnURL = null;
110: try {
111: returnURL = RequestUtils.computeURL(pageContext,
112: "logout-redirect", null, null, null, null, null,
113: false);
114: } catch (MalformedURLException mue) {
115: log.error("ERROR forming Struts URL: ", mue);
116: }
117: return returnURL;
118: }
119:
120: public String getCommentAuthenticator() {
121: return getSite() + "/CommentAuthenticatorServlet";
122: }
123:
124: public String themeResource(String theme, String filePath) {
125: return getSite() + "/themes/" + theme + "/" + filePath;
126: }
127:
128: public String themeResource(String theme, String filePath,
129: boolean absolute) {
130: if (absolute) {
131: return getAbsoluteSite() + "/themes/" + theme + "/"
132: + filePath;
133: }
134: return themeResource(theme, filePath);
135: }
136:
137: public String getHome() {
138: return URLUtilities.getWeblogCollectionURL(weblog, locale,
139: null, null, null, -1, true);
140: }
141:
142: public String home(int pageNum) {
143: return URLUtilities.getWeblogCollectionURL(weblog, locale,
144: null, null, null, pageNum, true);
145: }
146:
147: public String home(String customLocale) {
148: return URLUtilities.getWeblogCollectionURL(weblog,
149: customLocale, null, null, null, -1, true);
150: }
151:
152: public String home(String customLocale, int pageNum) {
153: return URLUtilities.getWeblogCollectionURL(weblog,
154: customLocale, null, null, null, pageNum, true);
155: }
156:
157: public String entry(String anchor) {
158: return URLUtilities.getWeblogEntryURL(weblog, locale, anchor,
159: true);
160: }
161:
162: public String comment(String anchor, String timeStamp) {
163: return URLUtilities.getWeblogCommentURL(weblog, locale, anchor,
164: timeStamp, true);
165: }
166:
167: public String comments(String anchor) {
168: return URLUtilities.getWeblogCommentsURL(weblog, locale,
169: anchor, true);
170: }
171:
172: public String trackback(String anchor) {
173: return URLUtilities.getWeblogEntryURL(weblog, locale, anchor,
174: true);
175: }
176:
177: public String date(String dateString) {
178: return URLUtilities.getWeblogCollectionURL(weblog, locale,
179: null, dateString, null, -1, true);
180: }
181:
182: public String date(String dateString, int pageNum) {
183: return URLUtilities.getWeblogCollectionURL(weblog, locale,
184: null, dateString, null, pageNum, true);
185: }
186:
187: public String category(String catPath) {
188: return URLUtilities.getWeblogCollectionURL(weblog, locale,
189: catPath, null, null, -1, true);
190: }
191:
192: public String category(String catPath, int pageNum) {
193: return URLUtilities.getWeblogCollectionURL(weblog, locale,
194: catPath, null, null, pageNum, true);
195: }
196:
197: public String tag(String tag) {
198: return URLUtilities.getWeblogCollectionURL(weblog, locale,
199: null, null, Arrays.asList(new String[] { tag }), -1,
200: true);
201: }
202:
203: public String tag(String tag, int pageNum) {
204: return URLUtilities.getWeblogCollectionURL(weblog, locale,
205: null, null, Arrays.asList(new String[] { tag }),
206: pageNum, true);
207: }
208:
209: public String tags(List tags) {
210: return URLUtilities.getWeblogCollectionURL(weblog, locale,
211: null, null, tags, -1, true);
212: }
213:
214: public String tags(List tags, int pageNum) {
215: return URLUtilities.getWeblogCollectionURL(weblog, locale,
216: null, null, tags, -1, true);
217: }
218:
219: public String collection(String dateString, String catPath) {
220: return URLUtilities.getWeblogCollectionURL(weblog, locale,
221: catPath, dateString, null, -1, true);
222: }
223:
224: public String collection(String dateString, String catPath,
225: int pageNum) {
226: return URLUtilities.getWeblogCollectionURL(weblog, locale,
227: catPath, dateString, null, pageNum, true);
228: }
229:
230: public String getSearch() {
231: return URLUtilities.getWeblogSearchURL(weblog, locale, null,
232: null, -1, false);
233: }
234:
235: public String search(String query, int pageNum) {
236: return URLUtilities.getWeblogSearchURL(weblog, locale, query,
237: null, pageNum, false);
238: }
239:
240: public String search(String query, String catPath, int pageNum) {
241: return URLUtilities.getWeblogSearchURL(weblog, locale, query,
242: catPath, pageNum, false);
243: }
244:
245: public String page(String pageLink) {
246: return URLUtilities.getWeblogPageURL(weblog, locale, pageLink,
247: null, null, null, null, -1, true);
248: }
249:
250: public String page(String pageLink, String dateString,
251: String catPath, int pageNum) {
252: return URLUtilities.getWeblogPageURL(weblog, locale, pageLink,
253: null, catPath, dateString, null, pageNum, true);
254: }
255:
256: public String resource(String filePath) {
257: return URLUtilities
258: .getWeblogResourceURL(weblog, filePath, true);
259: }
260:
261: public String getRsd() {
262: return URLUtilities.getWeblogRsdURL(weblog, true);
263: }
264:
265: public FeedURLS getFeed() {
266: return new FeedURLS();
267: }
268:
269: /** URL for editing a weblog entry */
270: public String editEntry(String anchor) {
271: String ret = null;
272: Map params = new HashMap();
273: params.put(RequestConstants.ANCHOR, anchor);
274: params.put(RequestConstants.WEBLOG, weblog.getHandle());
275: try {
276: ret = RequestUtils.computeURL(pageContext, "weblogEdit",
277: null, null, null, params, null, false);
278: } catch (MalformedURLException mue) {
279: log.error("ERROR forming Struts URL: ", mue);
280: }
281: return ret;
282: }
283:
284: /** URL for creating a new weblog entry */
285: public String getCreateEntry() {
286: String returnURL = null;
287: Map params = new HashMap();
288: params.put(RequestConstants.WEBLOG, weblog.getHandle());
289: try {
290: returnURL = RequestUtils.computeURL(pageContext,
291: "weblogCreate", null, null, null, params, null,
292: false);
293: } catch (MalformedURLException mue) {
294: log.error("ERROR forming Struts URL: ", mue);
295: }
296: return returnURL;
297: }
298:
299: /** URL for editing weblog settings */
300: public String getEditSettings() {
301: String returnURL = null;
302: Map params = new HashMap();
303: params.put(RequestConstants.WEBLOG, weblog.getHandle());
304: try {
305: returnURL = RequestUtils.computeURL(pageContext,
306: "editWebsite", null, null, null, params, null,
307: false);
308: } catch (MalformedURLException mue) {
309: log.error("ERROR forming Struts URL: ", mue);
310: }
311: return returnURL;
312: }
313:
314: /////// Inner Classes ///////
315:
316: public class FeedURLS {
317:
318: public EntryFeedURLS getEntries() {
319: return new EntryFeedURLS();
320: }
321:
322: public CommentFeedURLS getComments() {
323: return new CommentFeedURLS();
324: }
325: }
326:
327: public class EntryFeedURLS {
328:
329: public String getRss() {
330: return URLUtilities.getWeblogFeedURL(weblog, locale,
331: "entries", "rss", null, null, false, true);
332: }
333:
334: public String rss(String catPath, boolean excerpts) {
335: return URLUtilities.getWeblogFeedURL(weblog, locale,
336: "entries", "rss", catPath, null, excerpts, true);
337: }
338:
339: public String rssByTags(List tags, boolean excerpts) {
340: return URLUtilities.getWeblogFeedURL(weblog, locale,
341: "entries", "rss", null, tags, excerpts, true);
342: }
343:
344: public String getAtom() {
345: return URLUtilities.getWeblogFeedURL(weblog, locale,
346: "entries", "atom", null, null, false, true);
347: }
348:
349: public String atom(String catPath, boolean excerpts) {
350: return URLUtilities.getWeblogFeedURL(weblog, locale,
351: "entries", "atom", catPath, null, excerpts, true);
352: }
353:
354: public String atomByTags(List tags, boolean excerpts) {
355: return URLUtilities.getWeblogFeedURL(weblog, locale,
356: "entries", "atom", null, tags, excerpts, true);
357: }
358: }
359:
360: public class CommentFeedURLS {
361:
362: public String getRss() {
363: return URLUtilities.getWeblogFeedURL(weblog, locale,
364: "comments", "rss", null, null, false, true);
365: }
366:
367: public String rss(String catPath, boolean excerpts) {
368: return URLUtilities.getWeblogFeedURL(weblog, locale,
369: "comments", "rss", catPath, null, excerpts, true);
370: }
371:
372: public String getAtom() {
373: return URLUtilities.getWeblogFeedURL(weblog, locale,
374: "comments", "atom", null, null, false, true);
375: }
376:
377: public String atom(String catPath, boolean excerpts) {
378: return URLUtilities.getWeblogFeedURL(weblog, locale,
379: "comments", "atom", catPath, null, excerpts, true);
380: }
381:
382: }
383:
384: }
|