001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 02/04/2004 - 20:31:35
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum.common;
044:
045: import java.io.UnsupportedEncodingException;
046: import java.net.URI;
047: import java.net.URLEncoder;
048: import java.text.SimpleDateFormat;
049: import java.util.Date;
050:
051: import org.apache.commons.lang.StringUtils;
052:
053: import net.jforum.JForumExecutionContext;
054: import net.jforum.context.RequestContext;
055: import net.jforum.entities.User;
056: import net.jforum.exceptions.ForumException;
057: import net.jforum.util.preferences.ConfigKeys;
058: import net.jforum.util.preferences.SystemGlobals;
059: import net.jforum.util.preferences.TemplateKeys;
060: import freemarker.template.SimpleHash;
061:
062: /**
063: * @author Rafael Steil
064: * @version $Id: ViewCommon.java,v 1.32 2007/09/20 16:07:08 rafaelsteil Exp $
065: */
066: public final class ViewCommon {
067: /**
068: * Prepared the user context to use data pagination.
069: * The following variables are set to the context:
070: * <p>
071: * <ul>
072: * <li> <i>totalPages</i> - total number of pages
073: * <li> <i>recordsPerPage</i> - how many records will be shown on each page
074: * <li> <i>totalRecords</i> - number of records fount
075: * <li> <i>thisPage</i> - the current page being shown
076: * <li> <i>start</i> -
077: * </ul>
078: * </p>
079: * @param start int
080: * @param totalRecords int
081: * @param recordsPerPage int
082: */
083: public static void contextToPagination(int start, int totalRecords,
084: int recordsPerPage) {
085: SimpleHash context = JForumExecutionContext
086: .getTemplateContext();
087:
088: context
089: .put("totalPages", new Double(Math
090: .ceil((double) totalRecords
091: / (double) recordsPerPage)));
092: context.put("recordsPerPage", new Integer(recordsPerPage));
093: context.put("totalRecords", new Integer(totalRecords));
094: context.put("thisPage", new Double(Math
095: .ceil((double) (start + 1) / (double) recordsPerPage)));
096: context.put("start", new Integer(start));
097: }
098:
099: /**
100: * Prepares the template context to show the login page, using the current URI as return path.
101: * @return TemplateKeys.USER_LOGIN
102: */
103: public static String contextToLogin() {
104: RequestContext request = JForumExecutionContext.getRequest();
105:
106: String uri = request.getRequestURI();
107: String query = request.getQueryString();
108: String returnPath = query == null ? uri : uri + "?" + query;
109:
110: return contextToLogin(returnPath);
111: }
112:
113: /**
114: * Prepares the template context to show the login page, using "returnPath" as return path
115: * @param returnPath the URI to use as return path
116: * @return TemplateKeys.USER_LOGIN
117: */
118: public static String contextToLogin(String returnPath) {
119: JForumExecutionContext.getTemplateContext().put("returnPath",
120: returnPath);
121:
122: if (ConfigKeys.TYPE_SSO.equals(SystemGlobals
123: .getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
124: String redirect = SystemGlobals
125: .getValue(ConfigKeys.SSO_REDIRECT);
126:
127: if (!StringUtils.isEmpty(redirect)) {
128: URI redirectUri = URI.create(redirect);
129:
130: if (!redirectUri.isAbsolute()) {
131: throw new ForumException(
132: "SSO redirect URL should start with a scheme");
133: }
134:
135: try {
136: returnPath = URLEncoder.encode(ViewCommon
137: .getForumLink()
138: + returnPath, "UTF-8");
139: } catch (UnsupportedEncodingException e) {
140: }
141:
142: if (redirect.indexOf('?') == -1) {
143: redirect += "?";
144: } else {
145: redirect += "&";
146: }
147:
148: redirect += "returnUrl=" + returnPath;
149:
150: JForumExecutionContext.setRedirect(redirect);
151: }
152: }
153:
154: return TemplateKeys.USER_LOGIN;
155: }
156:
157: /**
158: * Returns the initial page to start fetching records from.
159: *
160: * @return The initial page number
161: */
162: public static int getStartPage() {
163: String s = JForumExecutionContext.getRequest().getParameter(
164: "start");
165: int start;
166:
167: if (StringUtils.isEmpty(s)) {
168: start = 0;
169: } else {
170: start = Integer.parseInt(s);
171:
172: if (start < 0) {
173: start = 0;
174: }
175: }
176:
177: return start;
178: }
179:
180: /**
181: * Gets the forum base link.
182: * The returned link has a trailing slash
183: * @return The forum link, with the trailing slash
184: */
185: public static String getForumLink() {
186: String forumLink = SystemGlobals
187: .getValue(ConfigKeys.FORUM_LINK);
188:
189: if (forumLink.charAt(forumLink.length() - 1) != '/') {
190: forumLink += "/";
191: }
192:
193: return forumLink;
194: }
195:
196: public static String toUtf8String(String s) {
197: StringBuffer sb = new StringBuffer();
198:
199: for (int i = 0; i < s.length(); i++) {
200: char c = s.charAt(i);
201:
202: if ((c >= 0) && (c <= 255)) {
203: sb.append(c);
204: } else {
205: byte[] b;
206:
207: try {
208: b = Character.toString(c).getBytes("utf-8");
209: } catch (Exception ex) {
210: System.out.println(ex);
211: b = new byte[0];
212: }
213:
214: for (int j = 0; j < b.length; j++) {
215: int k = b[j];
216:
217: if (k < 0) {
218: k += 256;
219: }
220:
221: sb.append('%').append(
222: Integer.toHexString(k).toUpperCase());
223: }
224: }
225: }
226:
227: return sb.toString();
228: }
229:
230: /**
231: * Formats a date using the pattern defined in the configuration file.
232: * The key is the value of {@link net.jforum.util.preferences.ConfigKeys.DATE_TIME_FORMAT}
233: * @param date the date to format
234: * @return the string with the formated date
235: */
236: public static String formatDate(Date date) {
237: SimpleDateFormat df = new SimpleDateFormat(SystemGlobals
238: .getValue(ConfigKeys.DATE_TIME_FORMAT));
239: return df.format(date);
240: }
241:
242: /**
243: * Escapes < by &lt; and > by &gt;
244: * @param contents the string to parse
245: * @return the new string
246: */
247: public static String espaceHtml(String contents) {
248: StringBuffer sb = new StringBuffer(contents);
249:
250: replaceAll(sb, "<", "<");
251: replaceAll(sb, ">", ">");
252:
253: return sb.toString();
254: }
255:
256: /**
257: * Replaces some string with another value
258: * @param sb the StrinbBuilder with the contents to work on
259: * @param what the string to be replaced
260: * @param with the new value
261: * @return the new string
262: */
263: public static String replaceAll(StringBuffer sb, String what,
264: String with) {
265: int pos = sb.indexOf(what);
266:
267: while (pos > -1) {
268: sb.replace(pos, pos + what.length(), with);
269: pos = sb.indexOf(what);
270: }
271:
272: return sb.toString();
273: }
274:
275: /**
276: * @see #replaceAll(StringBuffer, String, String)
277: * @param contents String
278: * @param what String
279: * @param with String
280: * @return String
281: */
282: public static String replaceAll(String contents, String what,
283: String with) {
284: return replaceAll(new StringBuffer(contents), what, with);
285: }
286:
287: /**
288: * Parse the user's signature, to make it proper to visualization
289: * @param u the user instance
290: */
291: public static void prepareUserSignature(User u) {
292: if (u.getSignature() != null) {
293: StringBuffer sb = new StringBuffer(u.getSignature());
294:
295: replaceAll(sb, "\n", "<br />");
296:
297: u.setSignature(sb.toString());
298: u.setSignature(PostCommon
299: .prepareTextForDisplayExceptCodeTag(u
300: .getSignature(), true, true));
301: }
302: }
303: }
|