@@ -104,6 +104,7 @@ public PackageURL(final String type, final String namespace, final String name,
104104 this .version = validateVersion (version );
105105 this .qualifiers = validateQualifiers (qualifiers );
106106 this .subpath = validatePath (subpath , true );
107+ verifyTypeConstraints (this .type , this .namespace , this .name );
107108 }
108109
109110 /**
@@ -533,11 +534,27 @@ private void parse(final String purl) throws MalformedPackageURLException {
533534 remainder .setLength (index );
534535 this .namespace = validateNamespace (parsePath (remainder .substring (start ), false ));
535536 }
537+ verifyTypeConstraints (this .type , this .namespace , this .name );
536538 } catch (URISyntaxException e ) {
537539 throw new MalformedPackageURLException ("Invalid purl: " + e .getMessage ());
538540 }
539541 }
540542
543+ /**
544+ * Some purl types may have specific constraints. This method attempts to verify them.
545+ * @param type the purl type
546+ * @param namespace the purl namespace
547+ * @param name the purl name
548+ * @throws MalformedPackageURLException if constraints are not met
549+ */
550+ private void verifyTypeConstraints (String type , String namespace , String name ) throws MalformedPackageURLException {
551+ if (StandardTypes .MAVEN .equals (type )) {
552+ if (namespace == null || namespace .isEmpty () || name == null || name .isEmpty ()) {
553+ throw new MalformedPackageURLException ("The PackageURL specified is invalid. Maven requires both a namespace and name." );
554+ }
555+ }
556+ }
557+
541558 @ SuppressWarnings ("StringSplitter" )//reason: surprising behavior is okay in this case
542559 private Map <String , String > parseQualifiers (final String encodedString ) throws MalformedPackageURLException {
543560 try {
0 commit comments