Skip to content

Commit

Permalink
Revert ServiceInterceptor changes from 7.2.12 #3167
Browse files Browse the repository at this point in the history
This reverts commit d253b15
  • Loading branch information
dtaimanov committed Mar 18, 2021
1 parent 644bba4 commit 81ab570
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 106 deletions.
78 changes: 14 additions & 64 deletions modules/core/src/com/haulmont/cuba/core/sys/ServiceInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@
import com.haulmont.cuba.security.app.UserSessionsAPI;
import com.haulmont.cuba.security.global.NoUserSessionException;
import com.haulmont.cuba.security.global.UserSession;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
* Intercepts invocations of the middleware services.
* <br> Checks {@link UserSession} validity and wraps exceptions into {@link RemoteException}.
*/
public class ServiceInterceptor implements MethodInterceptor {
public class ServiceInterceptor {
private static final Logger log = LoggerFactory.getLogger(ServiceInterceptor.class);

private UserSessionsAPI userSessions;
Expand All @@ -66,17 +63,16 @@ public void setConfiguration(Configuration configuration) {
logInternalServiceInvocation = configuration.getConfig(ServerConfig.class).getLogInternalServiceInvocation();
}

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
private Object aroundInvoke(ProceedingJoinPoint ctx) throws Throwable {
SecurityContext securityContext = AppContext.getSecurityContextNN();
boolean internalInvocation = securityContext.incServiceInvocation() > 0;
try {
if (internalInvocation) {
if (logInternalServiceInvocation) {
log.warn("Invoking '{}' from another service", longString(invocation.getMethod()));
log.warn("Invoking '{}' from another service", ctx.getSignature());
}

Object res = invocation.proceed();
Object res = ctx.proceed();

return res;
} else {
Expand All @@ -89,18 +85,18 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
throw new NoUserSessionException(securityContext.getSessionId());
}

log.trace("Invoking: {}, session={}", longString(invocation.getMethod()), userSession);
log.trace("Invoking: {}, session={}", ctx.getSignature(), userSession);

Object res = invocation.proceed();
Object res = ctx.proceed();

return res;
} catch (Throwable e) {
logException(e, invocation);
logException(e, ctx);
// Propagate the special exception to avoid serialization errors on remote clients
throw new RemoteException(e);
} finally {
if (checkTransactionOnExit && persistence.isInTransaction()) {
log.warn("Open transaction left in {}", shortString(invocation.getMethod()));
log.warn("Open transaction left in {}", ctx.getSignature().toShortString());
}
}
}
Expand All @@ -110,69 +106,23 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
}


protected void logException(Throwable e, MethodInvocation invocation) {
protected void logException(Throwable e, ProceedingJoinPoint ctx) {
if (e instanceof NoUserSessionException) {
// If you don't want NoUserSessionException in log, set level higher than INFO for ServiceInterceptor logger
log.info("Exception in {}: {}", shortString(invocation.getMethod()), e.toString());
log.info("Exception in {}: {}", ctx.getSignature().toShortString(), e.toString());
} else if (e instanceof MethodParametersValidationException) {
log.info("MethodParametersValidationException in {}: {}, violations:\n{}", shortString(invocation.getMethod()), e.toString(),
log.info("MethodParametersValidationException in {}: {}, violations:\n{}", ctx.getSignature().toShortString(), e.toString(),
((MethodParametersValidationException) e).getConstraintViolations());
} else if (e instanceof MethodResultValidationException) {
log.error("MethodResultValidationException in {}: {}, violations:\n{}", shortString(invocation.getMethod()), e.toString(),
log.error("MethodResultValidationException in {}: {}, violations:\n{}", ctx.getSignature().toShortString(), e.toString(),
((MethodResultValidationException) e).getConstraintViolations());
} else {
Logging annotation = e.getClass().getAnnotation(Logging.class);
if (annotation == null || annotation.value() == Logging.Type.FULL) {
log.error("Exception: ", e);
} else if (annotation.value() == Logging.Type.BRIEF) {
log.error("Exception in {}: {}", shortString(invocation.getMethod()), e.toString());
log.error("Exception in {}: {}", ctx.getSignature().toShortString(), e.toString());
}
}
}

protected static String longString(Method method) {
return methodString(method, true);
}

protected static String shortString(Method method) {
return methodString(method, false);
}

protected static String methodString(Method method, boolean longForm) {
StringBuilder sb = new StringBuilder();
if (longForm) {
appendType(sb, method.getReturnType(), false);
sb.append(" ");
}
appendType(sb, method.getDeclaringClass(), longForm);
sb.append(".");
sb.append(method.getName());
sb.append("(");
Class<?>[] parametersTypes = method.getParameterTypes();
appendTypes(sb, parametersTypes, longForm);
sb.append(")");
return sb.toString();
}

private static void appendTypes(StringBuilder sb, Class<?>[] types, boolean includeArgs) {
if (includeArgs) {
for (int size = types.length, i = 0; i < size; i++) {
appendType(sb, types[i], false);
if (i < size - 1) {
sb.append(",");
}
}
} else if (types.length != 0) {
sb.append("..");
}
}

private static void appendType(StringBuilder sb, Class<?> type, boolean useLongTypeName) {
if (type.isArray()) {
appendType(sb, type.getComponentType(), useLongTypeName);
sb.append("[]");
} else {
sb.append(useLongTypeName ? type.getName() : type.getSimpleName());
}
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions modules/core/src/com/haulmont/cuba/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
pointcut="execution(@com.haulmont.cuba.security.global.TrustedClientOnly * *(..))"/>
</aop:aspect>

<aop:aspect id="serviceAspect" ref="serviceInterceptor" order="1">
<aop:around method="aroundInvoke" pointcut="@within(org.springframework.stereotype.Service)"/>
</aop:aspect>

<aop:aspect id="mbeanAspect" ref="mbeanInterceptor" order="1">
<aop:around method="aroundInvoke"
pointcut="execution(* *..*MBean.*(..))"/>
Expand Down

0 comments on commit 81ab570

Please sign in to comment.