This is an automated email from the ASF dual-hosted git repository. dkulp pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 9dd5405a7cc680232a90b68d22c5f4d6d1a4b37e Author: neseleznev <ne.selez...@gmail.com> AuthorDate: Tue Nov 15 17:40:57 2022 +0200 CXF-8698: Add javadoc and tests to createContentID --- .../org/apache/cxf/attachment/AttachmentUtil.java | 20 ++++++++ .../apache/cxf/attachment/AttachmentUtilTest.java | 53 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java index e585d26ca1..0469b266c9 100644 --- a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java +++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java @@ -231,6 +231,26 @@ public final class AttachmentUtil { } } + /** + * Creates Content ID from {@link #ATT_UUID} and given namespace + * <p> + * Example: + * <pre>6976d00d-740c-48ed-b63d-8c56707544f...@example.com</pre> + * <p> + * <a href="https://datatracker.ietf.org/doc/html/rfc2392#section-2">RFC-2392 Section 2 (The MID and CID URL Schemes)</a> + * specifies Content ID as: + * <pre> + * content-id = url-addr-spec + * url-addr-spec = addr-spec ; URL encoding of RFC 822 addr-spec + * </pre> + * <a href="https://datatracker.ietf.org/doc/html/rfc822#appendix-D">RFC-822 Appendix D (ALPHABETICAL LISTING OF SYNTAX RULES)</a>: + * <pre> + * addr-spec = local-part "@" domain ; global address + * </pre> + * + * @param ns namespace. If null, falls back to "cxf.apache.org" + * @return Content ID + */ public static String createContentID(String ns) throws UnsupportedEncodingException { // tend to change String cid = "cxf.apache.org"; diff --git a/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java b/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java index 19c3564c2a..2c3b5f36b4 100644 --- a/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java +++ b/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java @@ -26,11 +26,15 @@ import org.apache.cxf.io.CachedOutputStream; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageImpl; +import org.junit.Ignore; import org.junit.Test; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.matchesPattern; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -133,6 +137,55 @@ public class AttachmentUtilTest { assertNotEquals(AttachmentUtil.createContentID(null), AttachmentUtil.createContentID(null)); } + @Test + public void testCreateContentIDWithNullDomainNamePassed() throws Exception { + String actual = AttachmentUtil.createContentID(null); + + // Yet RFC822 allows more characters in domain-literal, + // this regex is enough to check that the fallback domain is compliant + assertThat(actual, matchesPattern(".+@\\w+(\\.\\w+)*")); + } + + @Test + public void testCreateContentIDWithDomainNamePassed() throws Exception { + String domain = "subdomain.example.com"; + + String actual = AttachmentUtil.createContentID(domain); + + assertThat(actual, endsWith("@" + domain)); + } + + @Test + public void testCreateContentIDWithUrlPassed() throws Exception { + String domain = "subdomain.example.com"; + String url = "https://" + domain + "/a/b/c"; + + String actual = AttachmentUtil.createContentID(url); + + assertThat(actual, endsWith("@" + domain)); + } + + @Test + public void testCreateContentIDWithIPv4BasedUrlPassed() throws Exception { + String domain = "127.0.0.1"; + String url = "https://" + domain + "/a/b/c"; + + String actual = AttachmentUtil.createContentID(url); + + assertThat(actual, endsWith("@" + domain)); + } + + @Test + @Ignore //TODO:8698 Content-Id should contain valid domain, but IPv6 input results in URL-encoded string + public void testCreateContentIDWithIPv6BasedUrlPassed() throws Exception { + String domain = "[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]"; + String url = "http://" + domain + "/a/b/c"; + + String actual = AttachmentUtil.createContentID(url); + + assertThat(actual, endsWith("@" + domain)); + } + private CachedOutputStream testSetStreamedAttachmentProperties(final String property, final Object value) throws IOException { return testSetStreamedAttachmentProperties(property, value, new CachedOutputStream());