diff --git a/web/src/main/java/org/jboss/metadata/merge/web/spec/CookieConfigMetaDataMerger.java b/web/src/main/java/org/jboss/metadata/merge/web/spec/CookieConfigMetaDataMerger.java index 1d69391f4..71ca1de5b 100644 --- a/web/src/main/java/org/jboss/metadata/merge/web/spec/CookieConfigMetaDataMerger.java +++ b/web/src/main/java/org/jboss/metadata/merge/web/spec/CookieConfigMetaDataMerger.java @@ -4,7 +4,10 @@ */ package org.jboss.metadata.merge.web.spec; +import java.util.List; + import org.jboss.metadata.merge.javaee.support.IdMetaDataImplMerger; +import org.jboss.metadata.web.spec.AttributeValueMetaData; import org.jboss.metadata.web.spec.CookieConfigMetaData; /** @@ -82,6 +85,14 @@ public static void augment(CookieConfigMetaData dest, CookieConfigMetaData webFr throw new IllegalStateException("Unresolved conflict on max age"); } } + List fragmentAttributes = webFragmentMetaData.getAttributes(); + if (fragmentAttributes != null) { + List destinationAttributes = dest.getAttributes(); + if (destinationAttributes == null) { + dest.setAttributes(fragmentAttributes); + } else if (!resolveConflicts && !destinationAttributes.equals(fragmentAttributes) && (webMetaData == null || webMetaData.getAttributes() == null)) { + throw new IllegalStateException("Unresolved conflict on cookie attributes: " + destinationAttributes); + } + } } - } diff --git a/web/src/main/java/org/jboss/metadata/parser/servlet/CookieConfigMetaDataParser.java b/web/src/main/java/org/jboss/metadata/parser/servlet/CookieConfigMetaDataParser.java index 0442a4843..a21da383f 100644 --- a/web/src/main/java/org/jboss/metadata/parser/servlet/CookieConfigMetaDataParser.java +++ b/web/src/main/java/org/jboss/metadata/parser/servlet/CookieConfigMetaDataParser.java @@ -5,11 +5,17 @@ package org.jboss.metadata.parser.servlet; +import java.util.LinkedList; +import java.util.List; + import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import org.jboss.metadata.javaee.spec.DescriptionsImpl; +import org.jboss.metadata.parser.ee.DescriptionsMetaDataParser; import org.jboss.metadata.parser.ee.IdMetaDataParser; import org.jboss.metadata.property.PropertyReplacer; +import org.jboss.metadata.web.spec.AttributeValueMetaData; import org.jboss.metadata.web.spec.CookieConfigMetaData; /** @@ -64,6 +70,16 @@ public CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplacer prope throw unexpectedValue(reader, e); } break; + case ATTRIBUTE: + if (this.since(Version.SERVLET_6_0)) { + List attributes = cookieConfig.getAttributes(); + if (attributes == null) { + attributes = new LinkedList<>(); + cookieConfig.setAttributes(attributes); + } + attributes.add(this.parseAttribute(reader, propertyReplacer)); + break; + } // else fall through default: throw unexpectedElement(reader); } @@ -71,4 +87,32 @@ public CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplacer prope return cookieConfig; } + + private static final String ATTRIBUTE_NAME = "attribute-name"; + private static final String ATTRIBUTE_VALUE = "attribute-value"; + + protected AttributeValueMetaData parseAttribute(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException { + AttributeValueMetaData metaData = new AttributeValueMetaData(); + + IdMetaDataParser.parseAttributes(reader, metaData); + + DescriptionsImpl descriptions = new DescriptionsImpl(); + while (reader.hasNext() && reader.nextTag() != END_ELEMENT) { + switch (reader.getLocalName()) { + case ATTRIBUTE_NAME: + metaData.setAttributeName(getElementText(reader, propertyReplacer)); + break; + case ATTRIBUTE_VALUE: + metaData.setAttributeValue(getElementText(reader, propertyReplacer)); + break; + default: + if (DescriptionsMetaDataParser.parse(reader, descriptions, propertyReplacer)) { + metaData.setDescriptions(descriptions); + } else { + throw unexpectedElement(reader); + } + } + } + return metaData; + } } diff --git a/web/src/main/java/org/jboss/metadata/parser/servlet/Element.java b/web/src/main/java/org/jboss/metadata/parser/servlet/Element.java index c5d9c362a..92892363d 100644 --- a/web/src/main/java/org/jboss/metadata/parser/servlet/Element.java +++ b/web/src/main/java/org/jboss/metadata/parser/servlet/Element.java @@ -21,6 +21,7 @@ public enum Element { ABSOLUTE_ORDERING("absolute-ordering"), AFTER("after"), ASYNC_SUPPORTED("async-supported"), + ATTRIBUTE("attribute"), AUTH_CONSTRAINT("auth-constraint"), AUTH_METHOD("auth-method"), diff --git a/web/src/main/java/org/jboss/metadata/web/spec/AttributeValueMetaData.java b/web/src/main/java/org/jboss/metadata/web/spec/AttributeValueMetaData.java new file mode 100644 index 000000000..cdeb0c474 --- /dev/null +++ b/web/src/main/java/org/jboss/metadata/web/spec/AttributeValueMetaData.java @@ -0,0 +1,34 @@ +/* + * Copyright The JBoss Metadata Authors + * SPDX-License-Identifier: Apache-2.0 + */ +package org.jboss.metadata.web.spec; + +import org.jboss.metadata.javaee.support.IdMetaDataImplWithDescriptions; + +/** + * Describes an attribute name/value pair. + * @author Paul Ferraro + */ +public class AttributeValueMetaData extends IdMetaDataImplWithDescriptions { + private static final long serialVersionUID = 1; + + private String attributeName; + private String attributeValue; + + public String getAttributeName() { + return this.attributeName; + } + + public void setAttributeName(String attributeName) { + this.attributeName = attributeName; + } + + public String getAttributeValue() { + return this.attributeValue; + } + + public void setAttributeValue(String attributeValue) { + this.attributeValue = attributeValue; + } +} diff --git a/web/src/main/java/org/jboss/metadata/web/spec/CookieConfigMetaData.java b/web/src/main/java/org/jboss/metadata/web/spec/CookieConfigMetaData.java index 11a045cb0..42981fa5f 100644 --- a/web/src/main/java/org/jboss/metadata/web/spec/CookieConfigMetaData.java +++ b/web/src/main/java/org/jboss/metadata/web/spec/CookieConfigMetaData.java @@ -4,6 +4,8 @@ */ package org.jboss.metadata.web.spec; +import java.util.List; + import org.jboss.metadata.javaee.support.IdMetaDataImpl; /** @@ -23,6 +25,7 @@ public class CookieConfigMetaData extends IdMetaDataImpl { private boolean secureSet = false; private int maxAge = -1; private boolean maxAgeSet = false; + private List attributes; public String getName() { return name; @@ -83,6 +86,14 @@ public void setMaxAge(int maxAge) { maxAgeSet = true; } + public List getAttributes() { + return this.attributes; + } + + public void setAttributes(List attributes) { + this.attributes = attributes; + } + public boolean getHttpOnlySet() { return httpOnlySet; } diff --git a/web/src/test/java/org/jboss/test/metadata/web/WebApp10EverythingUnitTestCase.java b/web/src/test/java/org/jboss/test/metadata/web/WebApp10EverythingUnitTestCase.java index 0e5ccef53..9ce9dcd40 100644 --- a/web/src/test/java/org/jboss/test/metadata/web/WebApp10EverythingUnitTestCase.java +++ b/web/src/test/java/org/jboss/test/metadata/web/WebApp10EverythingUnitTestCase.java @@ -4,7 +4,10 @@ */ package org.jboss.test.metadata.web; +import java.util.List; + import org.jboss.metadata.merge.javaee.spec.JavaEEVersion; +import org.jboss.metadata.web.spec.AttributeValueMetaData; import org.jboss.metadata.web.spec.WebMetaData; /** @@ -19,4 +22,13 @@ public void testEverything() throws Exception { WebMetaData webApp = unmarshal(); assertEverything(webApp, Mode.SPEC, JavaEEVersion.V10); } + + protected void assertCookieAttributes(List attributes) { + assertNotNull("cookie attributes not set", attributes); + assertEquals(2, attributes.size()); + assertEquals("SameSite", attributes.get(0).getAttributeName()); + assertEquals("None", attributes.get(0).getAttributeValue()); + assertEquals("foo", attributes.get(1).getAttributeName()); + assertEquals("bar", attributes.get(1).getAttributeValue()); + } } diff --git a/web/src/test/java/org/jboss/test/metadata/web/WebApp6EverythingUnitTestCase.java b/web/src/test/java/org/jboss/test/metadata/web/WebApp6EverythingUnitTestCase.java index 1e438ac9a..1a27cf405 100644 --- a/web/src/test/java/org/jboss/test/metadata/web/WebApp6EverythingUnitTestCase.java +++ b/web/src/test/java/org/jboss/test/metadata/web/WebApp6EverythingUnitTestCase.java @@ -13,6 +13,7 @@ import org.jboss.metadata.javaee.spec.SecurityRoleRefsMetaData; import org.jboss.metadata.merge.javaee.spec.JavaEEVersion; import org.jboss.metadata.web.spec.AbsoluteOrderingMetaData; +import org.jboss.metadata.web.spec.AttributeValueMetaData; import org.jboss.metadata.web.spec.AuthConstraintMetaData; import org.jboss.metadata.web.spec.CookieConfigMetaData; import org.jboss.metadata.web.spec.DispatcherType; @@ -236,5 +237,10 @@ protected void assertCookieConfig(CookieConfigMetaData metaData) { assertTrue(metaData.getSecure()); assertTrue(metaData.getMaxAgeSet()); assertEquals(10, metaData.getMaxAge()); + assertCookieAttributes(metaData.getAttributes()); + } + + protected void assertCookieAttributes(List attributes) { + assertNull(attributes); } }