Search in sources :

Example 1 with GreenStepHessianProxyFactory

use of com.netsteadfast.greenstep.sys.GreenStepHessianProxyFactory in project bamboobsc by billchen198318.

the class HessianServiceProxyAspect method proxyProcess.

private Object proxyProcess(ProceedingJoinPoint pjp) throws AuthorityException, ServiceException, Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Annotation[] annotations = pjp.getTarget().getClass().getAnnotations();
    String serviceId = AspectConstants.getServiceId(annotations);
    /**
		 * 不需要被遠端代理的 service-bean
		 */
    if (!GreenStepHessianUtils.isProxyServiceId(serviceId)) {
        //logger.info( "reject proxy service: " + serviceId );
        return pjp.proceed();
    }
    String userId = StringUtils.defaultString((String) SecurityUtils.getSubject().getPrincipal());
    if (GreenStepHessianUtils.getConfigHessianHeaderCheckValueModeEnable()) {
        /**
			 * 沒使用者資訊不能處理遠端代理的 service-bean
			 */
        if (StringUtils.isBlank(userId)) {
            logger.warn("no userId");
            pjp.proceed();
        }
        /**
			 * 此使用者不能存取遠端代理的 service-bean
			 */
        if (GreenStepHessianUtils.isProxyBlockedAccountId(userId)) {
            logger.warn("reject proxy service: " + serviceId + " , blocked userId: " + userId);
            return pjp.proceed();
        }
    }
    String serviceInterfacesName = "";
    Class<?>[] serviceInterfaces = pjp.getTarget().getClass().getInterfaces();
    for (Class<?> clazz : serviceInterfaces) {
        if (clazz.getName().indexOf(".service.") > -1) {
            serviceInterfacesName = clazz.getName();
        }
    }
    if (StringUtils.isBlank(serviceInterfacesName)) {
        logger.error("error no service interface: " + serviceId);
        throw new Exception("error no service interface: " + serviceId);
    }
    String url = GreenStepHessianUtils.getServiceUrl(serviceId);
    String theSystemPath = ApplicationSiteUtils.getHost(Constants.getSystem()) + "/" + ApplicationSiteUtils.getContextPath(Constants.getSystem());
    /**
		 * 不要自己呼叫遠端代理的自己, 會造成 HessianServiceProxyAspect 一直重複觸發 (客戶端與server-remote同一台的情況下)
		 * 如客戶端是 http://127.0.0.1:8080/
		 * 但是客戶端有開啟 hessian.enable=Y 呼叫遠端代理, 然後遠端url設定為 hessian.serverUrl=http://127.0.0.1:8080/
		 * 
		 */
    if (url.indexOf(theSystemPath) > -1) {
        logger.error("cannot open same-server. now system contextPath = " + theSystemPath + " , but proxy url = " + url);
        throw new Exception("cannot open same-server. now system contextPath = " + theSystemPath + " , but proxy url = " + url);
    }
    logger.info("proxy url = " + url);
    HessianProxyFactory factory = null;
    Object proxyServiceObject = null;
    if (GreenStepHessianUtils.getConfigHessianHeaderCheckValueModeEnable()) {
        // 一般要checkValue模式
        factory = new GreenStepHessianProxyFactory();
        ((GreenStepHessianProxyFactory) factory).setHeaderCheckValue(GreenStepHessianUtils.getEncAuthValue(userId));
        proxyServiceObject = ((GreenStepHessianProxyFactory) factory).createForHeaderMode(Class.forName(serviceInterfacesName), url);
    } else {
        // 不使用checkValue模式
        factory = new HessianProxyFactory();
        proxyServiceObject = factory.create(Class.forName(serviceInterfacesName), url);
    }
    Method[] proxyObjectMethods = proxyServiceObject.getClass().getMethods();
    Method proxyObjectMethod = null;
    if (null == proxyObjectMethods) {
        logger.error("error no find proxy method: " + serviceId);
        throw new Exception("error no find proxy method: " + serviceId);
    }
    for (Method m : proxyObjectMethods) {
        if (m.getName().equals(signature.getMethod().getName()) && Arrays.equals(m.getParameterTypes(), signature.getMethod().getParameterTypes())) {
            proxyObjectMethod = m;
        }
    }
    if (null == proxyObjectMethod) {
        logger.error("error no execute proxy method: " + serviceId);
        throw new Exception("error no execute proxy method: " + serviceId);
    }
    Object resultObj = null;
    try {
        resultObj = proxyObjectMethod.invoke(proxyServiceObject, pjp.getArgs());
        this.setReCalculateSizePageOfForPageFindGridResult(resultObj, pjp.getArgs());
    } catch (InvocationTargetException e) {
        if (e.getMessage() != null) {
            throw new ServiceException(e.getMessage().toString());
        }
        if (e.getTargetException().getMessage() != null) {
            throw new ServiceException(e.getTargetException().getMessage().toString());
        }
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage().toString());
        throw e;
    }
    logger.info("proxy success: " + serviceId + " method: " + proxyObjectMethod.getName());
    return resultObj;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) GreenStepHessianProxyFactory(com.netsteadfast.greenstep.sys.GreenStepHessianProxyFactory) HessianProxyFactory(com.caucho.hessian.client.HessianProxyFactory) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) AuthorityException(com.netsteadfast.greenstep.base.exception.AuthorityException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) GreenStepHessianProxyFactory(com.netsteadfast.greenstep.sys.GreenStepHessianProxyFactory)

Aggregations

HessianProxyFactory (com.caucho.hessian.client.HessianProxyFactory)1 AuthorityException (com.netsteadfast.greenstep.base.exception.AuthorityException)1 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)1 GreenStepHessianProxyFactory (com.netsteadfast.greenstep.sys.GreenStepHessianProxyFactory)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1