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.velocity.deprecated;
020:
021: import java.net.MalformedURLException;
022: import java.util.Date;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Map;
028: import java.util.Vector;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.jsp.PageContext;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.struts.Globals;
035: import org.apache.struts.util.RequestUtils;
036: import org.apache.roller.RollerException;
037: import org.apache.roller.business.WeblogEntryPlugin;
038: import org.apache.roller.business.PluginManager;
039: import org.apache.roller.business.Roller;
040: import org.apache.roller.business.RollerFactory;
041: import org.apache.roller.pojos.FolderData;
042: import org.apache.roller.pojos.WebsiteData;
043: import org.apache.roller.pojos.wrapper.RefererDataWrapper;
044: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
045: import org.apache.roller.ui.core.RequestConstants;
046: import org.apache.roller.ui.core.RollerContext;
047: import org.apache.roller.ui.core.RollerSession;
048: import org.apache.roller.ui.core.tags.calendar.CalendarModel;
049: import org.apache.roller.ui.core.tags.calendar.CalendarTag;
050: import org.apache.roller.ui.core.tags.menu.EditorNavigationBarTag;
051: import org.apache.roller.ui.core.tags.menu.MenuTag;
052: import org.apache.roller.ui.core.tags.calendar.BigWeblogCalendarModel;
053: import org.apache.roller.ui.core.tags.calendar.WeblogCalendarModel;
054: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
055:
056: /**
057: * Provides assistance to VelociMacros, filling in where Velocity falls.
058: */
059: public class OldPageHelper {
060:
061: private static Log mLogger = LogFactory.getLog(OldPageHelper.class);
062:
063: private PageContext mPageContext = null;
064: private HttpServletRequest mRequest = null;
065: private HttpServletResponse mResponse = null;
066:
067: private Map mPagePlugins = null; // Plugins keyed by name
068: private WebsiteData mWebsite = null;
069: private Date mDate = null;
070: private FolderData mFolder = null;
071: private String mPageName = null;
072: private WeblogPageRequest mPageRequest = null;
073:
074: /**
075: * Initialize VelocityHelper, setting the variables it will be hiding from
076: * the Velocimacros.
077: */
078: public OldPageHelper(HttpServletRequest request,
079: HttpServletResponse response, Map ctx, WebsiteData website,
080: Date date, FolderData folder, String pageName,
081: PageContext pageContext, WeblogPageRequest pageRequest)
082: throws RollerException {
083:
084: // general request objects
085: mRequest = request;
086: mResponse = response;
087: mPageContext = pageContext;
088: mPageRequest = pageRequest;
089:
090: // data that we'll be reusing
091: mWebsite = website;
092: mDate = date;
093: mFolder = folder;
094:
095: // init plugins
096: Roller roller = RollerFactory.getRoller();
097: PluginManager ppmgr = roller.getPagePluginManager();
098: mPagePlugins = ppmgr.getWeblogEntryPlugins(mWebsite);
099: }
100:
101: /**
102: * Another stupid helper method to make up for the shortcomings of Velocity.
103: * @return HashMap
104: */
105: public Hashtable addParam(String key, String value, Hashtable map) {
106: if (map == null)
107: map = new Hashtable();
108: if (key != null && value != null)
109: map.put(key, value);
110: return map;
111: }
112:
113: /**
114: * Evaluates the String as a Velocimacro, returning the results.
115: *
116: * @deprecated shouldn't be used anymore because it's dangerous
117: *
118: * @param str String
119: * @return String
120: */
121: public String evaluateString(String str) {
122: // we no longer allow users to do this because it is dangerous
123: return str;
124: }
125:
126: /** Build the URL for editing an WeblogEntry **/
127: public String getEntryEditUrl(WeblogEntryDataWrapper entry) {
128: Hashtable params = new Hashtable();
129: params.put(RequestConstants.WEBLOGENTRY_ID, entry.getId());
130: params.put(RequestConstants.ANCHOR, entry.getAnchor());
131: if (mWebsite != null) {
132: params.put(RequestConstants.USERNAME, mWebsite.getHandle());
133: }
134: try {
135: return RequestUtils.computeURL(mPageContext, "weblogEdit",
136: null, null, null, params, null, false);
137: } catch (MalformedURLException mue) {
138: mLogger.warn("RollerRequest.editEntryUrl exception: ", mue);
139: }
140: return mRequest.getContextPath()
141: + "edtior/weblog.do?method=edit";
142: }
143:
144: public String getToggleLinkbackDisplayHTML(
145: RefererDataWrapper referer) {
146: String ret = "";
147: String link = null;
148: try {
149: RollerSession rollerSession = RollerSession
150: .getRollerSession(mRequest);
151: if (mWebsite != null
152: && rollerSession.isUserAuthorizedToAdmin(mWebsite)) {
153: Hashtable params = new Hashtable();
154: params.put(RequestConstants.REFERRER_ID, referer
155: .getId());
156: params.put(RequestConstants.WEBLOG, mWebsite
157: .getHandle());
158: link = RequestUtils.computeURL(mPageContext,
159: "toggleLinkback", null, null, null, params,
160: null, false);
161:
162: StringBuffer sb = new StringBuffer();
163: sb.append("[<a href=\"");
164: sb.append(link);
165: if (referer.getVisible().booleanValue()) {
166: sb.append("\">Visible</a>] ");
167: } else {
168: sb.append("\">Hidden</a>] ");
169: }
170: ret = sb.toString();
171: }
172: } catch (Exception e) {
173: // should never happen, but if it does:
174: mLogger.error("ERROR creating toggle-linkback URL", e);
175: }
176:
177: return ret;
178: }
179:
180: public boolean isUserAuthorizedToEdit() {
181: try {
182: RollerSession rses = RollerSession
183: .getRollerSession(mRequest);
184: if (rses.getAuthenticatedUser() != null && mWebsite != null) {
185: return rses.isUserAuthorizedToAdmin(mWebsite);
186: }
187: } catch (Exception e) {
188: mLogger.warn("PageHelper.isUserAuthorizedToEdit)", e);
189: }
190: return false;
191: }
192:
193: public void setContentType(String type) {
194: mResponse.setContentType(type);
195: }
196:
197: /**
198: * Display big weblog calendar, well suited for an archive page.
199: * @return HTML for calendar.
200: */
201: public String showBigWeblogCalendar() {
202: return showWeblogCalendar(true, null);
203: }
204:
205: /**
206: * Call hybrid EditorNavBarTag to render editor navbar.
207: * @param vertical True for vertical navbar.
208: * @return String HTML for navbar.
209: */
210: public String showEditorNavBar(boolean vertical) {
211: EditorNavigationBarTag editorTag = new EditorNavigationBarTag();
212: editorTag.setPageContext(mPageContext);
213: if (vertical) {
214: editorTag.setView("templates/navbar/navbar-vertical.vm");
215: } else {
216: editorTag.setView("templates/navbar/navbar-horizontal.vm");
217: }
218: editorTag.setModel("editor-menu.xml");
219: return editorTag.emit();
220: }
221:
222: /**
223: * Call hybrid EditorNavBarTag to render editor navbar.
224: * @param model Name of XML file in WEB-INF that contains XML for menu.
225: * @param template Name of Velocity template in classpath to display menu.
226: * @return String HTML for menu.
227: */
228: public String showMenu(String model, String template) {
229: MenuTag menuTag = new MenuTag();
230: menuTag.setPageContext(mPageContext);
231: menuTag.setModel(model);
232: menuTag.setView(template);
233: return menuTag.emit();
234: }
235:
236: //------------------------------------------------- WeblogCalendar methods
237:
238: /**
239: * Display weblog calendar.
240: * @return HTML for calendar.
241: */
242: public String showWeblogCalendar() {
243: return showWeblogCalendar(false, null);
244: }
245:
246: /**
247: * Weblog calendar display implementation.
248: * @param big Show big archive style calendar.
249: * @return HTML for calendar.
250: */
251: public String showWeblogCalendar(boolean big, String cat) {
252: if (OldWeblogPageModel.VELOCITY_NULL.equals(cat))
253: cat = null;
254: String ret = null;
255: try {
256: // setup weblog calendar model
257: CalendarModel model = null;
258: if (big) {
259: model = new BigWeblogCalendarModel(mPageRequest, cat);
260: } else {
261: model = new WeblogCalendarModel(mPageRequest, cat);
262: }
263:
264: // save model in JSP page context so CalendarTag can find it
265: mPageContext.setAttribute("calendarModel", model);
266:
267: // Create and setup calendar tag
268: CalendarTag calTag = new CalendarTag();
269: calTag.setPageContext(mPageContext);
270: calTag.setName("calendar");
271: calTag.setModel("calendarModel");
272: calTag.setLocale(mPageRequest.getLocaleInstance());
273: if (big) {
274: calTag.setClassSuffix("Big");
275: }
276: ret = calTag.emit();
277: } catch (Exception e) {
278: mLogger.error("Unexpected exception", e);
279: }
280: return ret;
281: }
282:
283: /**
284: * Convenience method, contrived helper for Velocity.
285: * @param useIds
286: * @param isAction
287: * @param path
288: * @param val1
289: * @param val2
290: * @return String
291: */
292: public String strutsUrlHelper(boolean useIds, boolean isAction,
293: String path, String val1, String val2) {
294: Hashtable params = new Hashtable();
295: return strutsUrlHelper1(useIds, isAction, path, val1, val2,
296: params);
297: }
298:
299: /**
300: * Very contrived helper method for Velocimacros generating Struts links.
301: * This is really only of use to the showNavBar macro.
302: * @param useIds
303: * @param isAction
304: * @param path
305: * @param val1
306: * @param val2
307: * @return String
308: */
309: public String strutsUrlHelper1(boolean useIds, boolean isAction,
310: String path, String val1, String val2, Hashtable params) {
311: if (useIds) {
312: if (mFolder != null) {
313: params.put(RequestConstants.FOLDER_ID, mFolder.getId());
314: }
315: if (mWebsite != null) {
316: params.put(RequestConstants.WEBLOG, mWebsite
317: .getHandle());
318: }
319: }
320:
321: if (OldStringUtils.isNotEmpty(val1) && !val1.equals("null")) {
322: params.clear();
323: params.put("weblog", val1);
324: }
325:
326: String returnUrl = "";
327: try {
328: if (isAction) {
329: returnUrl = RequestUtils.computeURL(mPageContext, path,
330: null, null, null, params, null, false);
331: } else {
332: returnUrl = RequestUtils.computeURL(mPageContext, null,
333: path, null, null, params, null, false);
334: }
335: } catch (MalformedURLException mue) {
336: mLogger.warn("RollerRequest.strutsUrlHelper exception: ",
337: mue);
338: returnUrl = "<span class=\"error\">ERROR generating link</span>";
339: }
340: return returnUrl;
341: }
342:
343: /**
344: * Pass the String through any PagePlugins that have been
345: * assigned to the PageHelper, as selected by the Entry.
346: *
347: * @param entry Entry being rendered.
348: * @param str String to which plugins are to be applied.
349: * @return Result of applying plugins to str.
350: */
351: public String renderPlugins(WeblogEntryDataWrapper entry, String str) {
352: String ret = str;
353: mLogger.debug("Applying page plugins to string");
354:
355: if (mPagePlugins != null) {
356: List entryPlugins = entry.getPluginsList();
357: // if no Entry plugins, don't bother looping.
358: if (entryPlugins != null && !entryPlugins.isEmpty()) {
359:
360: // now loop over mPagePlugins, matching
361: // against Entry plugins (by name):
362: // where a match is found render Plugin.
363: Iterator iter = mPagePlugins.keySet().iterator();
364: while (iter.hasNext()) {
365: String key = (String) iter.next();
366: if (entryPlugins.contains(key)) {
367: WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin) mPagePlugins
368: .get(key);
369: try {
370: ret = pagePlugin.render(entry.getPojo(),
371: ret);
372: } catch (Throwable t) {
373: mLogger.error("ERROR from plugin: "
374: + pagePlugin.getName(), t);
375: }
376: }
377: }
378: }
379: }
380:
381: return ret;
382: }
383:
384: /**
385: * This method used to return an array of supported locales based on some
386: * of the old i18n work done in Roller, however, as of Roller 3.0 there is
387: * no longer a list of supported languages. The languages available to a
388: * weblog are unbounded and are purely determined by the weblog author.
389: *
390: * This method always returns null.
391: */
392: public Locale[] getSupportedLanguages() {
393: return null;
394: }
395:
396: /**
397: * @return relative URL to page, starting with /username
398: */
399: public String getPathInfo() {
400: String pathInfo = mRequest.getPathInfo();
401: if (pathInfo == null) {
402: pathInfo = "";
403: }
404:
405: return pathInfo;
406: }
407:
408: public String getCommentAuthenticatorHtml() {
409: // deprecated, does nothing now
410: return "";
411: }
412:
413: }
|