Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Jan Bartel 2016-10-21 16:37:13 +11:00
commit 34f0015aa9
8 changed files with 124 additions and 20 deletions

View File

@ -44,7 +44,7 @@ public class PostConstructAnnotationHandler extends AbstractIntrospectableAnnota
public void doHandle(Class clazz)
{
//Check that the PostConstruct is on a class that we're interested in
if (Util.supportsPostConstructPreDestroy(clazz))
if (supportsPostConstruct(clazz))
{
Method[] methods = clazz.getDeclaredMethods();
for (int i=0; i<methods.length; i++)
@ -84,4 +84,27 @@ public class PostConstructAnnotationHandler extends AbstractIntrospectableAnnota
}
}
}
/**
* Check if the given class is permitted to have PostConstruct annotation.
* @param c the class
* @return true if the spec permits the class to have PostConstruct, false otherwise
*/
public boolean supportsPostConstruct (Class c)
{
if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
javax.servlet.Filter.class.isAssignableFrom(c) ||
javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionIdListener.class.isAssignableFrom(c) ||
javax.servlet.AsyncListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpUpgradeHandler.class.isAssignableFrom(c))
return true;
return false;
}
}

View File

@ -43,7 +43,7 @@ public class PreDestroyAnnotationHandler extends AbstractIntrospectableAnnotatio
public void doHandle(Class clazz)
{
//Check that the PreDestroy is on a class that we're interested in
if (Util.supportsPostConstructPreDestroy(clazz))
if (supportsPreDestroy(clazz))
{
Method[] methods = clazz.getDeclaredMethods();
for (int i=0; i<methods.length; i++)
@ -85,4 +85,27 @@ public class PreDestroyAnnotationHandler extends AbstractIntrospectableAnnotatio
}
}
}
/**
* Check if the spec permits the given class to use the PreDestroy annotation.
* @param c the class
* @return true if permitted, false otherwise
*/
public boolean supportsPreDestroy (Class c)
{
if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
javax.servlet.Filter.class.isAssignableFrom(c) ||
javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionIdListener.class.isAssignableFrom(c) ||
javax.servlet.AsyncListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpUpgradeHandler.class.isAssignableFrom(c))
return true;
return false;
}
}

View File

@ -21,6 +21,8 @@ package org.eclipse.jetty.annotations;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
@ -39,6 +41,10 @@ import org.eclipse.jetty.webapp.WebAppContext;
public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationHandler
{
private static final Logger LOG = Log.getLogger(ResourceAnnotationHandler.class);
protected static final List<Class<?>> ENV_ENTRY_TYPES =
Arrays.asList(new Class[] {String.class, Character.class, Integer.class, Boolean.class, Double.class, Byte.class, Short.class, Long.class, Float.class});
protected WebAppContext _context;
@ -57,7 +63,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
*/
public void doHandle(Class<?> clazz)
{
if (Util.supportsResourceInjection(clazz))
if (supportsResourceInjection(clazz))
{
handleClass(clazz);
@ -182,7 +188,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
metaData.setOrigin("resource-ref."+name+".injection",resource,clazz);
}
else if (!Util.isEnvEntryType(type))
else if (!isEnvEntryType(type))
{
//if this is an env-entry type resource and there is no value bound for it, it isn't
//an error, it just means that perhaps the code will use a default value instead
@ -196,7 +202,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//if this is an env-entry type resource and there is no value bound for it, it isn't
//an error, it just means that perhaps the code will use a default value instead
// JavaEE Spec. sec 5.4.1.3
if (!Util.isEnvEntryType(type))
if (!isEnvEntryType(type))
throw new IllegalStateException(e);
}
}
@ -339,7 +345,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
metaData.setOrigin("resource-ref."+name+".injection",resource,clazz);
}
else if (!Util.isEnvEntryType(paramType))
else if (!isEnvEntryType(paramType))
{
//if this is an env-entry type resource and there is no value bound for it, it isn't
@ -353,11 +359,47 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//if this is an env-entry type resource and there is no value bound for it, it isn't
//an error, it just means that perhaps the code will use a default value instead
// JavaEE Spec. sec 5.4.1.3
if (!Util.isEnvEntryType(paramType))
if (!isEnvEntryType(paramType))
throw new IllegalStateException(e);
}
}
}
}
/**
* Check if the given Class is one that the specification allows to have a Resource annotation.
*
* @param c the class
* @return true if Resource annotation permitted, false otherwise
*/
public boolean supportsResourceInjection (Class<?> c)
{
if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
javax.servlet.Filter.class.isAssignableFrom(c) ||
javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionIdListener.class.isAssignableFrom(c) ||
javax.servlet.AsyncListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpUpgradeHandler.class.isAssignableFrom(c))
return true;
return false;
}
/**
* Check if the class is one of the basic java types permitted as
* env-entries.
* @param clazz the class to check
* @return true if class is permitted by the spec to be an env-entry value
*/
public boolean isEnvEntryType (Class<?> clazz)
{
return ENV_ENTRY_TYPES.contains(clazz);
}
}

View File

@ -20,12 +20,14 @@ package org.eclipse.jetty.annotations;
import java.lang.reflect.Array;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.TypeUtil;
import org.objectweb.asm.Type;
/**
* Annotation Processing Utilities
* @deprecated and replaced by extra methods in {@link ResourceAnnotationHandler}, {@link PreDestroyAnnotationHandler}, {@link PostConstructAnnotationHandler} and {@link ServletPathSpec}
*/
public class Util
{

View File

@ -26,6 +26,7 @@ import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.Source;
@ -117,7 +118,7 @@ public class WebFilterAnnotation extends DiscoveredAnnotation
ArrayList<String> paths = new ArrayList<String>();
for (String s:urlPatterns)
{
paths.add(Util.normalizePattern(s));
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}
@ -188,7 +189,7 @@ public class WebFilterAnnotation extends DiscoveredAnnotation
ArrayList<String> paths = new ArrayList<String>();
for (String s:urlPatterns)
{
paths.add(Util.normalizePattern(s));
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}

View File

@ -27,6 +27,8 @@ import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.servlet.Holder;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.servlet.Source;
@ -102,7 +104,7 @@ public class WebServletAnnotation extends DiscoveredAnnotation
//canonicalize the patterns
ArrayList<String> urlPatternList = new ArrayList<String>();
for (String p : urlPatterns)
urlPatternList.add(Util.normalizePattern(p));
urlPatternList.add(ServletPathSpec.normalize(p));
String servletName = (annotation.name().equals("")?clazz.getName():annotation.name());

View File

@ -18,10 +18,26 @@
package org.eclipse.jetty.http.pathmap;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
public class ServletPathSpec extends PathSpec
{
/**
* If a servlet or filter path mapping isn't a suffix mapping, ensure
* it starts with '/'
*
* @param pathSpec the servlet or filter mapping pattern
* @return the pathSpec prefixed by '/' if appropriate
*/
public static String normalize(String pathSpec)
{
if (StringUtil.isNotBlank(pathSpec) && !pathSpec.startsWith("/") && !pathSpec.startsWith("*"))
return "/" + pathSpec;
return pathSpec;
}
public ServletPathSpec(String servletPathSpec)
{
super();

View File

@ -36,6 +36,7 @@ import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.SessionTrackingMode;
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
import org.eclipse.jetty.security.ConstraintAware;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.authentication.FormAuthenticator;
@ -1190,7 +1191,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
while (iter.hasNext())
{
String p = iter.next().toString(false, true);
p = normalizePattern(p);
p = ServletPathSpec.normalize(p);
//check if there is already a mapping for this path
ListIterator<ServletMapping> listItor = _servletMappings.listIterator();
@ -1254,7 +1255,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
while (iter.hasNext())
{
String p = iter.next().toString(false, true);
p = normalizePattern(p);
p = ServletPathSpec.normalize(p);
paths.add(p);
context.getMetaData().setOrigin(filterName+".filter.mapping."+p, descriptor);
}
@ -1338,7 +1339,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
while (iter2.hasNext())
{
String url = iter2.next().toString(false, true);
url = normalizePattern(url);
url = ServletPathSpec.normalize(url);
paths.add( url);
jpg.addUrlPattern(url);
}
@ -1478,7 +1479,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
while (iter2.hasNext())
{
String url = iter2.next().toString(false, true);
url = normalizePattern(url);
url = ServletPathSpec.normalize(url);
//remember origin so we can process ServletRegistration.Dynamic.setServletSecurityElement() correctly
context.getMetaData().setOrigin("constraint.url."+url, descriptor);
@ -1947,10 +1948,4 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
return l;
}
public String normalizePattern(String p)
{
if (p != null && p.length() > 0 && !p.startsWith("/") && !p.startsWith("*")) return "/" + p;
return p;
}
}