📄 skinutils.java
字号:
* Deletes a user stored value. Values are set using the
* <code>store(...)</code>
* methods.
*
* @param request the HttpServletRequest object, known as "request" on
* a JSP page.
* @param response the HttpServletRequest object, known as "response" on
* a JSP page.
* @param id the id or name of the stored value you wish to remove from persistence.
*/
public static void delete(HttpServletRequest request,
HttpServletResponse response, String id)
{
// First, remove it from the session:
HttpSession session = request.getSession();
session.removeAttribute(id);
// Invalidate the cookie by setting a null expired cookie in its place
deleteCookie(request, response, id);
}
public static void remove(HttpServletRequest request,
HttpServletResponse response, String id)
{
delete(request,response,id);
}
/**
*
*
* @param request the HttpServletRequest object, known as "request" on
* a JSP page.
* @param response the HttpServletRequest object, known as "response" on
* a JSP page.
*/
public static long getLastVisited(HttpServletRequest request,
HttpServletResponse response)
{
// Get session object
HttpSession session = request.getSession();
// The current instant in time. We use the CacheTimer field because this
// method will be called often and this will scale better than calling
// System.currentTimeMillis().
long now = CacheTimer.currentTime;
// First, try to retrieve the value from the session
String lastTime = (String)session.getAttribute(JIVE_LASTVISITED_TOKEN);
// Found a value in the session, so return it
if (lastTime != null) {
try {
long time = Long.parseLong(lastTime);
// update the last visited cookie time to now, but don't update
// the last visited time in the session:
saveCookie(response, JIVE_LASTVISITED_TOKEN, Long.toString(now));
// return the time value
return time;
}
catch(NumberFormatException e) {
e.printStackTrace();
}
}
// getting to this point means no time value was found in the session,
// so look for it in the cookie:
long time = now;
Cookie cookie = getCookie(request, JIVE_LASTVISITED_TOKEN);
if (cookie != null) {
lastTime = cookie.getValue();
}
if (lastTime != null) {
try {
time = Long.parseLong(lastTime);
} catch (NumberFormatException e) {}
}
// set the value in the session & cookie
session.setAttribute(JIVE_LASTVISITED_TOKEN, Long.toString(time));
saveCookie(response, JIVE_LASTVISITED_TOKEN, Long.toString(now));
// return the time
return time;
}
/**
* Returns true if the forum has been modified since the specified time.
*
* @param forum the forum to check.
* @param time the time to reference the forum against.
* @return true if the forum has been modified since the specified time.
*/
public static boolean isNew(Forum forum, long time)
{
return (forum.getModifiedDate().getTime() > time);
}
/**
* Returns true if the forum thread has been modified since the specified
* time.
*
* @param thread the thread to check.
* @param time the time to reference the thread against.
* @return true if the thread has been modified since the specified time.
*/
public static boolean isNew(ForumThread thread, long time)
{
return (thread.getModifiedDate().getTime() > time);
}
/**
* Returns true if the forum message has been modified since the specified
* time.
*
* @param message the message to check.
* @param time the time to reference the message against.
* @return true if the message has been modified since the specified time.
*/
public static boolean isNew(ForumMessage message, long time)
{
return (message.getModifiedDate().getTime() > time);
}
/**
* Returns the specified cookie, or <code>null</code> if the cookie
* does not exist.
*
* @param request The HttpServletRequest object, known as "request" in a
* JSP page.
* @param name the name of the cookie.
* @return the Cookie object if it exists, otherwise <code>null</code>.
*/
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie cookies[] = request.getCookies();
// Return null if there are no cookies or the name is invalid.
if(cookies == null || name == null || name.length() == 0) {
return null;
}
// Otherwise, we do a linear scan for the cookie.
for (int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals(name) ) {
return cookies[i];
}
}
return null;
}
/**
* Returns the specified resource bundle, which is a properties file
* that aids in localization of skins. This method is handy since it
* uses the class loader that other Jive classes are loaded from (hence,
* it can load bundles that are stored in jive.jar).
*
* @param baseName the name of the resource bundle to load.
* @param locale the desired Locale.
* @return the specified resource bundle, if it exists.
*/
public static ResourceBundle getResourceBundle(String baseName,
Locale locale)
{
return ResourceBundle.getBundle(baseName, locale);
}
/**
* Stores a value in a cookie. This cookie will persist for 30 days.
*
* @param request The HttpServletResponse object, known as "response" in a
* JSP page.
* @param name a name to identify the cookie
* @param value the value to store in the cookie
*/
public static void saveCookie(HttpServletResponse response, String name,
String value)
{
// Save the cookie value for 1 month
saveCookie(response, name, value, 60*60*24*30);
}
/**
* Stores a value in a cookie. This cookie will persist for the amount
* specified in the <tt>saveTime</tt> parameter.
*
* @param request The HttpServletResponse object, known as "response" in a
* JSP page.
* @param name a name to identify the cookie
* @param value the value to store in the cookie
* @param saveTime the time (in seconds) this cookie should live
*/
public static void saveCookie(HttpServletResponse response, String name,
String value, int saveTime)
{
// Check to make sure the new value is not null (appservers like Tomcat
// 4 blow up if the value is null).
if (value == null) {
value = "";
}
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(saveTime);
cookie.setPath("/");
response.addCookie(cookie);
}
/**
* Formats the unfiltered body of a message to make it appear in the "quote
* original" format. This is simply the body of the message with the
* delimiter appended to the beginning of each line. The delimiter
* is most often "> " by convention. A desired length for each line in the
* returned String can be specified to aid in formatting.<p>
*
* This method uses message.getUnfilteredBody() in order to get the body of
* the message. This usually yields better results for the formatting
* required by this method. However, it also has the potential of being
* a security risk if malicious HTML code is embedded in the body. Therefore,
* you should always filter HTML from the result of this method before
* showing it in an environment where HTML is interpreted. If you are
* showing the results of this method in an HTML <textarea>, there is
* no need to worry about malicious HTML.
*
* @param message the message to quote.
* @param delimiter a String that will start each line of the quoted
* message. For example, "> ";
* @param lineLength the desired length of each line in the quoted message.
* @return the unfiltered body of the message in the "quote original" format.
*/
public static String quoteOriginal(String body, String delimiter,
int lineLength)
{
if (body == null || body.length() == 0) {
return "";
}
if (body.trim().length() == 0) {
return "";
}
int length = body.length();
//Create a StringBuffer to hold the quoted body; approximate size.
StringBuffer buf = new StringBuffer(body.length());
//i maintains the current position in the String.
for (int i=0; i<length; ) {
String partialString =
StringUtils.chopAtWord(
body.substring(i),
lineLength
);
i += partialString.length()+1;
buf.append(delimiter).append(partialString.trim()).append("\n");
}
return buf.toString();
}
/**
* Formats a date for a user, according to their locale and time zone
* preferences.
*
* @param request the servlet request object.
* @param response the servlet response object.
* @param user the User that the date is being formatted for.
* @param date the Date object we're comparing the current time with.
*/
public static String formatDate(HttpServletRequest request,
HttpServletResponse response, User user, Date date)
{
Locale locale = JiveGlobals.getLocale();
TimeZone timeZone = getTimeZone(request, response, user);
// See if the date is today.
// Check the cache of DateFormat objects:
String key = locale.toString() + timeZone.getID();
if (dateFormatCache.containsKey(key)) {
return ((SimpleDateFormat)dateFormatCache.get(key)).format(date);
}
else {
// Cache miss
// DateFormat formatter
// = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT,locale);
// formatter.setTimeZone(timeZone);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
dateFormatCache.put(key,formatter);
return formatter.format(date);
}
}
/**
* Returns an English sentence which describes the <code>Date</code>
* parameter relative to the current time. For instance, if the passed
* in date was 39 seconds ago, this method returns:<p>
* "Less than 1 min ago"<p>
* Similiarly, a date 1 ago would be:<p>
* "Yesterday at 3:53 PM"<p>
*
* The method obeys user preferences for time zone, if they exist.
*
* @param request the servlet request object.
* @param response the servlet response object.
* @param user the User that the date is being formatted for, or null
* if an anonymmous user.
* @param date the Date object we're comparing the current time with.
*/
public static String dateToText(HttpServletRequest request,
HttpServletResponse response, User user, Date date)
{
if (date == null) {
return "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -