Skip to content

Conversation

Copy link

Copilot AI commented Oct 5, 2025

Fixes issue where the plugin fails to resolve schema URLs that redirect from HTTP to HTTPS, causing validation errors with "301 Moved Permanently" messages.

Problem

When XML schemas are accessed via HTTP URLs that redirect to HTTPS (like the OASIS catalog.xsd), the plugin would fail with errors like:

error: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw '301 Moved Permanently'.

This occurs because Java's URL.openStream() doesn't automatically follow cross-protocol redirects (HTTP to HTTPS) for security reasons.

Solution

Added openStreamWithRedirects() method to the Resolver class that:

  • Handles HTTP redirects including cross-protocol redirects (HTTP to HTTPS)
  • Supports redirect status codes: 301, 302, 303, 307, 308
  • Prevents infinite redirect loops by limiting to maximum of 5 redirects
  • Throws clear error messages when redirects are malformed or exceed limit

Updated asInputSource() and resolveAsURL() methods to use the new redirect-aware stream opening.

Testing

  • ✅ All existing unit tests pass (23 tests, 0 failures)
  • ✅ Full build verification complete (mvn clean verify)
  • ✅ Code formatting verified with Spotless

Fixes #59

Original prompt

This section details on the original issue you should resolve

<issue_title>Does not follow redirects when resolving schema for catalog.xml</issue_title>
<issue_description>I suddenly got the following error when executing validate in build which has previously worked for many years:

[ERROR] Failed to execute goal org.codehaus.mojo:xml-maven-plugin:1.0.2:validate (default) on project X: While parsing projectroot/catalog.xml, at http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd, line 2,  column 35: error: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw '301 Moved Permanently'.
[ERROR] While parsing projectroot/catalog.xml, at http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd, line 4,  column 36: error: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw '301 Moved Permanently'.
[ERROR] While parsing projectroot/catalog.xml, at http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd, line 5,  column 20: error: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'nginx'.
[ERROR] While parsing projectroot/catalog.xml, at http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd, line 6,  column 3: fatal error: The element type "hr" must be terminated by the matching end-tag "</hr>".
[ERROR] While parsing projectroot/catalog.xml, at http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd, line 6,  column 3: fatal error: The element type "hr" must be terminated by the matching end-tag "</hr>".

This hints about some error related to redirecting: "Saw '301 Moved Permanently'".
Executing curl -sLD - http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd confirms the schema-URL has indeed been moved:

$ curl -sLD - http://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 11 Nov 2020 18:31:18 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://www.oasis-open.org/committees/entity/release/1.1/catalog.xsd

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 11 Nov 2020 18:31:19 GMT
Content-Type: application/xml
Content-Length: 8744
Connection: keep-alive
Last-Modified: Wed, 11 Nov 2020 03:21:33 GMT
ETag: "17c9e0-2228-5b3cc4ac9e940"
Accept-Ranges: bytes
Strict-Transport-Security: "max-age=31536000; includeSubDomains" always
...

I have changed the URL in my catalog.xml to https, and this solves the problem, but the http-client used internally by xml-maven-plugin should maybe be set up to follow redirects?</issue_description>

Comments on the Issue (you are @copilot in this section)

@jochenw Just for the record: It appears, that the http URL works again, so we don't have something reproducable. @jochenw @runeflobakk Could you provide the relevant snippet from your POM? Catalog files? Whatever else you are using?
Fixes #59

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Created openStreamWithRedirects() method to handle HTTP redirects
- Handles cross-protocol redirects (HTTP to HTTPS) which Java's default URLConnection doesn't follow
- Updated asInputSource() and resolveAsURL() to use the new method
- Prevents redirect loops by limiting to 5 redirects maximum
- Fixes issue where catalog.xml schema URLs with 301 redirects were failing

Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
Copilot AI requested a review from slachiewicz October 5, 2025 18:25
@slachiewicz

This comment was marked as resolved.

@slachiewicz

This comment was marked as resolved.

Copy link
Author

Copilot AI commented Oct 6, 2025

@copilot continue

Implementation complete and verified. All tests pass successfully (commit 3b83d6a). The HTTP redirect support now properly handles cross-protocol redirects (HTTP to HTTPS) which resolves the catalog.xsd validation errors.

Copilot AI changed the title [WIP] Does not follow redirects when resolving schema for catalog.xml Fix HTTP redirect handling when resolving schemas for catalog.xml Oct 6, 2025
@slachiewicz slachiewicz marked this pull request as ready for review October 6, 2025 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does not follow redirects when resolving schema for catalog.xml

1 participant