skinutils.java
来自「Jive 是一个系统工程」· Java 代码 · 共 822 行 · 第 1/3 页
JAVA
822 行
Cookie cookie = new Cookie(JIVE_AUTOLOGIN_COOKIE, encodePasswordCookie(username, password)); cookie.setMaxAge(MAX_COOKIE_AGE); response.addCookie(cookie); } return authToken; } /** * Invalidates the cookie that otherwise lets a user auto-login. * * @param request The HttpServletRequest object, known as "request" in a JSP page. * @param response The HttpServletResponse object, known as "response" in a JSP page. */ public static void removeUserAuthorization( HttpServletRequest request, HttpServletResponse response ) { HttpSession session = request.getSession(); session.removeValue(JIVE_AUTH_TOKEN); Cookie cookie = new Cookie(JIVE_AUTOLOGIN_COOKIE, null); cookie.setMaxAge(0); response.addCookie(cookie); } /** * */ public static void invalidateCookie( HttpServletRequest request, HttpServletResponse response, String cookieName ) { Cookie cookie = new Cookie( cookieName, null ); // invalidate cookie cookie.setMaxAge(0); // deletes cookie response.addCookie(cookie); } /** * Persists a value for the length of the user's session. * * @see SkinUtils#store(HttpServletRequest,HttpServletResponse,String,String,int) store */ public static void store( HttpServletRequest request, HttpServletResponse response, String id, String value ) { store( request,response,id,value,0,false ); } public static void store( HttpServletRequest request, HttpServletResponse response, String id, String value, int secsToLive ) { store( request,response,id,value,secsToLive,false ); } /** * This method should be used in a jsp skin to store an arbritary value. * For example, we could persist the name of a user so that on a form page * where they enter their name, that field could be auto-filled in with * the stored value. * <p> * To indicate that the data should only be persisted for a session, pass * in 0 as the <code>timeToLive</code>. * * @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 name or identifier of the data you want to persist. * @param value The value you wish to store. * @param secsToLive The length (in seconds) this value will persist. Any value of 0 or * less indicates this data should only persist for a session. */ public static void store( HttpServletRequest request, HttpServletResponse response, String id, String value, int secsToLive, boolean restoreInSession ) { // This method uses sessions and cookies to persist data. We always store // it in the user's session. We'll only set it in a cookie if the // 'timeToLive' parameter is > 0. // If the id is null, return if( id == null ) { return; } // Get the session object: HttpSession session = request.getSession(); // check to see if the value already exists in the session -- if it does, // don't restore it unless specified if( ((String)session.getValue(id)) != null && !restoreInSession ) { return; } // At this point, restore (or store for the first time) the value in the session // Servlet API 2.1 call. Used to preserve compatibility with older app // servers. You might get deprecation warnings. session.putValue(id,value); // if the timeToLive param is > 0, store to the cookie: if( secsToLive > 0 ) { Cookie cookie = new Cookie(id,value); cookie.setMaxAge(secsToLive); response.addCookie(cookie); } } /** * Retrieves 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. */ public static String retrieve( HttpServletRequest request, HttpServletResponse response, String id ) { // just retrieve the value, don't remove it from persistence return( retrieve( request,response,id,false ) ); } /** * Retrieves a user stored value. Values are set using the <code>store(...)</code> * methods. If <code>remove</code> is true, the value is also removed * from persistence. * * @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. */ public static String retrieve( HttpServletRequest request, HttpServletResponse response, String id, boolean remove ) { // First, check the session. HttpSession session = request.getSession(); String value = (String)session.getValue(id); // if it's not found, check the cookies if( value == null ) { value = getCookieValue(request,id); } // remove it from persistence if indicated if( remove ) { remove( request,response,id ); } return value; } /** * Removes 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 remove( HttpServletRequest request, HttpServletResponse response, String id ) { // First, remove it from the session: HttpSession session = request.getSession(); session.removeValue(id); // Invalidate the cookie by setting a null expired cookie in its place Cookie cookie = new Cookie( id, null ); cookie.setMaxAge(0); response.addCookie(cookie); } /** * Returns the time in milliseconds that the user last visited Jive. */ public static long getLastVisited(HttpServletRequest request, HttpServletResponse response) { return getLastVisited(request,response,true); } /** * */ public static long getLastVisited(HttpServletRequest request, HttpServletResponse response, boolean updateLastVisitedTime) { //Get session object HttpSession session = request.getSession(); //The current instant in time. long now = System.currentTimeMillis(); //First, try to retrieve the value from the session String lastTime = (String)session.getValue(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 time to now, but don't update the // last visited time in the session: Cookie cookie = new Cookie(JIVE_LASTVISITED_TOKEN, Long.toString(now)); cookie.setMaxAge(60*60*24*30); response.addCookie(cookie); // 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 = -1; lastTime = getCookieValue(request,JIVE_LASTVISITED_TOKEN); if( lastTime != null ) { try { time = Long.parseLong(lastTime); } catch( NumberFormatException e ) {} } // set the value in the cookie, return the time if( time == -1 ) { time = now; } session.putValue(JIVE_LASTVISITED_TOKEN, Long.toString(time)); Cookie cookie = new Cookie(JIVE_LASTVISITED_TOKEN, Long.toString(now)); cookie.setMaxAge(60*60*24*30); response.addCookie(cookie); return time; } /** * Returns true if the message has been created or updated since * the last time the user visisted. * * @param message the message to check. * @param lastVisted the time the user last visisted the forum. * @return true if the message has been created or updated since the user's * last visit. */ public static boolean isNewMessage(ForumMessage message, long lastVisited) { if (message.getModifiedDate().getTime() > lastVisited) { return true; } else { return false; } } /** * Returns the specified Cookie object, or null 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 null. */ public static Cookie getCookie( HttpServletRequest request, String name ) { Cookie cookies[] = request.getCookies(); if(cookies == null || name == null || name.length() == 0) { return null; } //Otherwise, we have to 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 value of the specified cookie as a String. If the cookie * does not exist, the method returns null. * * @param request the HttpServletRequest object, known as "request" in a * JSP page. * @param name the name of the cookie * @return the value of the cookie, or null if the cookie does not exist. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?