com.vip.vjtools.vjkit.base.ExceptionUtil Java Examples

The following examples show how to use com.vip.vjtools.vjkit.base.ExceptionUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlMapper.java    From vjtools with Apache License 2.0 6 votes vote down vote up
/**
 * Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.
 */
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
	try {
		CollectionWrapper wrapper = new CollectionWrapper();
		wrapper.collection = root;

		JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
				CollectionWrapper.class, wrapper);

		StringWriter writer = new StringWriter();
		createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

		return writer.toString();
	} catch (JAXBException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #2
Source File: XmlMapper.java    From vjtools with Apache License 2.0 6 votes vote down vote up
/**
 * 创建Marshaller并设定encoding(可为null).
 * 线程不安全,需要每次创建或pooling。
 */
public static Marshaller createMarshaller(Class clazz, String encoding) {
	try {
		JAXBContext jaxbContext = getJaxbContext(clazz);

		Marshaller marshaller = jaxbContext.createMarshaller();

		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

		if (StringUtils.isNotBlank(encoding)) {
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
		}

		return marshaller;
	} catch (JAXBException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #3
Source File: XmlMapper.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 创建UnMarshaller.
 * 线程不安全,需要每次创建或pooling。
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
	try {
		JAXBContext jaxbContext = getJaxbContext(clazz);
		return jaxbContext.createUnmarshaller();
	} catch (JAXBException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #4
Source File: J360WXPayConfig.java    From j360-boot-app-all with Apache License 2.0 5 votes vote down vote up
@Override
InputStream getCertStream() {
    File cert = new File(certPath);
    try {
        return new FileInputStream(cert);
    } catch (FileNotFoundException e) {
        log.error("找不到本地的cert地址: {}", certPath);
        throw ExceptionUtil.unchecked(e);
    }
}
 
Example #5
Source File: CleanUpScheduler.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * return current date time by specified hour:minute
 * 
 * @param plan format: hh:mm
 */
public static Date getCurrentDateByPlan(String plan, String pattern) {
	try {
		FastDateFormat format = FastDateFormat.getInstance(pattern);
		Date end = format.parse(plan);
		Calendar today = Calendar.getInstance();
		end = DateUtils.setYears(end, (today.get(Calendar.YEAR)));
		end = DateUtils.setMonths(end, today.get(Calendar.MONTH));
		end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH));
		return end;
	} catch (Exception e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #6
Source File: BasicFutureTest.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws InterruptedException, ExecutionException {
	MyFuture<String> future = new MyFuture<String>();
	Tasks.success(future);
	String result = future.get();
	assertThat(result).isEqualTo("haha");

	// 无人设置返回值
	try {
		MyFuture<String> future2 = new MyFuture<String>();
		future2.get(10, TimeUnit.MILLISECONDS);
		fail("should fail before");
	} catch (TimeoutException e) {
		assertThat(e).isInstanceOf(TimeoutException.class);
	}

	// 失败
	try {
		MyFuture<String> future3 = new MyFuture<String>();
		Tasks.fail(future3);
		future3.get();
		fail("should fail before");
	} catch (Throwable t) {
		assertThat(ExceptionUtil.unwrap(t)).hasMessage("wuwu");
	}

	// 取消
	MyFuture<String> future4 = new MyFuture<String>();
	Tasks.cancel(future4);
	assertThat(future4.isCancelled()).isTrue();
	try{
		String result4 = future4.get();
		fail("should fail here");
	}catch(CancellationException cae){
		
	}
	
	
}
 
Example #7
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 调用构造函数.
 */
public static <T> T invokeConstructor(final Class<T> cls, Object... args) {
	try {
		return ConstructorUtils.invokeConstructor(cls, args);
	} catch (Exception e) {
		throw ExceptionUtil.unwrapAndUnchecked(e);
	}
}
 
Example #8
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 调用预先获取的Method,用于反复调用的场景
 */
public static <T> T invokeMethod(final Object obj, Method method, Object... args) {
	try {
		return (T) method.invoke(obj, args);
	} catch (Exception e) {
		throw ExceptionUtil.unwrapAndUnchecked(e);
	}
}
 
Example #9
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 生成AES密钥,可选长度为128,192,256位.
 */
public static byte[] generateAesKey(int keysize) {
	try {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALG);
		keyGenerator.init(keysize);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #10
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
 * 
 * @param input 原始字节数组
 * @param key 符合AES要求的密钥
 * @param iv 初始向量
 * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
 */
private static byte[] aes(byte[] input, byte[] key, byte[] iv, int mode) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
		IvParameterSpec ivSpec = new IvParameterSpec(iv);
		Cipher cipher = Cipher.getInstance(AES_CBC_ALG);
		cipher.init(mode, secretKey, ivSpec);
		return cipher.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #11
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
 * 
 * @param input 原始字节数组
 * @param key 符合AES要求的密钥
 * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
 */
private static byte[] aes(byte[] input, byte[] key, int mode) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
		Cipher cipher = Cipher.getInstance(AES_ALG);
		cipher.init(mode, secretKey);
		return cipher.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #12
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 生成HMAC-SHA1密钥,返回字节数组,长度为160位(20字节). HMAC-SHA1算法对密钥无特殊要求, RFC2401建议最少长度为160位(20字节).
 */
public static byte[] generateHmacSha1Key() {
	try {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1_ALG);
		keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #13
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
 * 
 * @param input 原始输入字符数组
 * @param key HMAC-SHA1密钥
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, HMACSHA1_ALG);
		Mac mac = Mac.getInstance(HMACSHA1_ALG);
		mac.init(secretKey);
		return mac.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #14
Source File: CleanUpScheduler.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * return current date time by specified hour:minute
 * 
 * @param plan format: hh:mm
 */
public static Date getCurrentDateByPlan(String plan, String pattern) {
	try {
		FastDateFormat format = FastDateFormat.getInstance(pattern);
		Date end = format.parse(plan);
		Calendar today = Calendar.getInstance();
		end = DateUtils.setYears(end, (today.get(Calendar.YEAR)));
		end = DateUtils.setMonths(end, today.get(Calendar.MONTH));
		end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH));
		return end;
	} catch (Exception e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #15
Source File: BasicFutureTest.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws InterruptedException, ExecutionException {
	MyFuture<String> future = new MyFuture<String>();
	Tasks.success(future);
	String result = future.get();
	assertThat(result).isEqualTo("haha");

	// 无人设置返回值
	try {
		MyFuture<String> future2 = new MyFuture<String>();
		future2.get(10, TimeUnit.MILLISECONDS);
		fail("should fail before");
	} catch (TimeoutException e) {
		assertThat(e).isInstanceOf(TimeoutException.class);
	}

	// 失败
	try {
		MyFuture<String> future3 = new MyFuture<String>();
		Tasks.fail(future3);
		future3.get();
		fail("should fail before");
	} catch (Throwable t) {
		assertThat(ExceptionUtil.unwrap(t)).hasMessage("wuwu");
	}

	// 取消
	MyFuture<String> future4 = new MyFuture<String>();
	Tasks.cancel(future4);
	assertThat(future4.isCancelled()).isTrue();
	try {
		String result4 = future4.get();
		fail("should fail here");
	} catch (CancellationException cae) {

	}


}
 
Example #16
Source File: PaddingOracleCBCForShiro.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] aes(byte[] input, byte[] key, byte[] iv, int mode) {
  try {
    SecretKey secretKey = new SecretKeySpec(key, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(mode, secretKey, ivSpec);
    return cipher.doFinal(input);
  } catch (GeneralSecurityException var7) {
    throw ExceptionUtil.unchecked(var7);
  }
}
 
Example #17
Source File: XmlMapper.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * Xml->Java Object.
 */
public static <T> T fromXml(String xml, Class<T> clazz) {
	try {
		StringReader reader = new StringReader(xml);
		return (T) createUnmarshaller(clazz).unmarshal(reader);
	} catch (JAXBException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #18
Source File: XmlMapper.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * Java Object->Xml with encoding.
 */
public static String toXml(Object root, Class clazz, String encoding) {
	try {
		StringWriter writer = new StringWriter();
		createMarshaller(clazz, encoding).marshal(root, writer);
		return writer.toString();
	} catch (JAXBException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #19
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 调用构造函数.
 */
public static <T> T invokeConstructor(final Class<T> cls, Object... args) {
	try {
		return ConstructorUtils.invokeConstructor(cls, args);
	} catch (Exception e) {
		throw ExceptionUtil.unwrapAndUnchecked(e);
	}
}
 
Example #20
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 调用预先获取的Method,用于反复调用的场景
 */
public static <T> T invokeMethod(final Object obj, Method method, Object... args) {
	try {
		return (T) method.invoke(obj, args);
	} catch (Exception e) {
		throw ExceptionUtil.unwrapAndUnchecked(e);
	}
}
 
Example #21
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 生成AES密钥,可选长度为128,192,256位.
 */
public static byte[] generateAesKey(int keysize) {
	try {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALG);
		keyGenerator.init(keysize);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #22
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
 * 
 * @param input 原始字节数组
 * @param key 符合AES要求的密钥
 * @param iv 初始向量
 * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
 */
private static byte[] aes(byte[] input, byte[] key, byte[] iv, int mode) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
		IvParameterSpec ivSpec = new IvParameterSpec(iv);
		Cipher cipher = Cipher.getInstance(AES_CBC_ALG);
		cipher.init(mode, secretKey, ivSpec);
		return cipher.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #23
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
 * 
 * @param input 原始字节数组
 * @param key 符合AES要求的密钥
 * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
 */
private static byte[] aes(byte[] input, byte[] key, int mode) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
		Cipher cipher = Cipher.getInstance(AES_ALG);
		cipher.init(mode, secretKey);
		return cipher.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #24
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 生成HMAC-SHA1密钥,返回字节数组,长度为160位(20字节). HMAC-SHA1算法对密钥无特殊要求, RFC2401建议最少长度为160位(20字节).
 */
public static byte[] generateHmacSha1Key() {
	try {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1_ALG);
		keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #25
Source File: CryptoUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
 * 
 * @param input 原始输入字符数组
 * @param key HMAC-SHA1密钥
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, HMACSHA1_ALG);
		Mac mac = Mac.getInstance(HMACSHA1_ALG);
		mac.init(secretKey);
		return mac.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example #26
Source File: CloneableException.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简便函数,定义静态异常时使用
 */
public CloneableException setStackTrace(Class<?> throwClazz, String throwMethod) {
	ExceptionUtil.setStackTrace(this, throwClazz, throwMethod);
	return this;
}
 
Example #27
Source File: CloneableRuntimeException.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简便函数,定义静态异常时使用
 */
public CloneableRuntimeException setStackTrace(Class<?> throwClazz, String throwMethod) {
	ExceptionUtil.setStackTrace(this, throwClazz, throwMethod);
	return this;
}
 
Example #28
Source File: CloneableRuntimeException.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简便函数,定义静态异常时使用
 */
public CloneableRuntimeException setStackTrace(Class<?> throwClazz, String throwMethod) {
	ExceptionUtil.setStackTrace(this, throwClazz, throwMethod);
	return this;
}
 
Example #29
Source File: CloneableException.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简便函数,定义静态异常时使用
 */
public CloneableException setStackTrace(Class<?> throwClazz, String throwMethod) {
	ExceptionUtil.setStackTrace(this, throwClazz, throwMethod);
	return this;
}