skinutils.java
来自「Jive 是一个系统工程」· Java 代码 · 共 822 行 · 第 1/3 页
JAVA
822 行
public static String getCookieValue(HttpServletRequest request, String name) { Cookie cookie = getCookie(request,name); if(cookie != null) { return cookie.getValue(); } return null; } /** * 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 ""; } 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 ); System.out.println("--" + partialString); i += partialString.length()+1; buf.append(delimiter).append(partialString.trim()).append("\\n"); } return buf.toString(); } /** * Returns a String describing the amount of time between now (current * system time) and the passed in date time. Example output is "5 hours * ago" or "Yesterday at 3:30 pm" * * @param date the Date to compare the current time with. * @return a description of the difference in time, ie: "5 hours ago" * or "Yesterday at 3:30pm" */ public static String dateToText( Date date ) { if( date == null ) { return ""; } long delta = System.currentTimeMillis() - date.getTime(); // within the last hour if( (delta / HOUR) < 1 ) { long minutes = (delta/MINUTE); if( minutes == 0 ) { return "Less than 1 min ago"; } else if( minutes == 1 ) { return "1 minute ago"; } else { return ( minutes + " minutes ago" ); } } // sometime today if( (delta / DAY) < 1 ) { long hours = (delta/HOUR); if( hours <= 1 ) { return "1 hour ago"; } else { return ( hours + " hours ago" ); } } // within the last week if( (delta / WEEK) < 1 ) { long days = (delta/DAY); if( days <= 1 ) { return yesterdayFormatter.format(date); } else { return dateFormatter.format(date); } } // before a week ago else { return dateFormatter.format(date); } } /** * test method for this class */ /* public static void main( String args[] ) { Calendar cal = Calendar.getInstance(); System.out.println( "now:\t" + dateToText(cal.getTime()) ); for( int i=0; i<122; i++ ) { cal.setTime( new Date(cal.getTime().getTime() - (30*MINUTE) ) ); System.out.println( (i+1) + " min ago:\t" + dateToText(cal.getTime()) + "\t" + cal.getTime() ); } } */ /** * Returns true if the user is a system administrator. * * @param authToken the authentication token of the user * @return true if the user is a system administrator, false otherwise. */ public static boolean isSystemAdmin( Authorization authToken ) { ForumFactory forumFactory = ForumFactory.getInstance(authToken); ForumPermissions permissions = forumFactory.getPermissions(authToken); return permissions.get(ForumPermissions.SYSTEM_ADMIN); } /** * Returns true if the user is a forum adminstrator of any forum in the * system. For example, if there are 3 forums in the system and the user * is an adminstrator of any one or more of them, this method will return * true.<p> * * Use the method <code>isForumAdmin( Authorization, Forum)</code> to * check an individual forum for administrator status.) * * @param authToken the authentication token of the user * @return true if the user is a forum administrator of any forum in the system. * @see SkinUtils#isForumAdmin(Authorization, Forum) */ public static boolean isForumAdmin( Authorization authToken ) { ForumFactory forumFactory = ForumFactory.getInstance(authToken); Iterator forumIterator = forumFactory.forums(); if( !forumIterator.hasNext() ) { return false; } while( forumIterator.hasNext() ) { Forum forum = (Forum)forumIterator.next(); if( forum.hasPermission(ForumPermissions.FORUM_ADMIN) ) { return true; } } return false; } /** * Returns true if the user is a forum adminstrator of the given forum. * * @param authToken the authentication token of the user * @param forum the forum to check administrator status on. * @return true if the user is a forum administrator of the given forum. */ public static boolean isForumAdmin( Authorization authToken, Forum forum ) { return( forum.hasPermission(ForumPermissions.FORUM_ADMIN) ); } /** * Returns true if the user is a group administrator of any group in the * system. For example, if there are 3 groups in the system and the user * is an adminstrator of any one or more of them, this method will return * true.<p> * * Use the method <code>isGroupAdmin( Authorization, Group)</code> to check * an individual group for administrator status.) * * @see SkinUtils#isGroupAdmin(Authorization, Group) */ public static boolean isGroupAdmin( Authorization authToken ) { ForumFactory forumFactory = ForumFactory.getInstance(authToken); ProfileManager manager = forumFactory.getProfileManager(); Iterator groupIterator = manager.groups(); if( !groupIterator.hasNext() ) { return false; } while( groupIterator.hasNext() ) { Group group = (Group)groupIterator.next(); if( group.hasPermission(ForumPermissions.GROUP_ADMIN) ) { return true; } } return false; } /** * Returns true if the user is a group administrator of the given group. * * @param authToken the authentication token of the user * @param group the group to check administrator status on. * @return true if the user is a group administrator of the given group. */ public static boolean isGroupAdmin( Authorization authToken, Group group ) { return( group.hasPermission(ForumPermissions.GROUP_ADMIN) ); } /** * Builds a cookie string containing a username and password.<p> * * Note: with open source this is not really secure, but it prevents users * from snooping the cookie file of others and by changing the XOR mask and * character offsets, you can easily tweak results. * * @param username The username. * @param password The password. * @return String encoding the input parameters, an empty string if one of * the arguments equals <code>null</code>. */ private static String encodePasswordCookie (String username, String password) { StringBuffer buf = new StringBuffer(); if (username != null && password != null) { byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes(); int b; for (int n = 0; n < bytes.length; n++) { b = bytes[n] ^ (ENCODE_XORMASK + n); buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F))); buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F))); } } return buf.toString(); } /** * Unrafels a cookie string containing a username and password. * @param value The cookie value. * @return String[] containing the username at index 0 and the password at * index 1, or <code>{ null, null }</code> if cookieVal equals * <code>null</code> or the empty string. */ private static String[] decodePasswordCookie( String cookieVal ) { // check that the cookie value isn't null or zero-length if( cookieVal == null || cookieVal.length() <= 0 ) { return null; } // unrafel the cookie value char[] chars = cookieVal.toCharArray(); byte[] bytes = new byte[chars.length / 2]; int b; for (int n = 0, m = 0; n < bytes.length; n++) { b = chars[m++] - ENCODE_CHAR_OFFSET1; b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4; bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n)); } cookieVal = new String(bytes); int pos = cookieVal.indexOf(ENCODE_DELIMETER); String username = (pos < 0) ? "" : cookieVal.substring(0, pos); String password = (pos < 0) ? "" : cookieVal.substring(pos + 1); return new String[] { username, password }; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?