Skip to content

Commit c844f5c

Browse files
Added support for BomLink
1 parent 2a28ba5 commit c844f5c

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of CycloneDX Core (Java).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
package org.cyclonedx.exception;
20+
21+
/**
22+
* Exception throws when parsing CycloneDX BOM-Link URNs.
23+
*
24+
* @author Steve Springett
25+
* @since 7.1.4
26+
*/
27+
public class BomLinkException extends Exception {
28+
29+
/**
30+
* Constructs a new exception.
31+
* @param message the detail message.
32+
* @since 7.1.4
33+
*/
34+
public BomLinkException(String message) {
35+
super(message);
36+
}
37+
38+
/**
39+
* Constructs a new exception.
40+
* @param cause the cause
41+
* @since 7.1.4
42+
*/
43+
public BomLinkException(Throwable cause) {
44+
super(cause);
45+
}
46+
47+
/**
48+
* Constructs a new exception.
49+
* @param message the detail message.
50+
* @param cause the cause
51+
* @since 7.1.4
52+
*/
53+
public BomLinkException(String message, Throwable cause) {
54+
super(message, cause);
55+
}
56+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of CycloneDX Core (Java).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
package org.cyclonedx.util;
20+
21+
import org.cyclonedx.exception.BomLinkException;
22+
import java.net.URI;
23+
import java.net.URISyntaxException;
24+
import java.util.UUID;
25+
26+
/**
27+
* Parses URNs that conform to the "cdx" namespace identifier
28+
* as defined by https://www.iana.org/assignments/urn-formal/cdx
29+
* @since 7.1.4
30+
*/
31+
public class BomLink {
32+
33+
private static final String EXCEPTION_MESSAGE = "Invalid BOM-Link. URN syntax must conform to \"urn:cdx:serialNumber/version#bom-ref\" where serialNumber is a valid UUID (required), version is an integer (required), and bom-ref is optional.";
34+
private final UUID serialNumber;
35+
private final int version;
36+
private final String bomRef;
37+
38+
public BomLink(final String urn) throws BomLinkException {
39+
if (urn != null && urn.startsWith("urn:cdx:")) {
40+
try {
41+
final URI uri = new URI(urn);
42+
final String[] parts = uri.getSchemeSpecificPart().split("/");
43+
if (parts.length == 2) {
44+
this.serialNumber = UUID.fromString(parts[0].replace("cdx:", ""));
45+
this.version = Integer.parseInt(parts[1]);
46+
this.bomRef = uri.getFragment();
47+
} else {
48+
throw new BomLinkException(EXCEPTION_MESSAGE);
49+
}
50+
} catch (URISyntaxException | IllegalArgumentException e) {
51+
throw new BomLinkException(EXCEPTION_MESSAGE, e);
52+
}
53+
} else {
54+
throw new BomLinkException(EXCEPTION_MESSAGE);
55+
}
56+
}
57+
58+
public static boolean isBomLink(final String bomRef) {
59+
try {
60+
new BomLink(bomRef);
61+
return true;
62+
} catch (BomLinkException e) {
63+
return false;
64+
}
65+
}
66+
67+
public UUID getSerialNumber() {
68+
return serialNumber;
69+
}
70+
71+
public int getVersion() {
72+
return version;
73+
}
74+
75+
public String getBomRef() {
76+
return bomRef;
77+
}
78+
}

0 commit comments

Comments
 (0)