⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skinutils.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @throws UnauthorizedException
     * @return The authorization token if authenticated, otherwise
     *      <code>null</code>
     */
    public static Authorization setUserAuthorization(HttpServletRequest request,
            HttpServletResponse response, String username, String password,
            boolean autoLogin) throws UserNotFoundException, UnauthorizedException
    {
        HttpSession session = request.getSession();
        Authorization authToken = AuthorizationFactory.getAuthorization(username, password);
        session.putValue(JIVE_AUTH_TOKEN, authToken);

        if (autoLogin) {
            Cookie cookie = new Cookie(JIVE_AUTOLOGIN_COOKIE, encodePasswordCookie(username, password));
            cookie.setMaxAge(MAX_COOKIE_AGE);
            cookie.setPath("/");
            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);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * Invalidate the specified cookie and delete it from the response object.
     *
     * @param request The HttpServletRequest object, known as "request" in a JSP page.
     * @param response The HttpServletResponse object, known as "response" in a JSP page.
     * @param cookieName The name of the cookie you want to delete.
     */
    public static void invalidateCookie( HttpServletRequest request, HttpServletResponse response, String cookieName ) {
        Cookie cookie = new Cookie( cookieName, null ); // invalidate cookie
        cookie.setMaxAge(0); // deletes cookie
        cookie.setPath("/");
        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 );
    }

    /**
     * Persists a value for the time (in seconds) specified
     *
     * @see SkinUtils#store(HttpServletRequest,HttpServletResponse,String,String,int) store
     */
    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);
            cookie.setPath("/");
            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.
     * @return The value of the specified id, otherwise <code>null</code>.
     */
    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.
     * @return The value of the specified id, otherwise <code>null</code>.
     */
    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);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * Returns the time in milliseconds that the user last visited Jive.
     *
     * @param request the HttpServletRequest object, known as "request" on a JSP page.
     * @param response the HttpServletRequest object, known as "response" on a JSP page.
     * @see SkinUtils#getLastVisited(HttpServletRequest,HttpServletResponse,boolean) getLastVisited
     * @return The time (in milliseconds) that the suer last visited Jive.
     */
    public static long getLastVisited(HttpServletRequest request,
            HttpServletResponse response)
    {
        return getLastVisited(request,response,true);
    }

    /**
     * Returns the time in milliseconds that the user last visited the Jive system.
     *
     * @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 updateLastVisitedTime Set to <code>true</code> if you wish to update
     * the user's last visited time to the current time; set to <code>false</code> otherwise.
     * @return The time (in milliseconds) that the suer last visited Jive.
     */
    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);
                cookie.setPath("/");
                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 = now;
        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
        session.putValue(JIVE_LASTVISITED_TOKEN, Long.toString(time));
        Cookie cookie = new Cookie(JIVE_LASTVISITED_TOKEN, Long.toString(now));
        cookie.setMaxAge(60*60*24*30);
        cookie.setPath("/");
        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.
     */
    public static String getCookieValue(HttpServletRequest request, String name) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -