Last active
December 27, 2015 01:19
-
-
Save xcooper/7243769 to your computer and use it in GitHub Desktop.
Aspect that check outside web service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
public class WebServiceAspect { | |
@Pointcut("execution(public * com.service..*Service.*(..))") | |
void allService() {} | |
@Around("allService()") | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public Object depressException(ProceedingJoinPoint joinPoint) throws Throwable { | |
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | |
Class returnType = signature.getReturnType(); | |
try { | |
return joinPoint.proceed(); | |
} catch (InternalServerErrorException e) { | |
if (returnType.isAssignableFrom(Set.class)) { | |
return new HashSet(0); | |
} | |
if (returnType.isAssignableFrom(List.class)) { | |
return new ArrayList(0); | |
} | |
if (returnType.isAssignableFrom(Map.class)) { | |
return new HashMap(0); | |
} | |
return null; | |
} | |
} | |
@Before("allService()") | |
public void printOutArguments(JoinPoint joinPoint) throws Throwable { | |
if (log.isTraceEnabled()) { | |
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | |
Object[] args = joinPoint.getArgs(); | |
ObjectWriter writer = new ObjectMapper().writerWithDefaultPrettyPrinter(); | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < args.length; i++) { | |
Object arg = args[i]; | |
try { | |
String json = writer.writeValueAsString(arg); | |
sb.append(String.format("arg[%d] = %s\n", i, json)); | |
} catch (Exception e) { | |
log.error("%s", e.getMessage()); | |
} | |
} | |
log.trace("call {} with:[\n{}]", signature.getName(), sb); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment