
org.visallo.web.parameterProviders.RemoteAddrParameterProviderFactory Maven / Gradle / Ivy
package org.visallo.web.parameterProviders;
import com.google.inject.Inject;
import com.v5analytics.webster.HandlerChain;
import com.v5analytics.webster.parameterProviders.ParameterProvider;
import com.v5analytics.webster.parameterProviders.ParameterProviderFactory;
import org.visallo.core.config.Configuration;
import org.visallo.core.model.user.UserRepository;
import org.visallo.web.AuthenticationHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import static com.google.common.base.Preconditions.checkNotNull;
public class RemoteAddrParameterProviderFactory extends ParameterProviderFactory {
private final ParameterProvider parameterProvider;
@Inject
public RemoteAddrParameterProviderFactory(UserRepository userRepository, Configuration configuration) {
parameterProvider = new VisalloBaseParameterProvider(userRepository, configuration) {
@Override
public String getParameter(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) {
return AuthenticationHandler.getRemoteAddr(request);
}
};
}
@Override
public boolean isHandled(Method handleMethod, Class extends String> parameterType, Annotation[] parameterAnnotations) {
return getRemoteAddrAnnotation(parameterAnnotations) != null;
}
@Override
public ParameterProvider createParameterProvider(Method handleMethod, Class> parameterType, Annotation[] parameterAnnotations) {
RemoteAddr remoteAddrAnnotation = getRemoteAddrAnnotation(parameterAnnotations);
checkNotNull(remoteAddrAnnotation, "cannot find " + RemoteAddr.class.getName());
return parameterProvider;
}
private static RemoteAddr getRemoteAddrAnnotation(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof RemoteAddr) {
return (RemoteAddr) annotation;
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy