File Source: OldPageHelper.java
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.weblogger.ui.rendering.velocity.deprecated;
20
21 import java.util.Date;
22 import java.util.Hashtable;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Map;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.jsp.PageContext;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.weblogger.WebloggerException;
33 import org.apache.roller.weblogger.business.plugins.entry.WeblogEntryPlugin;
34 import org.apache.roller.weblogger.business.plugins.PluginManager;
35 import org.apache.roller.weblogger.business.Weblogger;
36 import org.apache.roller.weblogger.business.WebloggerFactory;
37 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
38 import org.apache.roller.weblogger.pojos.WeblogBookmarkFolder;
39 import org.apache.roller.weblogger.pojos.Weblog;
40 import org.apache.roller.weblogger.pojos.WeblogPermission;
41 import org.apache.roller.weblogger.pojos.wrapper.WeblogReferrerWrapper;
42 import org.apache.roller.weblogger.pojos.wrapper.WeblogEntryWrapper;
43 import org.apache.roller.weblogger.ui.core.RollerSession;
44 import org.apache.roller.weblogger.ui.core.tags.calendar.CalendarModel;
45 import org.apache.roller.weblogger.ui.core.tags.calendar.CalendarTag;
46 import org.apache.roller.weblogger.ui.core.tags.calendar.BigWeblogCalendarModel;
47 import org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel;
48 import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
49
50
51 /**
52 * Provides assistance to VelociMacros, filling in where Velocity falls.
53 */
54 public class OldPageHelper {
55
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldPageHelper__static_init
*
* Postconditions:
* init'ed(mLogger)
*/
56 private static Log mLogger = LogFactory.getLog(OldPageHelper.class);
57
58 private PageContext mPageContext = null;
59 private HttpServletRequest mRequest = null;
60 private HttpServletResponse mResponse = null;
61
62 private Map mPagePlugins = null; // Plugins keyed by name
63 private Weblog mWebsite = null;
64 private Date mDate = null;
65 private WeblogBookmarkFolder mFolder = null;
66 private String mPageName = null;
67 private WeblogPageRequest mPageRequest = null;
68
69 /**
70 * Initialize VelocityHelper, setting the variables it will be hiding from
71 * the Velocimacros.
72 */
73 public OldPageHelper(HttpServletRequest request,
74 HttpServletResponse response,
75 Map ctx,
76 Weblog website,
77 Date date,WeblogBookmarkFolder folder,
78 String pageName,
79 PageContext pageContext,
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldPageHelper(HttpServletRequest, HttpServletResponse, Map, Weblog, Date, WeblogBookmarkFolder, String, PageContext, WeblogPageRequest)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getPluginManager(...)@95 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@94 != null
*
* Postconditions:
* this.mDate == date
* init'ed(this.mDate)
* this.mFolder == folder
* init'ed(this.mFolder)
* this.mPageContext == pageContext
* init'ed(this.mPageContext)
* this.mPageName == null
* init'ed(this.mPagePlugins)
* this.mPageRequest == pageRequest
* init'ed(this.mPageRequest)
* ...
*/
80 WeblogPageRequest pageRequest) throws WebloggerException {
81
82 // general request objects
83 mRequest = request;
84 mResponse = response;
85 mPageContext = pageContext;
86 mPageRequest = pageRequest;
87
88 // data that we'll be reusing
89 mWebsite = website;
90 mDate = date;
91 mFolder = folder;
92
93 // init plugins
94 Weblogger roller = WebloggerFactory.getWeblogger();
95 PluginManager ppmgr = roller.getPluginManager();
96 mPagePlugins = ppmgr.getWeblogEntryPlugins(mWebsite);
97 }
98
99
100 /**
101 * Another stupid helper method to make up for the shortcomings of Velocity.
102 * @return HashMap
103 */
104 public Hashtable addParam(String key, String value, Hashtable map) {
/*
P/P * Method: Hashtable addParam(String, String, Hashtable)
*
* Postconditions:
* return_value == One-of{map, &new Hashtable(addParam#1)}
* return_value != null
* new Hashtable(addParam#1) num objects <= 1
*
* Test Vectors:
* key: Addr_Set{null}, Inverse{null}
* map: Inverse{null}, Addr_Set{null}
* value: Addr_Set{null}, Inverse{null}
*/
105 if (map == null) map = new Hashtable();
106 if (key != null && value != null)
107 map.put(key, value);
108 return map;
109 }
110
111
112 /**
113 * Evaluates the String as a Velocimacro, returning the results.
114 *
115 * @deprecated shouldn't be used anymore because it's dangerous
116 *
117 * @param str String
118 * @return String
119 */
120 public String evaluateString(String str) {
121 // we no longer allow users to do this because it is dangerous
/*
P/P * Method: String evaluateString(String)
*
* Postconditions:
* return_value == str
* init'ed(return_value)
*/
122 return str;
123 }
124
125
126 /** Build the URL for editing an WeblogEntry **/
127 public String getEntryEditUrl(WeblogEntryWrapper entry) {
/*
P/P * Method: String getEntryEditUrl(WeblogEntryWrapper)
*
* Preconditions:
* entry != null
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@128 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@128 != null
* org.apache.roller.weblogger.pojos.wrapper.WeblogEntryWrapper:getWebsite(...)@128 != null
*
* Postconditions:
* init'ed(return_value)
*/
128 return WebloggerFactory.getWeblogger().getUrlStrategy().getEntryEditURL(entry.getWebsite().getHandle(), entry.getId(), false);
129 }
130
131
132 public String getToggleLinkbackDisplayHTML(WeblogReferrerWrapper referer) {
133 // NOTE: this was EOLed as part of Weblogger 4.0 since we no longer
134 // have an action for toggling linkback display
/*
P/P * Method: String getToggleLinkbackDisplayHTML(WeblogReferrerWrapper)
*
* Postconditions:
* return_value == &""
*/
135 return "";
136 }
137
138
139 public boolean isUserAuthorizedToEdit() {
140 try {
/*
P/P * Method: bool isUserAuthorizedToEdit()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mRequest)
* (soft) init'ed(this.mWebsite)
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
*
* Postconditions:
* init'ed(return_value)
*/
141 RollerSession rses =
142 RollerSession.getRollerSession(mRequest);
143 if ( rses != null && rses.getAuthenticatedUser() != null
144 && mWebsite != null) {
145 return mWebsite.hasUserPermissions(
146 rses.getAuthenticatedUser(), WeblogPermission.ADMIN);
147 }
148 } catch (Exception e) {
149 mLogger.warn("PageHelper.isUserAuthorizedToEdit)", e);
150 }
151 return false;
152 }
153
154
155 public void setContentType( String type ) {
/*
P/P * Method: void setContentType(String)
*
* Preconditions:
* this.mResponse != null
*/
156 mResponse.setContentType(type);
157 }
158
159
160 /**
161 * Display big weblog calendar, well suited for an archive page.
162 * @return HTML for calendar.
163 */
164 public String showBigWeblogCalendar() {
/*
P/P * Method: String showBigWeblogCalendar()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mPageRequest.localeInstance)
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) this.mPageContext != null
* (soft) this.mPageRequest != null
* (soft) init'ed(this.mPageRequest.locale)
* (soft) init'ed(this.mPageRequest.weblogHandle)
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.mPageRequest.localeInstance)
* possibly_updated(this.mPageRequest.weblog)
* new Locale(getLocaleInstance#1) num objects == 0
* new Locale(getLocaleInstance#2) num objects == 0
*/
165 return showWeblogCalendar(true, null);
166 }
167
168
169 /**
170 * Call hybrid EditorNavBarTag to render editor navbar.
171 * @param vertical True for vertical navbar.
172 * @return String HTML for navbar.
173 */
174 public String showEditorNavBar(boolean vertical) {
175 // this method has been EOLd starting in Weblogger 4.0
176 // this same functionality can be obtained via the #showAuthorMenu() macro
/*
P/P * Method: String showEditorNavBar(bool)
*
* Postconditions:
* return_value == null
*/
177 return null;
178 }
179
180
181 /**
182 * Call hybrid EditorNavBarTag to render editor navbar.
183 * @param model Name of XML file in WEB-INF that contains XML for menu.
184 * @param template Name of Velocity template in classpath to display menu.
185 * @return String HTML for menu.
186 */
187 public String showMenu(String model, String template) {
188 // this method has been EOLd starting in Weblogger 4.0
189 // this same functionality can be obtained via the #showAuthorMenu() macro
/*
P/P * Method: String showMenu(String, String)
*
* Postconditions:
* return_value == null
*/
190 return null;
191 }
192
193 //------------------------------------------------- WeblogCalendar methods
194
195 /**
196 * Display weblog calendar.
197 * @return HTML for calendar.
198 */
199 public String showWeblogCalendar() {
/*
P/P * Method: String showWeblogCalendar()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mPageRequest.localeInstance)
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) this.mPageContext != null
* (soft) this.mPageRequest != null
* (soft) init'ed(this.mPageRequest.locale)
* (soft) init'ed(this.mPageRequest.weblogHandle)
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.mPageRequest.localeInstance)
* possibly_updated(this.mPageRequest.weblog)
* new Locale(getLocaleInstance#1) num objects == 0
* new Locale(getLocaleInstance#2) num objects == 0
*/
200 return showWeblogCalendar(false, null);
201 }
202
203
204 /**
205 * Weblog calendar display implementation.
206 * @param big Show big archive style calendar.
207 * @return HTML for calendar.
208 */
209 public String showWeblogCalendar( boolean big, String cat ) {
/*
P/P * Method: String showWeblogCalendar(bool, String)
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mPageRequest.localeInstance)
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) this.mPageContext != null
* (soft) this.mPageRequest != null
* (soft) init'ed(this.mPageRequest.locale)
* (soft) init'ed(this.mPageRequest.weblogHandle)
*
* Postconditions:
* init'ed(return_value)
* init'ed(this.mPageRequest.localeInstance)
* possibly_updated(this.mPageRequest.weblog)
* new Locale(getLocaleInstance#1) num objects == 0
* new Locale(getLocaleInstance#2) num objects == 0
*
* Test Vectors:
* big: {0}, {1}
* java.lang.String:equals(...)@210: {0}, {1}
*/
210 if (OldWeblogPageModel.VELOCITY_NULL.equals(cat)) cat = null;
211 String ret = null;
212 try {
213 // setup weblog calendar model
214 CalendarModel model = null;
215 if ( big ) {
216 model = new BigWeblogCalendarModel(mPageRequest, cat);
217 } else {
218 model = new WeblogCalendarModel(mPageRequest, cat);
219 }
220
221 // save model in JSP page context so CalendarTag can find it
222 mPageContext.setAttribute("calendarModel", model);
223
224 // Create and setup calendar tag
225 CalendarTag calTag = new CalendarTag();
226 calTag.setPageContext(mPageContext);
227 calTag.setName("calendar");
228 calTag.setModel("calendarModel");
229 calTag.setLocale(mPageRequest.getLocaleInstance());
230 if ( big ) {
231 calTag.setClassSuffix("Big");
232 }
233 ret = calTag.emit();
234 } catch (Exception e) {
235 mLogger.error("Unexpected exception",e);
236 }
237 return ret;
238 }
239
240
241 /**
242 * Convenience method, contrived helper for Velocity.
243 * @param useIds
244 * @param isAction
245 * @param path
246 * @param val1
247 * @param val2
248 * @return String
249 */
250 public String strutsUrlHelper( boolean useIds, boolean isAction,
251 String path, String val1, String val2) {
/*
P/P * Method: String strutsUrlHelper(bool, bool, String, String, String)
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* init'ed(return_value)
*/
252 Hashtable params = new Hashtable();
253 return strutsUrlHelper1( useIds, isAction, path, val1, val2, params);
254 }
255
256
257 /**
258 * Very contrived helper method for Velocimacros generating Struts links.
259 * This is really only of use to the showNavBar macro.
260 * @param useIds
261 * @param isAction
262 * @param path
263 * @param val1
264 * @param val2
265 * @return String
266 */
267 public String strutsUrlHelper1( boolean useIds, boolean isAction,
268 String path, String val1, String val2, Hashtable params) {
269
270 // NOTE: this method is now official defunct since Weblogger 4.0
271 // when we EOLed struts1 and had no real equivalent for this
272
/*
P/P * Method: String strutsUrlHelper1(bool, bool, String, String, String, Hashtable)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@282 != null
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@284 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@282 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@284 != null
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* init'ed(return_value)
*
* Test Vectors:
* path: Inverse{null}, Addr_Set{null}
* java.lang.String:equals(...)@275: {0}, {1}
* java.lang.String:equals(...)@278: {0}, {1}
* java.lang.String:equals(...)@281: {0}, {1}
* java.lang.String:equals(...)@283: {0}, {1}
*/
273 if(path == null) {
274 return null;
275 } else if("weblogCreate".equals(path)) {
276 // a little hacky, but hopefully nobody is really using this anymore
277 return WebloggerRuntimeConfig.getRelativeContextURL()+"/roller-ui/authoring/entryAdd.rol?weblog="+val1;
278 } else if("editWebsite".equals(path)) {
279 // a little hacky, but hopefully nobody is really using this anymore
280 return WebloggerRuntimeConfig.getRelativeContextURL()+"/roller-ui/authoring/weblogConfig.rol?weblog="+val1;
281 } else if("logout-redirect".equals(path)) {
282 return WebloggerFactory.getWeblogger().getUrlStrategy().getLogoutURL(false);
283 } else if("login-redirect".equals(path)) {
284 return WebloggerFactory.getWeblogger().getUrlStrategy().getLoginURL(false);
285 } else {
286 return "<span class=\"error\">ERROR generating link</span>";
287 }
288 }
289
290
291 /**
292 * Pass the String through any PagePlugins that have been
293 * assigned to the PageHelper, as selected by the Entry.
294 *
295 * @param entry Entry being rendered.
296 * @param str String to which plugins are to be applied.
297 * @return Result of applying plugins to str.
298 */
299 public String renderPlugins(WeblogEntryWrapper entry, String str) {
/*
P/P * Method: String renderPlugins(WeblogEntryWrapper, String)
*
* Preconditions:
* mLogger != null
* init'ed(this.mPagePlugins)
* (soft) entry != null
*
* Presumptions:
* java.util.Map:get(...)@315 != null
* java.util.Map:keySet(...)@311 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.mPagePlugins: Addr_Set{null}, Inverse{null}
* java.util.Iterator:hasNext(...)@312: {0}, {1}
* java.util.List:contains(...)@314: {0}, {1}
* java.util.List:isEmpty(...)@306: {1}, {0}
* org.apache.roller.weblogger.pojos.wrapper.WeblogEntryWrapper:getPluginsList(...)@304: Addr_Set{null}, Inverse{null}
*/
300 String ret = str;
301 mLogger.debug("Applying page plugins to string");
302
303 if (mPagePlugins != null) {
304 List entryPlugins = entry.getPluginsList();
305 // if no Entry plugins, don't bother looping.
306 if (entryPlugins != null && !entryPlugins.isEmpty()) {
307
308 // now loop over mPagePlugins, matching
309 // against Entry plugins (by name):
310 // where a match is found render Plugin.
311 Iterator iter = mPagePlugins.keySet().iterator();
312 while (iter.hasNext()) {
313 String key = (String)iter.next();
314 if (entryPlugins.contains(key)) {
315 WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin)mPagePlugins.get(key);
316 try {
317 ret = pagePlugin.render(entry.getPojo(), ret);
318 } catch (Throwable t) {
319 mLogger.error("ERROR from plugin: " + pagePlugin.getName(), t);
320 }
321 }
322 }
323 }
324 }
325
326 return ret;
327 }
328
329
330 /**
331 * This method used to return an array of supported locales based on some
332 * of the old i18n work done in Weblogger, however, as of Weblogger 3.0 there is
333 * no longer a list of supported languages. The languages available to a
334 * weblog are unbounded and are purely determined by the weblog author.
335 *
336 * This method always returns null.
337 */
338 public Locale[] getSupportedLanguages() {
/*
P/P * Method: Locale[] getSupportedLanguages()
*
* Postconditions:
* return_value == null
*/
339 return null;
340 }
341
342
343 /**
344 * @return relative URL to page, starting with /username
345 */
346 public String getPathInfo() {
/*
P/P * Method: String getPathInfo()
*
* Preconditions:
* this.mRequest != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* javax.servlet.http.HttpServletRequest:getPathInfo(...)@347: Inverse{null}, Addr_Set{null}
*/
347 String pathInfo = mRequest.getPathInfo();
348 if(pathInfo == null) {
349 pathInfo = "";
350 }
351
352 return pathInfo;
353 }
354
355
356 public String getCommentAuthenticatorHtml() {
357 // deprecated, does nothing now
/*
P/P * Method: String getCommentAuthenticatorHtml()
*
* Postconditions:
* return_value == &""
*/
358 return "";
359 }
360
361 }
SofCheck Inspector Build Version : 2.18479
| OldPageHelper.java |
2009-Jan-02 14:24:46 |
| OldPageHelper.class |
2009-Sep-04 03:12:45 |