?? skinutils.java
字號:
package com.nextier.model;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
/**
* A collection of utility methods for use in OA Skins. Because these
* methods make skin development much easier, skin authors should study them
* carefully.<p>
*
* Three major areas of funtionality are provided:<p><ol>
* <li> Methods that simplify Authorization tasks:
* <ul>
* <li>{@link #login(HttpServletRequest, HttpServletResponse, String, String, boolean)}
* <li>{@link #getUserAuthorization(HttpServletRequest, HttpServletResponse)}
* <li>{@link #removeUserAuthorization(HttpServletRequest, HttpServletResponse)}
* </ul>
* <p>
* <li> Methods that get and set Session and cookie values.
* <ul>
* <li>{@link #getCookie(HttpServletRequest, String)}
* <li>{@link #remove(HttpServletRequest, HttpServletResponse, String)}
* <li>{@link #retrieve(HttpServletRequest, HttpServletResponse, String)}
* <li>{@link #store(HttpServletRequest, HttpServletResponse, String, String)}
* <li>{@link #store(HttpServletRequest, HttpServletResponse, String, String, int)}
* <li>{@link #store(HttpServletRequest, HttpServletResponse, String, String, int boolean)}
* </ul>
* <p>
* <li> Date methods.
* <ul>
* <li>{@link #dateToText(HttpServletRequest, HttpServletResponse, User, Date)}
* <li>{@link #formatDate(HttpServletRequest, HttpServletResponse, User, Date)}
* <li>{@link #getLastVisited(HttpServletRequest, HttpServletResponse)}
* </ul>
* </ol>
*/
public class SkinUtils {
/** Name of the cookie used to store user info for auto-login purposes */
private static final String OA_AUTOLOGIN_COOKIE = "oa.authorization.autologin";
// Default cookie time to live (in seconds).
// private static final int MAX_COOKIE_AGE = (int)(OAGlobals.WEEK / 1000) * 8;
// Days of the week
private static final String[] DAYS_OF_WEEK =
{"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
// Months of the year
private static final String[] MONTHS_OF_YEAR =
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};
// "Tweakable" parameters for the cookie password encoding. NOTE: changing
// these and recompiling this class will essentially invalidate old cookies.
private final static int ENCODE_XORMASK = 0x5A;
private final static char ENCODE_DELIMETER = '\002';
private final static char ENCODE_CHAR_OFFSET1 = 'A';
private final static char ENCODE_CHAR_OFFSET2 = 'h';
//logger
private final static Logger log = Logger.getLogger("fy");
// A reuseable global calendar object
private static Calendar globalCal = Calendar.getInstance();
public static Logger getLogger(){
return log;
}
/**
* Returns an Authorization token for the user. The session is first checked
* and if the token is not found, the OA cookie is checked. If the cookie
* is found,
*
* @param request the HttpServletRequest object, known as "request" in a
* JSP page.
* @param response The HttpServletResponse object, known as "response" in
* a JSP page.
* @return A users's authorization token if they're already authenticated,
* otherwise <code>null</code>.
*/
//public static Authorization getUserAuthorization(HttpServletRequest request,
//HttpServletResponse response){
//HttpSession session = request.getSession();
// Check 1: check for the OA authentication token in the user's session.
// Authorization authToken = (Authorization)session.getAttribute(OAGlobals.OA_AUTH_TOKEN);
//if (authToken != null) {
// return authToken;
// }
// Check 2: check the oa cookie for loginname and password
//Cookie cookie = getCookie(request, OA_AUTOLOGIN_COOKIE);
//if (cookie != null) {
//try {
// at this point, we found a cookie so grab the loginname and
// password from it, create an authorization token and store
// that in the session
// String[] values = decodePasswordCookie(cookie.getValue());
// String loginname = values[0];
// String password = values[1];
// Try to validate the user based on the info from the cookie.
// Catch any exceptions
// authToken = AuthorizationFactory.getAuthorization(loginname,password);
// }
// catch (Exception e) {}
// put that token in the user's session:
//if (authToken != null) {
// session.setAttribute(OAGlobals.OA_AUTH_TOKEN, authToken);
//}
// return the authorization token
// return authToken;
//}
//return null;
//}
//public static SecurityFactory getSecurityFactory(HttpServletRequest request,
// HttpServletResponse response){
//HttpSession session = request.getSession(true);
// SecurityFactory securityFactory = (SecurityFactory)session.getAttribute(OAGlobals.OA_SECURITY_FACTORY);
/// if (securityFactory != null)
// return securityFactory;
//return null;
//}
/**
* Validates the user and optionally enables auto-login by creating an
* auto-login cookie.
*
* @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 loginname the loginname.
* @param password the password.
* @param autoLogin if <code>true</code> create a cookie that enables auto-login.
* @throws UserNotFoundException
* @throws UnauthorizedException
*/
//public static Authorization login(HttpServletRequest request,
// HttpServletResponse response, String loginname, String password,
// boolean autoLogin) throws UserNotFoundException, UnauthorizedException
//{
//HttpSession session = request.getSession();
//Authorization authToken = AuthorizationFactory.getAuthorization(loginname, password);
// session.setAttribute(OAGlobals.OA_AUTH_TOKEN, authToken);
// If auto-login is enabled, create the auto-login cookie
//f (autoLogin) {
// saveCookie(response,OA_AUTOLOGIN_COOKIE,
// encodePasswordCookie(loginname,password));
// }
// return authToken;
// }
//public static Authorization setUserAuthorization(HttpServletRequest request,
// HttpServletResponse response, String loginname, String password,
// boolean autoLogin) throws UserNotFoundException, UnauthorizedException
//{
// return login(request, response, loginname, password, autoLogin);
// }
/**
* Removes a user's token from the session and invalidates the auto-login
* cookie (if one exists).
*
* @param request the HttpServletRequest object; "request" in JSP pages.
* @param response the HttpServletResponse object; "response" in JSP pages.
*/
// public static void logout(HttpServletRequest request,
// HttpServletResponse response)
// {
// HttpSession session = request.getSession();
// session.removeAttribute(OAGlobals.OA_AUTH_TOKEN);
// deleteCookie(request, response, OA_AUTOLOGIN_COOKIE);
// }
// public static void removeUserAuthorization(HttpServletRequest request,
// HttpServletResponse response)
// {
// logout(request,response);
// }
/**
* Invalidates the specified cookie.
*/
public static void deleteCookie(HttpServletRequest request,
HttpServletResponse response, String cookieName)
{
// invalidate the cookie
Cookie cookie = new Cookie(cookieName, "");
// delete the cookie when the user closes their webbrowser
cookie.setMaxAge(0);
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)
{
// By default, we'll just store the value in the session (saveTime
// is zero)
store(request, response, id, value, 0);
}
/**
* This method should be used in a 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>. Otherwise, the value will be
* saved for one month.
*
* @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 saveTime 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 saveTime)
{
// If the id is null, return
if (id == null) {
return;
}
// Get the session object
HttpSession session = request.getSession();
// Store the value in the session
session.setAttribute(id, value);
// if the timeToLive param is > 0, store to a cookie
if (saveTime > 0) {
saveCookie(response, id, value, saveTime);
}
}
/**
* Retrieves a user stored value. Values are set using the
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -