From ce6054391cb00124dd225a6d83a756628916d5e5 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 1 Dec 2016 11:57:05 +1100 Subject: [PATCH] Issue #877 --- .../QuickStartDescriptorGenerator.java | 11 +++ .../QuickStartDescriptorProcessor.java | 67 ++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java index e1d6e473803..c78e2dc2a7b 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java @@ -77,6 +77,7 @@ public class QuickStartDescriptorGenerator { private static final Logger LOG = Log.getLogger(QuickStartDescriptorGenerator.class); + public static final String ORIGIN = "org.eclipse.jetty.originAttribute"; public static final String DEFAULT_QUICKSTART_DESCRIPTOR_NAME = "quickstart-web.xml"; public static final String DEFAULT_ORIGIN_ATTRIBUTE_NAME = "origin"; @@ -152,6 +153,16 @@ public class QuickStartDescriptorGenerator addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_RESOURCES,normalizer); + //add the name of the origin attribute, if it is being used + if (_generateOrigin) + { + out.openTag("context-param") + .tag("param-name", ORIGIN) + .tag("param-value", _originAttribute) + .closeTag(); + } + + // init params for (String p : _webApp.getInitParams().keySet()) out.openTag("context-param",origin(md,"context-param." + p)) diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java index 35d41ea17ae..e006c1e015f 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java @@ -28,9 +28,12 @@ import javax.servlet.ServletContext; import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.annotations.ServletContainerInitializersStarter; import org.eclipse.jetty.plus.annotation.ContainerInitializer; +import org.eclipse.jetty.servlet.ServletMapping; import org.eclipse.jetty.util.QuotedStringTokenizer; +import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceCollection; +import org.eclipse.jetty.webapp.DefaultsDescriptor; import org.eclipse.jetty.webapp.Descriptor; import org.eclipse.jetty.webapp.IterativeDescriptorProcessor; import org.eclipse.jetty.webapp.MetaInfConfiguration; @@ -44,11 +47,18 @@ import org.eclipse.jetty.xml.XmlParser; */ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor { + + private String _originAttributeName = null; + + /** + * + */ public QuickStartDescriptorProcessor() { try { registerVisitor("context-param", this.getClass().getMethod("visitContextParam", __signature)); + registerVisitor("servlet-mapping", this.getClass().getMethod("visitServletMapping", __signature)); } catch (Exception e) { @@ -62,6 +72,7 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor @Override public void start(WebAppContext context, Descriptor descriptor) { + _originAttributeName = context.getInitParameter(QuickStartDescriptorGenerator.ORIGIN); } /** @@ -70,8 +81,50 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor @Override public void end(WebAppContext context, Descriptor descriptor) { + _originAttributeName = null; + } + + + /** + * Process a servlet-mapping element + * + * @param context the webapp + * @param descriptor the xml file to process + * @param node the servlet-mapping element in the xml file to process + */ + public void visitServletMapping(WebAppContext context, Descriptor descriptor, XmlParser.Node node) + { + String servletName = node.getString("servlet-name", false, true); + ServletMapping mapping = null; + ServletMapping[] mappings = context.getServletHandler().getServletMappings(); + + if (mappings != null) + { + for (ServletMapping m:mappings) + { + if (servletName.equals(m.getServletName())) + { + mapping = m; + break; + } + } + } + + if (mapping != null && _originAttributeName != null) + { + String origin = node.getAttribute(_originAttributeName); + if (!StringUtil.isBlank(origin) && origin.startsWith(DefaultsDescriptor.class.getSimpleName())) + mapping.setDefault(true); + } } + /** + * Process a context-param element + * @param context the webapp + * @param descriptor the xml file to process + * @param node the context-param node in the xml file + * @throws Exception + */ public void visitContextParam (WebAppContext context, Descriptor descriptor, XmlParser.Node node) throws Exception { @@ -82,11 +135,16 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor // extract values switch(name) { + case QuickStartDescriptorGenerator.ORIGIN: + { + //value already contains what we need + break; + } case ServletContext.ORDERED_LIBS: case AnnotationConfiguration.CONTAINER_INITIALIZERS: case MetaInfConfiguration.METAINF_TLDS: case MetaInfConfiguration.METAINF_RESOURCES: - + { context.removeAttribute(name); QuotedStringTokenizer tok = new QuotedStringTokenizer(value,","); @@ -94,7 +152,7 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor values.add(tok.nextToken().trim()); break; - + } default: values.add(value); } @@ -103,6 +161,11 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor // handle values switch(name) { + case QuickStartDescriptorGenerator.ORIGIN: + { + context.setAttribute(QuickStartDescriptorGenerator.ORIGIN, value); + break; + } case ServletContext.ORDERED_LIBS: { List libs = new ArrayList<>();