Skip to content

Commit

Permalink
JBMETA-457 Add missing metadata/parser support for cookie attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pferraro committed Jul 19, 2024
1 parent 2582e4b commit c074912
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -82,6 +85,14 @@ public static void augment(CookieConfigMetaData dest, CookieConfigMetaData webFr
throw new IllegalStateException("Unresolved conflict on max age");
}
}
List<AttributeValueMetaData> fragmentAttributes = webFragmentMetaData.getAttributes();
if (fragmentAttributes != null) {
List<AttributeValueMetaData> 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);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -64,11 +70,49 @@ public CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplacer prope
throw unexpectedValue(reader, e);
}
break;
case ATTRIBUTE:
if (this.since(Version.SERVLET_6_0)) {
List<AttributeValueMetaData> 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);
}
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.jboss.metadata.web.spec;

import java.util.List;

import org.jboss.metadata.javaee.support.IdMetaDataImpl;

/**
Expand All @@ -23,6 +25,7 @@ public class CookieConfigMetaData extends IdMetaDataImpl {
private boolean secureSet = false;
private int maxAge = -1;
private boolean maxAgeSet = false;
private List<AttributeValueMetaData> attributes;

public String getName() {
return name;
Expand Down Expand Up @@ -83,6 +86,14 @@ public void setMaxAge(int maxAge) {
maxAgeSet = true;
}

public List<AttributeValueMetaData> getAttributes() {
return this.attributes;
}

public void setAttributes(List<AttributeValueMetaData> attributes) {
this.attributes = attributes;
}

public boolean getHttpOnlySet() {
return httpOnlySet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,4 +22,13 @@ public void testEverything() throws Exception {
WebMetaData webApp = unmarshal();
assertEverything(webApp, Mode.SPEC, JavaEEVersion.V10);
}

protected void assertCookieAttributes(List<AttributeValueMetaData> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AttributeValueMetaData> attributes) {
assertNull(attributes);
}
}

0 comments on commit c074912

Please sign in to comment.