use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class MethodLifeCycleHandler method handleError.
@Override
public Object handleError(ForestRequest request, ForestResponse response, Throwable ex) {
this.response = response;
ForestRuntimeException e = null;
if (ex instanceof ForestRuntimeException) {
e = (ForestRuntimeException) ex;
} else {
e = new ForestRuntimeException(ex);
}
request.getInterceptorChain().onError(e, request, response);
Object resultData = null;
if (request.getOnError() != null) {
request.getOnError().onError(e, request, response);
resultData = response.getResult();
return resultData;
} else {
throw e;
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class HTTPProxyLifeCycle method beforeExecute.
@Override
public boolean beforeExecute(ForestRequest request) {
MappingTemplate hostTemplate = (MappingTemplate) getAttribute(request, "host_temp");
MappingTemplate portTemplate = (MappingTemplate) getAttribute(request, "port_temp");
MappingTemplate usernameTemplate = (MappingTemplate) getAttribute(request, "username_temp");
MappingTemplate passwordTemplate = (MappingTemplate) getAttribute(request, "password_temp");
Object httpProxySource = request.getMethod().getExtensionParameterValue(PARAM_KEY_HTTP_PROXY_SOURCE);
Object[] args = request.getArguments();
String host = hostTemplate.render(args);
String portStr = portTemplate.render(args);
String username = null, password = null;
if (usernameTemplate != null) {
username = usernameTemplate.render(args);
}
if (passwordTemplate != null) {
password = passwordTemplate.render(args);
}
int port = 80;
if (StringUtils.isBlank(host)) {
if (httpProxySource != null && httpProxySource instanceof HTTPProxySource) {
request.setProxy(((HTTPProxySource) httpProxySource).getProxy(request));
return true;
}
throw new ForestRuntimeException("[Forest] Proxy host cannot be empty!");
}
if (StringUtils.isNotBlank(portStr)) {
try {
port = Integer.parseInt(portStr);
} catch (Throwable th) {
}
}
ForestProxy proxy = new ForestProxy(host, port);
if (StringUtils.isNotEmpty(username)) {
proxy.setUsername(username);
}
if (StringUtils.isNotEmpty(password)) {
proxy.setPassword(password);
}
request.setProxy(proxy);
return true;
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class SSLUtils method customSSL.
/**
* 自定义SSL证书
* @param request Forest请求对象,{@link ForestRequest}类实例
* @return SSL上下文,{@link SSLContext}类实例
*/
public static SSLContext customSSL(ForestRequest request) {
SSLContext sslContext = null;
final SSLKeyStore fKeyStore = request.getKeyStore();
final KeyStore keyStore = fKeyStore.getTrustStore();
final String certPass = fKeyStore.getCertPass();
if (keyStore != null) {
try {
// 密钥库
char[] certPassCharArray = certPass.toCharArray();
KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
kmf.init(keyStore, certPassCharArray);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContextBuilder scBuilder = SSLContexts.custom();
String protocol = request.getSslProtocol();
if (StringUtils.isNotEmpty(protocol)) {
scBuilder.useProtocol(protocol);
}
scBuilder.loadTrustMaterial(keyStore, new TrustSelfSignedStrategy());
if (certPass != null) {
scBuilder.loadKeyMaterial(keyStore, certPassCharArray);
}
sslContext = scBuilder.build();
} catch (NoSuchAlgorithmException e) {
throw new ForestRuntimeException(e);
} catch (KeyManagementException e) {
throw new ForestRuntimeException(e);
} catch (KeyStoreException e) {
throw new ForestRuntimeException(e);
} catch (UnrecoverableKeyException e) {
throw new ForestRuntimeException(e);
}
}
return sslContext;
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ReflectUtils method copyAnnotationAttributes.
public static void copyAnnotationAttributes(Annotation source, Object target) {
if (target == null) {
return;
}
Map<String, Object> attrs = getAttributesFromAnnotation(source);
Class targetClass = target.getClass();
for (String name : attrs.keySet()) {
String methodName = NameUtils.setterName(name);
try {
Method setterMethod = null;
for (Method method : targetClass.getMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
setterMethod = method;
break;
}
}
if (setterMethod != null) {
setterMethod.invoke(target, attrs.get(name));
}
} catch (Throwable e) {
throw new ForestRuntimeException(e);
}
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class SSLKeyStore method loadTrustStore.
public void loadTrustStore() {
if (inputStream != null) {
try {
trustStore = KeyStore.getInstance(keystoreType);
String pass = this.keystorePass;
if (pass == null) {
trustStore.load(inputStream, null);
} else {
trustStore.load(inputStream, pass.trim().toCharArray());
}
} catch (KeyStoreException e) {
throw new ForestRuntimeException(e);
} catch (CertificateException e) {
throw new ForestRuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new ForestRuntimeException(e);
} catch (IOException e) {
throw new ForestRuntimeException(e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations