|
| 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