RestrictedLiferayObjectWrapper extends LiferayObjectWrapper { private boolean _isRestricted(Class<?> clazz) { return _restrictedClassMap.computeIfAbsent( clazz.getName(), className -> { if (_allowedClassNames.contains(className)) { return false; } for (Class<?> restrictedClass : _restrictedClasses) { if (!restrictedClass.isAssignableFrom(clazz)) { continue; } return true; } int index = className.lastIndexOf(StringPool.PERIOD); if (index == -1) { return false; } String packageName = className.substring(0, index); packageName = packageName.concat(StringPool.PERIOD); for (String restrictedPackageName : _restrictedPackageNames) { if (!packageName.startsWith(restrictedPackageName)) { continue; } return true; } return false; }); } RestrictedLiferayObjectWrapper(
		String[] allowedClassNames, String[] restrictedClassNames,
		String[] restrictedMethodNames); @Override TemplateModel wrap(Object object);  }
@Test public void testIsRestricted() { Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper(null, null, null), TestLiferayObject.class)); Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( new String[] {TestLiferayObject.class.getName()}, new String[] {TestLiferayObject.class.getName()}, null), TestLiferayObject.class)); Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"java.lang.String"}, null), TestLiferayObject.class)); Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"com.liferay.portal.cache"}, null), TestLiferayObject.class)); Assert.assertTrue( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {TestLiferayObject.class.getName()}, null), TestLiferayObject.class)); Assert.assertTrue( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"com.liferay.portal.template.freemarker"}, null), TestLiferayObject.class)); Assert.assertTrue( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"com.liferay.portal.*"}, null), TestLiferayObject.class)); Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"com.liferay.portal.template.freemarker"}, null), byte.class)); Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( null, new String[] {"com.liferay.portal.template.freemarker"}, null), byte.class)); }
@Test public void testIsRestrictedWithNoContextClassloader() { Thread thread = Thread.currentThread(); ClassLoader contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader(null); try { Assert.assertFalse( _isRestricted( new RestrictedLiferayObjectWrapper( new String[] {TestLiferayObject.class.getName()}, new String[] {TestLiferayObject.class.getName()}, null), TestLiferayObject.class)); } finally { thread.setContextClassLoader(contextClassLoader); } }