📄 myutil.java
字号:
* Get the String with a slash character '/' before the locale name
* @param localeName the user's preferred locale
* @return the String with a slash character '/' before the locale name if
* this locale is configed to support it. Otherwise,
* an empty String will be return
*/
public static String getLocaleNameAndSlash(String localeName) {
if ( (localeName == null) || (localeName.length() == 0) ) {
return "";
}
String retValue = "";
String[] supportedLocales = MVNForumConfig.getSupportedLocaleNames();
if (supportedLocales == null) {
log.error("Assertion in MyUtil.getLocaleNameAndSlash. Please check your configuration.");
return "";
}
for (int i = 0; i < supportedLocales.length; i++) {
if (localeName.equals(supportedLocales[i])) {
retValue = "/" + localeName;
break;
}
}
return retValue;
}
public static String getCompanyCssPath(CompanyBean companyBean, String contextPath) {
String cssPath = null;
if (companyBean.getCompanyCss().length() > 0) {
cssPath = companyBean.getCompanyCss();
} else {
// use default company css
cssPath = MVNForumGlobal.COMPANY_DEFAULT_CSS_PATH;
}
return contextPath + cssPath;
}
public static String getCompanyLogoPath(CompanyBean companyBean, String contextPath) {
String logoPath = null;
if (companyBean.getCompanyLogo().length() > 0) {
logoPath = companyBean.getCompanyLogo();
} else {
// use default company logo
logoPath = MVNForumGlobal.COMPANY_DEFAULT_LOGO_PATH;
}
return contextPath + logoPath;
}
/**
* Get the locale from locale name
* @param localeName : in this format la_CO_VA, eg. en_US
* @return the locale instance of the localeName
*/
public static Locale getLocale(String localeName) {
// now, find out the 3 elements of a locale: language, country, variant
String[] localeElement = StringUtil.getStringArray(localeName, "_");
String language = "";
String country = "";
String variant = "";
if (localeElement.length >= 1) {
language = localeElement[0];
}
if (localeElement.length >= 2) {
country = localeElement[1];
}
if (localeElement.length >= 3) {
variant = localeElement[2];
}
return new Locale(language, country, variant);
}
public static void ensureCorrectCurrentPassword(GenericRequest request)
throws BadInputException, AssertionException, DatabaseException, AuthenticationException {
if (request.isServletRequest()) {
ensureCorrectCurrentPassword(request.getServletRequest());
} else {
//@todo implement later
//throw new IllegalStateException("Not implemented.");
}
}
public static void ensureCorrectCurrentPassword(HttpServletRequest request)
throws BadInputException, AssertionException, DatabaseException, AuthenticationException {
OnlineUser onlineUser = OnlineUserManager.getInstance().getOnlineUser(request);
OnlineUserFactory onlineUserFactory = ManagerFactory.getOnlineUserFactory();
try {
if (onlineUser.getAuthenticationType() == OnlineUser.AUTHENTICATION_TYPE_REALM) {
onlineUserFactory.ensureCorrectPassword(onlineUser.getMemberName(), OnlineUserManager.PASSWORD_OF_METHOD_REALM, true);
} else if (onlineUser.getAuthenticationType() == OnlineUser.AUTHENTICATION_TYPE_CUSTOMIZATION) {
if(MVNForumConfig.getEnablePasswordlessAuth()) {
// dont need password
onlineUserFactory.ensureCorrectPassword(onlineUser.getMemberName(), OnlineUserManager.PASSWORD_OF_METHOD_CUSTOMIZATION, true);
} else {
// must have password
// @todo: implement this case by using Authenticator
onlineUserFactory.ensureCorrectPassword(onlineUser.getMemberName(), OnlineUserManager.PASSWORD_OF_METHOD_CUSTOMIZATION, true);
}
} else {
//This user did not login by REALM or CUSTOMIZATION
String memberPassword = "";
String memberPasswordMD5 = ParamUtil.getParameter(request, "md5pw", false);
if (memberPasswordMD5.length() == 0 || (memberPasswordMD5.endsWith("==") == false)) {
// md5 is not valid, try to use unencoded password method
memberPassword = ParamUtil.getParameterPassword(request, "MemberCurrentMatkhau", 3, 0);
if (memberPassword.length() == 0) {
throw new AssertionException("Cannot allow memberPassword's length is 0. Serious Assertion Failed.");
}
}
// Please note that below code ONLY CORRECT when the ParamUtil.getParameterPassword
// return a NON-EMPTY string
if (memberPassword.length() > 0) {
// that is we cannot find the md5 password
onlineUserFactory.ensureCorrectPassword(onlineUser.getMemberName(), memberPassword, false);
} else {
// have the md5, go ahead
onlineUserFactory.ensureCorrectPassword(onlineUser.getMemberName(), memberPasswordMD5, true);
}
}
} catch (AuthenticationException e) {
Locale locale = I18nUtil.getLocaleInRequest(request);
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.wrong_password");
throw new BadInputException(localizedMessage);
//throw new BadInputException("You have typed the wrong password. Cannot proceed.");
}
}
public static void writeMvnForumImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedImage image = MVNForumInfo.getImage();
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
outputStream.flush();
} catch (IOException ex) {
throw ex;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) { }
}
}
}
public static void writeMvnCoreImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedImage image = MVNCoreInfo.getImage();
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
outputStream.flush();
} catch (IOException ex) {
throw ex;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) { }
}
}
}
public static void checkClassName(Locale locale, String className, boolean required) throws BadInputException {
if (required == false) {
if (className.length() == 0) return;
}
try {
Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new BadInputException("Cannot load class: " + className);
}
}
public static void saveVNTyperMode(GenericRequest request, GenericResponse response) {
if (request.isServletRequest()) {
saveVNTyperMode(request.getServletRequest(), response.getServletResponse());
}
}
public static void saveVNTyperMode(HttpServletRequest request, HttpServletResponse response) {
String vnTyperMode = ParamUtil.getParameter(request, "vnselector");
if (vnTyperMode.equals("VNI") || vnTyperMode.equals("TELEX") ||
vnTyperMode.equals("VIQR") || vnTyperMode.equals("NOVN")) {
Cookie typerModeCookie = new Cookie(MVNForumConstant.VN_TYPER_MODE, vnTyperMode);
typerModeCookie.setPath("/");
response.addCookie(typerModeCookie);
}
}
// note that this method can check for duplicate but difference in case: Admin,admin,ADMIN
public static Hashtable checkMembers(String[] memberNames, Locale locale)
throws AssertionException, DatabaseException, BadInputException {
Hashtable memberMap = new Hashtable();
boolean isFailed = false;
StringBuffer missingNames = new StringBuffer(512);
for (int i = 0; i < memberNames.length; i++) {
int receivedMemberID = -1;
String memberName = memberNames[i];
StringUtil.checkGoodName(memberName);
try {
receivedMemberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
} catch (ObjectNotFoundException ex) {
isFailed = true;
if (missingNames.length() > 0) {
missingNames.append(", ");
}
missingNames.append(memberName);
continue;
}
memberMap.put(new Integer(receivedMemberID), memberName);
} // end for
if (isFailed) { // the receivers does not exist.
String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.receivers_are_not_members", new Object[] {missingNames});
throw new BadInputException(localizedMessage);
}
return memberMap;
}
/**
* Check if the online user is Root Admin
*/
public static boolean isRootAdmin(GenericRequest request) throws AuthenticationException, AssertionException, DatabaseException {
return ( OnlineUserManager.getInstance().getOnlineUser(request).getMemberID() == MVNForumConstant.MEMBER_ID_OF_ADMIN);
}
/**
* Check if the online user is Root Admin
*/
public static boolean isRootAdmin(HttpServletRequest request) throws AuthenticationException, AssertionException, DatabaseException {
return ( OnlineUserManager.getInstance().getOnlineUser(request).getMemberID() == MVNForumConstant.MEMBER_ID_OF_ADMIN );
}
/**
* check if memberID belongs to admin's id
*/
public static boolean isRootAdminID(int memberID) {
return (memberID == MVNForumConstant.MEMBER_ID_OF_ADMIN);
}
public static String getRowCSS(int rowIndex) {
return (rowIndex%2 == 0 ? CSS_ROW_BODY : CSS_ROW_ALTERNATE);
}
// public static void checkInstance(String className, Class clazz) throws BadInputException {
// try {
// Class.forName(className);
// throw new BadInputException("Class " + className + " is not a implementation of " + clazz);
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// throw new BadInputException("Cannot load class " + className);
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -