Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more origin info to quickstart-web.xml elements #5400

Merged
merged 8 commits into from
Oct 14, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void doHandle(Class clazz)
for (String r : roles)
{
((ConstraintSecurityHandler)_context.getSecurityHandler()).addRole(r);
_context.getMetaData().setOrigin("security-role." + r, declareRoles, clazz);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void apply()

FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());

metaData.setOrigin(name + ".filter.mapping." + Long.toHexString(mapping.hashCode()), filterAnnotation, clazz);
if (urlPatterns.length > 0)
{
ArrayList<String> paths = new ArrayList<String>();
Expand Down Expand Up @@ -179,7 +179,7 @@ public void apply()
{
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());

metaData.setOrigin(holder.getName() + ".filter.mapping." + Long.toHexString(mapping.hashCode()), filterAnnotation, clazz);
if (urlPatterns.length > 0)
{
ArrayList<String> paths = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public void apply()
mapping = new ServletMapping(source);
mapping.setServletName(holder.getName());
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
_context.getMetaData().setOrigin(servletName + ".servlet.mapping." + Long.toHexString(mapping.hashCode()), annotation, clazz);
}
else
{
Expand Down Expand Up @@ -190,6 +191,7 @@ public void apply()
mapping = new ServletMapping(new Source(Source.Origin.ANNOTATION, clazz.getName()));
mapping.setServletName(servletName);
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
_context.getMetaData().setOrigin(servletName + ".servlet.mapping." + Long.toHexString(mapping.hashCode()), annotation, clazz);
}
}

Expand Down Expand Up @@ -228,7 +230,7 @@ public void apply()
LOG.debug("Removed path {} from mapping {} from defaults descriptor ", p, existingMapping);
}
}
_context.getMetaData().setOrigin(servletName + ".servlet.mapping." + p, annotation, clazz);
_context.getMetaData().setOrigin(servletName + ".servlet.mapping.url" + p, annotation, clazz);
}
allMappings.add(mapping);
_context.getServletHandler().setServletMappings(allMappings.toArray(new ServletMapping[allMappings.size()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.eclipse.jetty.quickstart;

import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.webapp.Descriptor;
import org.eclipse.jetty.webapp.IterativeDescriptorProcessor;
import org.eclipse.jetty.webapp.WebAppContext;
Expand All @@ -36,12 +37,11 @@ public class ExtraXmlDescriptorProcessor extends IterativeDescriptorProcessor
private static final Logger LOG = LoggerFactory.getLogger(ExtraXmlDescriptorProcessor.class);

private final StringBuilder _buffer = new StringBuilder();
private final boolean _showOrigin;
private String _originAttribute;
private String _origin;

public ExtraXmlDescriptorProcessor()
{
_showOrigin = LOG.isDebugEnabled();
try
{
registerVisitor("env-entry", getClass().getMethod("saveSnippet", __signature));
Expand All @@ -60,19 +60,27 @@ public ExtraXmlDescriptorProcessor()
public void start(WebAppContext context, Descriptor descriptor)
{
LOG.debug("process {}", descriptor);
_origin = (" <!-- " + descriptor + " -->\n");
_origin = (StringUtil.isBlank(_originAttribute) ? null : " <!-- " + descriptor + " -->\n");
}

@Override
public void end(WebAppContext context, Descriptor descriptor)
{
}

public void setOriginAttribute(String name)
{
_originAttribute = name;
}

public void saveSnippet(WebAppContext context, Descriptor descriptor, XmlParser.Node node)
throws Exception
{
//Note: we have to output the origin as a comment field instead of
//as an attribute like the other other elements because
//we are copying these elements _verbatim_ from the descriptor
LOG.debug("save {}", node.getTag());
if (_showOrigin)
if (_origin != null)
_buffer.append(_origin);
_buffer.append(" ").append(node.toString()).append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
if (f != null && f.getSource() == Source.EMBEDDED)
continue;

out.openTag("filter-mapping");
out.openTag("filter-mapping", origin(md, mapping.getFilterName() + ".filter.mapping." + Long.toHexString(mapping.hashCode())));
out.tag("filter-name", mapping.getFilterName());
if (mapping.getPathSpecs() != null)
for (String s : mapping.getPathSpecs())
Expand Down Expand Up @@ -289,7 +289,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
if (sh != null && sh.getSource() == Source.EMBEDDED)
continue;

out.openTag("servlet-mapping", origin(md, mapping.getServletName() + ".servlet.mappings"));
out.openTag("servlet-mapping", origin(md, mapping.getServletName() + ".servlet.mapping." + Long.toHexString(mapping.hashCode())));
out.tag("servlet-name", mapping.getServletName());
if (mapping.getPathSpecs() != null)
for (String s : mapping.getPathSpecs())
Expand Down Expand Up @@ -327,7 +327,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
ConstraintAware ca = (ConstraintAware)security;
for (String r : ca.getRoles())
{
out.openTag("security-role")
out.openTag("security-role", origin(md, "security-role." + r))
.tag("role-name", r)
.closeTag();
}
Expand Down Expand Up @@ -398,7 +398,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
out.openTag("welcome-file-list");
for (String welcomeFile : context.getWelcomeFiles())
{
out.tag("welcome-file", welcomeFile);
out.tag("welcome-file", origin(md, "welcome-file." + welcomeFile), welcomeFile);
}
out.closeTag();
}
Expand Down Expand Up @@ -429,6 +429,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
if (cookieConfig != null)
{
out.openTag("cookie-config");

if (cookieConfig.getName() != null)
out.tag("name", origin(md, "cookie-config.name"), cookieConfig.getName());

Expand Down Expand Up @@ -789,6 +790,7 @@ public Map<String, String> origin(MetaData md, String name)
public void preConfigure(WebAppContext context) throws Exception
{
ExtraXmlDescriptorProcessor extraXmlProcessor = new ExtraXmlDescriptorProcessor();
extraXmlProcessor.setOriginAttribute(getOriginAttribute());
context.getMetaData().addDescriptorProcessor(extraXmlProcessor);
context.setAttribute(ExtraXmlDescriptorProcessor.class.getName(), extraXmlProcessor);
super.preConfigure(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml
if (TimeUnit.MINUTES.toSeconds(mins) > Integer.MAX_VALUE)
throw new IllegalStateException("Max session-timeout in minutes is " + TimeUnit.SECONDS.toMinutes(Integer.MAX_VALUE));
context.getServletContext().setSessionTimeout((int)mins);
context.getMetaData().setOrigin("session.timeout", descriptor);
}

//Servlet Spec 3.0
Expand All @@ -672,15 +673,15 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml
if (iter.hasNext())
{
Set<SessionTrackingMode> modes = null;
Origin o = context.getMetaData().getOrigin("session.tracking-mode");
Origin o = context.getMetaData().getOrigin("session.tracking-modes");
switch (o)
{
case NotSet://not previously set, starting fresh
case WebDefaults://previously set in web defaults, allow this descriptor to start fresh
{

modes = new HashSet<SessionTrackingMode>();
context.getMetaData().setOrigin("session.tracking-mode", descriptor);
context.getMetaData().setOrigin("session.tracking-modes", descriptor);
break;
}
case WebXml:
Expand All @@ -692,7 +693,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml
modes = new HashSet<SessionTrackingMode>();
else
modes = new HashSet<SessionTrackingMode>(context.getSessionHandler().getEffectiveSessionTrackingModes());
context.getMetaData().setOrigin("session.tracking-mode", descriptor);
context.getMetaData().setOrigin("session.tracking-modes", descriptor);
break;
}
default:
Expand All @@ -703,7 +704,9 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml
{
XmlParser.Node mNode = (XmlParser.Node)iter.next();
String trackMode = mNode.toString(false, true);
modes.add(SessionTrackingMode.valueOf(trackMode));
SessionTrackingMode mode = SessionTrackingMode.valueOf(trackMode);
modes.add(mode);
context.getMetaData().setOrigin("session.tracking-mode." + mode, descriptor);
}
context.getSessionHandler().setSessionTrackingModes(modes);
}
Expand Down Expand Up @@ -1035,13 +1038,13 @@ public void visitWelcomeFileList(WebAppContext context, Descriptor descriptor, X
case NotSet:
{
context.getMetaData().setOrigin("welcome-file-list", descriptor);
addWelcomeFiles(context, node);
addWelcomeFiles(context, node, descriptor);
break;
}
case WebXml:
{
//web.xml set the welcome-file-list, all other descriptors then just merge in
addWelcomeFiles(context, node);
addWelcomeFiles(context, node, descriptor);
break;
}
case WebDefaults:
Expand All @@ -1052,19 +1055,19 @@ public void visitWelcomeFileList(WebAppContext context, Descriptor descriptor, X
{
context.setWelcomeFiles(new String[0]);
}
addWelcomeFiles(context, node);
addWelcomeFiles(context, node, descriptor);
break;
}
case WebOverride:
{
//web-override set the list, all other descriptors just merge in
addWelcomeFiles(context, node);
addWelcomeFiles(context, node, descriptor);
break;
}
case WebFragment:
{
//A web-fragment first set the welcome-file-list. Other descriptors just add.
addWelcomeFiles(context, node);
addWelcomeFiles(context, node, descriptor);
break;
}
default:
Expand Down Expand Up @@ -1182,14 +1185,14 @@ public void visitErrorPage(WebAppContext context, Descriptor descriptor, XmlPars
}
}

public void addWelcomeFiles(WebAppContext context, XmlParser.Node node)
public void addWelcomeFiles(WebAppContext context, XmlParser.Node node, Descriptor descriptor)
{
Iterator<XmlParser.Node> iter = node.iterator("welcome-file");
while (iter.hasNext())
{
XmlParser.Node indexNode = (XmlParser.Node)iter.next();
String welcome = indexNode.toString(false, true);

context.getMetaData().setOrigin("welcome-file." + welcome, descriptor);
//Servlet Spec 3.0 p. 74 welcome files are additive
if (welcome != null && welcome.trim().length() > 0)
context.setWelcomeFiles((String[])ArrayUtil.addToArray(context.getWelcomeFiles(), welcome, String.class));
Expand All @@ -1201,7 +1204,8 @@ public ServletMapping addServletMapping(String servletName, XmlParser.Node node,
ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
mapping.setServletName(servletName);
mapping.setFromDefaultDescriptor(descriptor instanceof DefaultsDescriptor);

context.getMetaData().setOrigin(servletName + ".servlet.mapping." + Long.toHexString(mapping.hashCode()), descriptor);

List<String> paths = new ArrayList<String>();
Iterator<XmlParser.Node> iter = node.iterator("url-pattern");
while (iter.hasNext())
Expand Down Expand Up @@ -1255,7 +1259,7 @@ public ServletMapping addServletMapping(String servletName, XmlParser.Node node,
}

paths.add(p);
context.getMetaData().setOrigin(servletName + ".servlet.mapping." + p, descriptor);
context.getMetaData().setOrigin(servletName + ".servlet.mapping.url" + p, descriptor);
}

mapping.setPathSpecs((String[])paths.toArray(new String[paths.size()]));
Expand All @@ -1269,15 +1273,15 @@ public void addFilterMapping(String filterName, XmlParser.Node node, WebAppConte
{
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(filterName);

context.getMetaData().setOrigin(filterName + ".filter.mapping." + Long.toHexString(mapping.hashCode()), descriptor);
List<String> paths = new ArrayList<String>();
Iterator<XmlParser.Node> iter = node.iterator("url-pattern");
while (iter.hasNext())
{
String p = iter.next().toString(false, true);
p = ServletPathSpec.normalize(p);
paths.add(p);
context.getMetaData().setOrigin(filterName + ".filter.mapping." + p, descriptor);
context.getMetaData().setOrigin(filterName + ".filter.mapping.url" + p, descriptor);
}
mapping.setPathSpecs((String[])paths.toArray(new String[paths.size()]));

Expand All @@ -1286,6 +1290,7 @@ public void addFilterMapping(String filterName, XmlParser.Node node, WebAppConte
while (iter.hasNext())
{
String n = ((XmlParser.Node)iter.next()).toString(false, true);
context.getMetaData().setOrigin(filterName + ".filter.mapping.servlet" + n, descriptor);
names.add(n);
}
mapping.setServletNames((String[])names.toArray(new String[names.size()]));
Expand Down Expand Up @@ -1741,6 +1746,7 @@ public void visitSecurityRole(WebAppContext context, Descriptor descriptor, XmlP
XmlParser.Node roleNode = node.get("role-name");
String role = roleNode.toString(false, true);
((ConstraintAware)context.getSecurityHandler()).addRole(role);
context.getMetaData().setOrigin("security-role." + role, descriptor);
}

public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser.Node node)
Expand Down