com.ulisesbocchio.jasyptspringboot.aop.EncryptablePropertySourceMethodInterceptor Maven / Gradle / Ivy
package com.ulisesbocchio.jasyptspringboot.aop;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.core.env.PropertySource;
/**
* @author Ulises Bocchio
*/
public class EncryptablePropertySourceMethodInterceptor implements MethodInterceptor, EncryptablePropertySource {
private final EncryptablePropertyResolver resolver;
private final PropertySource delegate;
public EncryptablePropertySourceMethodInterceptor(PropertySource delegate, EncryptablePropertyResolver resolver) {
this.resolver = resolver;
this.delegate = delegate;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if(isGetDelegateCall(invocation)) {
return getDelegate();
}
Object returnValue = invocation.proceed();
if(isGetPropertyCall(invocation)) {
return getProperty(resolver, getPropertySource(invocation), getNameArgument(invocation));
}
return returnValue;
}
@SuppressWarnings("unchecked")
private PropertySource getPropertySource(MethodInvocation invocation) {
return (PropertySource) invocation.getThis();
}
private String getNameArgument(MethodInvocation invocation) {
return (String) invocation.getArguments()[0];
}
private boolean isGetDelegateCall(MethodInvocation invocation) {
return invocation.getMethod().getName().equals("getDelegate");
}
private boolean isGetPropertyCall(MethodInvocation invocation) {
return invocation.getMethod().getName().equals("getProperty")
&& invocation.getMethod().getParameters().length == 1
&& invocation.getMethod().getParameters()[0].getType() == String.class;
}
@Override
public PropertySource getDelegate() {
return delegate;
}
}