Polish SecurityJacksonModules

Issue gh-3736
* ClassLoader argument - this is required because we do not want to assume
the ClassLoader that should be used
* Clean up logging - logging is now at debug level because we don't expect
all of the modules are loaded (they are quite possibly off the ClassPath)
* Remove ObjectUtils as it was being used on methods that expect a
Collection or Array with non collection based objects
* Polish Javadoc warnings
This commit is contained in:
Rob Winch 2016-09-01 14:59:35 -05:00
parent c2d8ea92d0
commit 3fb77f3b59
8 changed files with 30 additions and 24 deletions

View File

@ -71,8 +71,9 @@ public class CasAuthenticationTokenMixinTests {
} }
ObjectMapper buildObjectMapper() { ObjectMapper buildObjectMapper() {
ClassLoader loader = getClass().getClassLoader();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModules(SecurityJacksonModules.getModules()); mapper.registerModules(SecurityJacksonModules.getModules(loader));
return mapper; return mapper;
} }

View File

@ -23,7 +23,6 @@ import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -63,41 +62,42 @@ public final class SecurityJacksonModules {
} }
public static void enableDefaultTyping(ObjectMapper mapper) { public static void enableDefaultTyping(ObjectMapper mapper) {
if(!ObjectUtils.isEmpty(mapper)) { if(mapper != null) {
TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null); TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
if (ObjectUtils.isEmpty(typeBuilder)) { if (typeBuilder == null) {
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
} }
} }
} }
private static Module loadAndGetInstance(String className) { @SuppressWarnings("unchecked")
private static Module loadAndGetInstance(String className, ClassLoader loader) {
Module instance = null; Module instance = null;
try { try {
logger.debug("Loading module " + className); Class<? extends Module> securityModule = (Class<? extends Module>) ClassUtils.forName(className, loader);
Class<? extends Module> securityModule = (Class<? extends Module>) ClassUtils.forName(className, ClassUtils.getDefaultClassLoader()); if (securityModule != null) {
if (!ObjectUtils.isEmpty(securityModule)) { if(logger.isDebugEnabled()) {
logger.debug("Loaded module " + className + ", now registering"); logger.debug("Loaded module " + className + ", now registering");
}
instance = securityModule.newInstance(); instance = securityModule.newInstance();
} }
} catch (ClassNotFoundException e) { } catch (Exception e) {
logger.warn("Module class not found : " + e.getMessage()); if(logger.isDebugEnabled()) {
} catch (InstantiationException e) { logger.debug("Cannot load module " + className, e);
logger.error(e.getMessage()); }
} catch (IllegalAccessException e) {
logger.error(e.getMessage());
} }
return instance; return instance;
} }
/** /**
* @param loader the ClassLoader to use
* @return List of available security modules in classpath. * @return List of available security modules in classpath.
*/ */
public static List<Module> getModules() { public static List<Module> getModules(ClassLoader loader) {
List<Module> modules = new ArrayList<Module>(); List<Module> modules = new ArrayList<Module>();
for (String className : securityJackson2ModuleClasses) { for (String className : securityJackson2ModuleClasses) {
Module module = loadAndGetInstance(className); Module module = loadAndGetInstance(className, loader);
if (!ObjectUtils.isEmpty(module)) { if (module != null) {
modules.add(module); modules.add(module);
} }
} }

View File

@ -37,7 +37,8 @@ public abstract class AbstractMixinTests {
protected ObjectMapper buildObjectMapper() { protected ObjectMapper buildObjectMapper() {
if (ObjectUtils.isEmpty(mapper)) { if (ObjectUtils.isEmpty(mapper)) {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.registerModules(SecurityJacksonModules.getModules()); ClassLoader loader = getClass().getClassLoader();
mapper.registerModules(SecurityJacksonModules.getModules(loader));
} }
return mapper; return mapper;
} }

View File

@ -37,7 +37,7 @@ import javax.servlet.http.Cookie;
* ObjectMapper mapper = new ObjectMapper(); * ObjectMapper mapper = new ObjectMapper();
* mapper.registerModule(new WebJackson2Module()); * mapper.registerModule(new WebJackson2Module());
* </pre> * </pre>
* <b>Note: use {@link SecurityJacksonModules#getModules()} to get list of all security modules.</b> * <b>Note: use {@link SecurityJacksonModules#getModules(ClassLoader)} to get list of all security modules.</b>
* *
* @author Jitendra Singh * @author Jitendra Singh
* @see SecurityJacksonModules * @see SecurityJacksonModules

View File

@ -34,7 +34,8 @@ public abstract class AbstractMixinTests {
protected ObjectMapper buildObjectMapper() { protected ObjectMapper buildObjectMapper() {
if (ObjectUtils.isEmpty(mapper)) { if (ObjectUtils.isEmpty(mapper)) {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.registerModules(SecurityJacksonModules.getModules()); ClassLoader loader = getClass().getClassLoader();
mapper.registerModules(SecurityJacksonModules.getModules(loader));
} }
return mapper; return mapper;
} }

View File

@ -39,7 +39,8 @@ public class CookieMixinTests {
ObjectMapper buildObjectMapper() { ObjectMapper buildObjectMapper() {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModules(SecurityJacksonModules.getModules()); ClassLoader loader = getClass().getClassLoader();
mapper.registerModules(SecurityJacksonModules.getModules(loader));
return mapper; return mapper;
} }

View File

@ -45,7 +45,8 @@ public class DefaultCsrfTokenMixinTests {
@Before @Before
public void setup() { public void setup() {
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
objectMapper.registerModules(SecurityJacksonModules.getModules()); ClassLoader loader = getClass().getClassLoader();
objectMapper.registerModules(SecurityJacksonModules.getModules(loader));
defaultCsrfTokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", " + defaultCsrfTokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", " +
"\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}"; "\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
} }

View File

@ -48,7 +48,8 @@ public class WebAuthenticationDetailsMixinTests {
@Before @Before
public void setup() { public void setup() {
this.mapper = new ObjectMapper(); this.mapper = new ObjectMapper();
this.mapper.registerModules(SecurityJacksonModules.getModules()); ClassLoader loader = getClass().getClassLoader();
this.mapper.registerModules(SecurityJacksonModules.getModules(loader));
} }
@Test @Test