sop-java/sop-java/src/main/java/sop/Profile.java

83 lines
2.7 KiB
Java
Raw Normal View History

2023-04-14 14:03:22 +02:00
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop;
2023-04-27 13:17:58 +02:00
import sop.util.UTF8Util;
2023-04-17 13:39:36 +02:00
2023-04-14 14:03:22 +02:00
/**
* Tuple class bundling a profile name and description.
*
* @see <a href="https://www.ietf.org/archive/id/draft-dkg-openpgp-stateless-cli-05.html#name-profile">
* SOP Spec - Profile</a>
*/
public class Profile {
private final String name;
private final String description;
2023-04-17 13:39:36 +02:00
/**
* Create a new {@link Profile} object.
* The {@link #toString()} representation MUST NOT exceed a length of 1000 bytes.
*
* @param name profile name
* @param description profile description
*/
2023-04-14 14:03:22 +02:00
public Profile(String name, String description) {
this.name = name;
this.description = description;
2023-04-17 13:39:36 +02:00
if (exceeds1000CharLineLimit(this)) {
throw new IllegalArgumentException("The line representation of a profile MUST NOT exceed 1000 bytes.");
}
2023-04-14 14:03:22 +02:00
}
2023-04-17 13:39:36 +02:00
/**
* Return the name (also known as identifier) of the profile.
* A profile name is a UTF-8 string that has no whitespace in it.
* Similar to OpenPGP Notation names, profile names are divided into two namespaces:
* The IETF namespace and the user namespace.
* A profile name in the user namespace ends with the <pre>@</pre> character (0x40) followed by a DNS domain name.
* A profile name in the IETF namespace does not have an <pre>@</pre> character.
* A profile name in the user space is owned and controlled by the owner of the domain in the suffix.
* A profile name in the IETF namespace that begins with the string <pre>rfc</pre> should have semantics that hew as
* closely as possible to the referenced RFC.
* Similarly, a profile name in the IETF namespace that begins with the string <pre>draft-</pre> should have
* semantics that hew as closely as possible to the referenced Internet Draft.
*
* @return name
*/
2023-04-14 14:03:22 +02:00
public String getName() {
return name;
}
2023-04-17 13:39:36 +02:00
/**
* Return a free-form description of the profile.
*
* @return description
*/
2023-04-14 14:03:22 +02:00
public String getDescription() {
return description;
}
2023-04-17 13:39:36 +02:00
/**
* Convert the profile into a String for displaying.
*
* @return string
*/
2023-04-14 14:03:22 +02:00
public String toString() {
return getName() + ": " + getDescription();
}
2023-04-17 13:39:36 +02:00
/**
* Test if the string representation of the profile exceeds the limit of 1000 bytes length.
* @param profile profile
* @return <pre>true</pre> if the profile exceeds 1000 bytes, <pre>false</pre> otherwise.
*/
private static boolean exceeds1000CharLineLimit(Profile profile) {
String line = profile.toString();
2023-04-27 13:17:58 +02:00
return line.getBytes(UTF8Util.UTF8).length > 1000;
2023-04-17 13:39:36 +02:00
}
2023-04-14 14:03:22 +02:00
}