package com.perforce;
import java.security.Key;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import com.google.gson.Gson;
import com.perforce.model.UserInfo;
public class Utility {
private static byte[] RAW = "c9eea8c11230979f12b6fc88aa42f6ac".getBytes();
public static String encrypt(String value) {
byte[] encrypted = null;
try {
Key skeySpec = new SecretKeySpec(RAW, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec,ivParams);
encrypted = cipher.doFinal(value.getBytes());
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(Base64.encodeBase64(encrypted));
}
public static String decrypt(String encrypted) {
byte[] original = null;
Cipher cipher = null;
try {
Key key = new SecretKeySpec(RAW, "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
byte[] ivByte = new byte[cipher.getBlockSize()];
//This class specifies an initialization vector (IV). Examples which use
//IVs are ciphers in feedback mode, e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation.
IvParameterSpec ivParamsSpec = new IvParameterSpec(ivByte);
cipher.init(Cipher.DECRYPT_MODE, key, ivParamsSpec);
original= cipher.doFinal(Base64.decodeBase64(encrypted));
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(original);
}
public static String convertToEncryptedJson(Object obj) {
String json = convertToJson(obj);
return encrypt(json);
}
public static String convertToJson(Object obj) {
Gson gson = new Gson();
return gson.toJson(obj);
}
public static Object convertFromEncryptedJson(String encrypted, Class<?> t) {
String json = decrypt(encrypted);
return convertFromJson(json, t);
}
public static Object convertFromJson(String json, Class<?> t) {
Gson gson = new Gson();
return gson.fromJson(json, t);
}
public static P4Helper getHelper(HttpServletRequest request) throws ApplicationException {
return (P4Helper) request.getSession().getAttribute(Constants.P4HELPER_SESSIONKEY);
}
public static void transferAttribute(HttpServletRequest request, String paramName) {
String value = request.getParameter(paramName);
if(value != null && value.trim().length() > 0) {
request.setAttribute(paramName, value);
}
}
public static void sendEmail(String to, String subject, String messageText) throws ApplicationException {
sendEmail(to, subject, messageText, null);
}
public static void sendEmail(String to, String subject, String messageText, String htmlMessageText) throws ApplicationException {
try {
Email email = null;
if(htmlMessageText != null) {
email = new HtmlEmail();
} else {
email = new SimpleEmail();
}
email.setHostName(getStringAttribute(Constants.MAIL_HOST));
email.setSmtpPort(getIntAttribute(Constants.MAIL_PORT));
if ("true".equalsIgnoreCase(getStringAttribute(Constants.MAIL_AUTH))) {
email.setAuthenticator(new DefaultAuthenticator(getStringAttribute(Constants.MAIL_AUTH_USER), getStringAttribute(Constants.MAIL_AUTH_PASS)));
}
if("true".equalsIgnoreCase(getStringAttribute(Constants.MAIL_SSL))) {
email.setSSLOnConnect(true);
}
email.setFrom(getStringAttribute(Constants.MAIL_FROM));
email.setSubject(subject);
if(email instanceof HtmlEmail) {
HtmlEmail he = (HtmlEmail) email;
he.setHtmlMsg(htmlMessageText);
he.setTextMsg(messageText);
}
email.addTo(to);
email.send();
} catch (EmailException ee) {
throw new ApplicationException(ee);
}
}
public static UserInfo getPrincipal() {
UserInfo ui = null;
SecurityContext context = SecurityContextHolder.getContext();
if(context != null) {
Authentication authentication = context.getAuthentication();
if(authentication != null) {
Object principal = authentication.getPrincipal();
if(principal instanceof UserInfo) {
ui = (UserInfo) principal;
}
}
}
return ui;
}
public static boolean isAuthenticated() {
boolean authenticated = false;
SecurityContext context = SecurityContextHolder.getContext();
if(context != null) {
Authentication authentication = context.getAuthentication();
if(authentication != null) {
authenticated = authentication.isAuthenticated();
}
}
return authenticated;
}
public static String getRandomKey() {
String uuid = UUID.randomUUID().toString();
return uuid.replaceAll("-", "");
}
public static int getIntAttribute(String name) {
return getIntAttribute(name, -1);
}
public static int getIntAttribute(String name, int defaultValue) {
String value = getStringAttribute(name, Integer.toString(defaultValue));
if(value != null) {
return Integer.parseInt(value);
} else {
return defaultValue;
}
}
public static String getStringAttribute(String name) {
return getStringAttribute(name, null);
}
public static String getStringAttribute(String name, String defaultValue) {
String value = defaultValue;
Object obj = ServletContextHolder.getInstance().getServletContext().getAttribute(name);
if(obj != null && obj instanceof String) {
value = ((String) obj).trim();
}
return value;
}
public static boolean getBooleanAttribute(String name, boolean defaultValue) {
boolean result = defaultValue;
String value = getStringAttribute(name);
if(value != null) {
result = Boolean.parseBoolean(value);
}
return result;
}
}