KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > www > BlogNavigator


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: BlogNavigator.java,v 1.6 2007/02/20 02:04:36 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.blog.www;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.opensubsystems.blog.data.Blog;
30 import org.opensubsystems.blog.data.Entry;
31 import org.opensubsystems.core.util.Log;
32 import org.opensubsystems.core.www.WebCommonConstants;
33 import org.opensubsystems.core.www.WebUtils;
34
35 /**
36  * Class responsible for parsing and creating URLs for blogs and their entries.
37  *
38  * If root URL is http://www.opensubsystems.org then the structure of URLs is
39  * - Bog with folder "firstblog"
40  * root URL = http://www.opensubsystems.org/firstblog/
41  * page URL = http://www.opensubsystems.org/firstblog/index.html
42  * - Entry belonging to "firstblog" with ID 123456789
43  * page URL = http://www.opensubsystems.org/firstblog/123456789.html
44  *
45  * @version $Id: BlogNavigator.java,v 1.6 2007/02/20 02:04:36 bastafidli Exp $
46  * @author Miro Halas
47  * @code.reviewer Miro Halas
48  * @code.reviewed 1.3 2006/07/15 09:09:26 bastafidli
49  */

50 public class BlogNavigator
51 {
52    // Constants ////////////////////////////////////////////////////////////////
53

54    /**
55     * Page where user can login to the system.
56     */

57    public static final String JavaDoc LOGIN_WEB_PAGE = "login.html";
58    
59    /**
60     * Page where user can logout to the system.
61     */

62    public static final String JavaDoc LOGOUT_WEB_PAGE = "logout.html";
63    
64    // Attributes ///////////////////////////////////////////////////////////////
65

66    /**
67     * URL to the directory containing all blogs.
68     */

69    protected String JavaDoc m_strBlogDirectoryURL;
70    
71    /**
72     * Request that will be used by this navigator
73     */

74    protected HttpServletRequest JavaDoc m_hsrqRequest;
75
76    // Cached values ////////////////////////////////////////////////////////////
77

78    /**
79     * Logger for this class
80     */

81    private static Logger JavaDoc s_logger = Log.getInstance(BlogNavigator.class);
82
83    // Constructors /////////////////////////////////////////////////////////////
84

85    /**
86     * Creates a new instance of BlogNavigator
87     *
88     * @param hsrqRequest - request that will be used by this navigator
89     */

90    public BlogNavigator(
91       HttpServletRequest JavaDoc hsrqRequest
92    )
93    {
94       m_hsrqRequest = hsrqRequest;
95       
96       // If this is part of web application installed under name chronicle
97
// and this servlet is registered under name blog, the navigator
98
// will be constructed for directory /chronicle/blog
99
String JavaDoc strBlogDirectoryURL = hsrqRequest.getContextPath();
100       
101       if (!strBlogDirectoryURL.endsWith(WebCommonConstants.URL_SEPARATOR))
102       {
103          // Be forgiving and correct the URL if it is not correct, but print
104
// message about it so we can optimize code
105
s_logger.finest("Optimize call to BlogNavigator constructor" +
106                          " to pass fully formed URL.");
107
108          StringBuffer JavaDoc sbTemp = new StringBuffer JavaDoc(strBlogDirectoryURL);
109          sbTemp.append(WebCommonConstants.URL_SEPARATOR);
110          m_strBlogDirectoryURL = sbTemp.toString();
111       }
112       else
113       {
114          m_strBlogDirectoryURL = strBlogDirectoryURL;
115       }
116    }
117   
118    // URL Accessors ////////////////////////////////////////////////////////////
119

120    /**
121     * Get URL to where a form can be posted to be processed
122     *
123     * @return String - URL to posts the forms to
124     */

125    public String JavaDoc getPostURL(
126    )
127    {
128       // For static navigation the URL will be posted to the same servlet where
129
// it came from
130
return "";
131    }
132
133    /**
134     * Get URL to the root directory where all blogs exist.
135     *
136     * @return String - URL ends with WebCommonConstants.URL_SEPARATOR_CHAR
137     */

138    public String JavaDoc getRootURL(
139    )
140    {
141       StringBuffer JavaDoc sbURL = new StringBuffer JavaDoc(m_strBlogDirectoryURL);
142       
143       sbURL.append(WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE);
144       return sbURL.toString();
145    }
146
147    /**
148     * Get URL to the page which displays this blog.
149     *
150     * @param blog - blog to get page URL for
151     * @return String
152     */

153    public String JavaDoc getURL(
154       Blog blog
155    )
156    {
157       return getBlogURL(blog.getFolder());
158    }
159    
160    /**
161     * Get URL to the page that displays specified blog.
162     *
163     * @param objBlogIdentification - identification of the blog to get page URL
164     * for, which should be the folder
165     * @return String
166     */

167    public String JavaDoc getBlogURL(
168       Object JavaDoc objBlogIdentification
169    )
170    {
171       StringBuffer JavaDoc sbURL;
172
173       sbURL = getBlogRootURL(objBlogIdentification.toString());
174       sbURL.append(WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE);
175
176       return sbURL.toString();
177    }
178
179    /**
180     * Get URL to the page which displays specified entry.
181     *
182     * @param blog - blog to which this entry belongs to
183     * @param entry - entry to get page URL for
184     * @return String - URL to the page displaying specified entry
185     */

186    public String JavaDoc getURL(
187       Blog blog,
188       Entry entry
189    )
190    {
191       return getBlogEntryURL(blog.getFolder(), entry.getId());
192    }
193
194    /**
195     * Get URL to the page which displays specified blog entry.
196     *
197     * @param entryIdentification - identification of entry
198     * @return String - URL to the page displaying specified entry
199     */

200    public String JavaDoc getURL(
201       BlogEntryIdentification entryIdentification
202    )
203    {
204       return getBlogEntryURL((String JavaDoc)entryIdentification.m_objBlogIdentification,
205                              entryIdentification.m_iBlogEntryIdentification);
206    }
207
208    /**
209     * Get blog which coresponds to given request.
210     *
211     * @param hsrqRequest - the servlet request.
212     * @return Object - Identification of Blog coresponding to given request.
213     * This can be either String value identifying the Blog
214     * folder or Integer value identifying the Blog URL.
215     * Ff no such Blog exists, an exception will be thrown
216     */

217    public Object JavaDoc getBlogIdentification(
218       HttpServletRequest JavaDoc hsrqRequest
219    )
220    {
221       // Static BlogNavigator always returnes string value since it is part of
222
// the URL
223
String JavaDoc strBlogId = "";
224       String JavaDoc strExtraPath;
225       String JavaDoc strURL;
226       
227       strURL = hsrqRequest.getServletPath();
228       strExtraPath = hsrqRequest.getPathInfo();
229       if (strExtraPath != null)
230       {
231          strURL += strExtraPath;
232       }
233       if ((strURL != null) && (strURL.length() > 0))
234       {
235          int iIndex = strURL.indexOf(WebCommonConstants.URL_SEPARATOR_CHAR);
236          int iLastIndex = strURL.lastIndexOf(WebCommonConstants.URL_SEPARATOR_CHAR);
237          if ((iLastIndex != -1) && (iLastIndex > iIndex))
238          {
239             // Get blog which has ID the same as the path specified in the URL
240
strBlogId = strURL.substring(iIndex + 1, iLastIndex);
241          }
242       }
243          
244       return strBlogId;
245    }
246
247    /**
248     * Get blog and entries which coresponds to given request.
249     *
250     * @param hsrqRequest - the servlet request.
251     * @return BlogEntryIdentification
252     */

253    public BlogEntryIdentification getBlogEntryIdentification(
254       HttpServletRequest JavaDoc hsrqRequest
255    )
256    {
257       String JavaDoc strBlogURL;
258       String JavaDoc strExtraPath;
259       BlogEntryIdentification entryIdentification = null;
260
261       strBlogURL = hsrqRequest.getServletPath();
262       strExtraPath = hsrqRequest.getPathInfo();
263       if (strExtraPath != null)
264       {
265          strBlogURL += strExtraPath;
266       }
267       if ((strBlogURL != null) && (strBlogURL.length() > 0))
268       {
269          int iIndex = strBlogURL.indexOf(WebCommonConstants.URL_SEPARATOR_CHAR);
270          int iLastIndex = strBlogURL.lastIndexOf(WebCommonConstants.URL_SEPARATOR_CHAR);
271
272          if ((iLastIndex != -1) && (iLastIndex > iIndex))
273          {
274             try
275             {
276                int iEntryId;
277                
278                iEntryId = Integer.parseInt(
279                              strBlogURL.substring(iLastIndex + 1,
280                                 strBlogURL.lastIndexOf(
281                                      WebCommonConstants.WEB_PAGE_EXTENSION)));
282                entryIdentification = new BlogEntryIdentification();
283                entryIdentification.m_objBlogIdentification
284                   = strBlogURL.substring(strBlogURL.indexOf(
285                        WebCommonConstants.URL_SEPARATOR_CHAR) + 1, iLastIndex);
286                entryIdentification.m_iBlogEntryIdentification = iEntryId;
287                   
288             }
289             catch (NumberFormatException JavaDoc nfeExc)
290             {
291                s_logger.log(Level.FINEST,
292                             "The URL doesn't identify valid Blog entry.", nfeExc);
293                // Just return null and let the caller handle it
294
}
295          }
296       }
297       
298       return entryIdentification;
299    }
300    
301    /**
302     * Test if the requested path is path to the page where user can login to
303     * the system.
304     *
305     * @return boolean - true if the page is login page
306     */

307    public boolean isLoginPage(
308    )
309    {
310       String JavaDoc strPath;
311       
312       strPath = WebUtils.getFullRequestPath(m_hsrqRequest);
313
314       return strPath.endsWith(LOGIN_WEB_PAGE);
315    }
316
317    /**
318     * Test if the requested path is path to the page where user can login to
319     * the system.
320     *
321     * @return boolean - true if the page is login page
322     */

323    public boolean isLogoutPage(
324    )
325    {
326       String JavaDoc strPath;
327       
328       strPath = WebUtils.getFullRequestPath(m_hsrqRequest);
329
330       return strPath.endsWith(LOGOUT_WEB_PAGE);
331    }
332
333    // Helper methods ///////////////////////////////////////////////////////////
334

335    /**
336     * Get URL to the root directory where all entries for given blog exist.
337     *
338     * @param strId - ID of the blog to get root URL for
339     * @return String - URL ends with WebCommonConstants.URL_SEPARATOR_CHAR
340     */

341    protected StringBuffer JavaDoc getBlogRootURL(
342       String JavaDoc strId
343    )
344    {
345       StringBuffer JavaDoc sbURL;
346
347       sbURL = new StringBuffer JavaDoc(m_strBlogDirectoryURL);
348       sbURL.append(strId);
349       sbURL.append(WebCommonConstants.URL_SEPARATOR_CHAR);
350
351       return sbURL;
352    }
353
354    /**
355     * Get URL to the page which displays specified blog entry.
356     *
357     * @param strFolder - folder where this entry belongs to
358     * @param iEntryId - id of entry to get page URL for
359     * @return String - URL to the page displaying specified entry
360     */

361    protected String JavaDoc getBlogEntryURL(
362       String JavaDoc strFolder,
363       int iEntryId
364    )
365    {
366       StringBuffer JavaDoc sbURL;
367
368       sbURL = getBlogRootURL(strFolder);
369       sbURL.append(iEntryId);
370       sbURL.append(WebCommonConstants.WEB_PAGE_EXTENSION);
371
372       return sbURL.toString();
373    }
374    
375 }
376
Popular Tags