Skip to content

Commit

Permalink
issue_844: Made updates using the checkstyle configuration to various…
Browse files Browse the repository at this point in the history
… files. Have also made some config changes to the checkstyle.xml file.
  • Loading branch information
ThatSilentCoder committed Oct 10, 2024
1 parent 2dcdc15 commit b80d722
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 412 deletions.
22 changes: 9 additions & 13 deletions HIRS_Utils/src/main/java/hirs/utils/BannerConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,35 @@
public class BannerConfiguration {

private static final Path BANNER_PROPERTIES_PATH = FileSystems.getDefault()
.getPath("/opt/tomcat/webapps/HIRS_AttestationCAPortal", "WEB-INF", "classes", "banner.properties");
.getPath("/opt/tomcat/webapps/HIRS_AttestationCAPortal",
"WEB-INF", "classes", "banner.properties");

private static final String BANNER_COLOR = "banner.color";
private static final String BANNER_STRING = "banner.string";
private static final String BANNER_DYNAMIC = "banner.dynamic";
private static final String LEFT_CONTENT = "left.content";
private static final String RIGHT_CONTENT = "right.content";

private final ArrayList<String> leftContent = new ArrayList<>();
private final ArrayList<String> rightContent = new ArrayList<>();
@Getter
private String bannerColor = "";
@Getter
private String bannerString = "";
@Getter
private String bannerDynamic = "";

private final ArrayList<String> leftContent = new ArrayList<>();
private final ArrayList<String> rightContent = new ArrayList<>();

/**
* Banner Configuration default constructor.
* Verify if the file exist, if it does it will get all the
* properties values and save them on the class.
*
* @throws IOException the banner level for the web site.
* @throws IOException the banner level for the website.
*/
public BannerConfiguration() throws IOException {
if (!Files.exists(BANNER_PROPERTIES_PATH)) {
log.info(String.format(
"No file found at %s. Banner will not display.",
BANNER_PROPERTIES_PATH.toString()
BANNER_PROPERTIES_PATH
));
return;
}
Expand All @@ -69,8 +68,8 @@ public BannerConfiguration() throws IOException {
/**
* This method applies any dynamically configuration found in the properties file,
* if it exists.
* @param bannerProps
* @return the banner level for the web site.
*
* @param bannerProps banner props
*/
private void setBannerProperties(final Properties bannerProps) {

Expand Down Expand Up @@ -103,10 +102,7 @@ private void setBannerProperties(final Properties bannerProps) {
* @return if the banner was set.
*/
public Boolean getHasBanner() {
if (!bannerColor.isEmpty() || !bannerString.isEmpty()) {
return true;
}
return false;
return !bannerColor.isEmpty() || !bannerString.isEmpty();
}

/**
Expand Down
42 changes: 25 additions & 17 deletions HIRS_Utils/src/main/java/hirs/utils/HexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
*/
public final class HexUtils {

/**
* Default private constructor so checkstyles doesn't complain
*/
private HexUtils() { }
/**
* The mathematical base for the hexadecimal representation.
*/
public static final int HEX_BASIS = 16;

/**
* An integer representation of the byte 0xff or 255.
*/
public static final int FF_BYTE = 0xff;

/**
* Default private constructor so checkstyles doesn't complain.
*/
private HexUtils() {
}

/**
* Converts a binary hex string to a byte array.
*
* @param s string to convert
* @return byte array representation of s
*/
Expand All @@ -40,6 +42,7 @@ public static byte[] hexStringToByteArray(final String s) {

/**
* Converts a byte array to a hex represented binary string.
*
* @param b byte array to convert
* @return hex string representation of array
*/
Expand All @@ -58,6 +61,7 @@ public static String byteArrayToHexString(final byte[] b) {

/**
* Converts an individual hex string to an integer.
*
* @param s an individual hex string
* @return an integer representation of a hex string
*/
Expand All @@ -68,9 +72,10 @@ public static Integer hexToInt(final String s) {

/**
* Takes a byte array returns a subset of the array.
* @param b the array to take a subset of
*
* @param b the array to take a subset of
* @param start the first index to copy
* @param end the last index to copy (inclusive)
* @param end the last index to copy (inclusive)
* @return a new array of bytes from start to end
*/
public static byte[] subarray(final byte[] b, final int start, final int end) {
Expand All @@ -81,20 +86,22 @@ public static byte[] subarray(final byte[] b, final int start, final int end) {

/**
* Takes in a byte array and reverses the order.
* @param in byte array to reverse
*
* @param in byte array to reverse
* @return reversed byte array
*/
public static byte[] leReverseByte(final byte[] in) {
public static byte[] leReverseByte(final byte[] in) {
byte[] finished = new byte[in.length];
for (int i = 0; i < finished.length; i++) {
finished[i] = in[(in.length - 1) - i];
}
return finished;
for (int i = 0; i < finished.length; i++) {
finished[i] = in[(in.length - 1) - i];
}
return finished;
}

/**
* Takes in a byte array and reverses the order then converts to an int.
* @param in byte array to reverse
*
* @param in byte array to reverse
* @return integer that represents the reversed byte array
*/
public static int leReverseInt(final byte[] in) {
Expand All @@ -104,12 +111,13 @@ public static int leReverseInt(final byte[] in) {

/**
* Takes in a byte array of 4 bytes and returns a long.
* @param bytes byte array to convert
*
* @param bytes byte array to convert
* @return long representation of the bytes
*/
public static long bytesToLong(final byte[] bytes) {
BigInteger lValue = new BigInteger(bytes);
BigInteger lValue = new BigInteger(bytes);

return lValue.abs().longValue();
return lValue.abs().longValue();
}
}
23 changes: 12 additions & 11 deletions HIRS_Utils/src/main/java/hirs/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
public final class JsonUtils {

/**
* Default private constructor so checkstyles doesn't complain
* Default private constructor so checkstyles doesn't complain.
*/
private JsonUtils() { }
private JsonUtils() {
}

/**
* Getter for the JSON Object that is associated with the elementName value
* mapped in the associated JSON file.
* Default {@link java.nio.charset.Charset} is UTF 8
*
* @param jsonPath the object holding the location of the file to parse.
* @param jsonPath the object holding the location of the file to parse.
* @param elementName the specific object to pull from the file
* @return a JSON object
*/
Expand All @@ -44,9 +45,9 @@ public static JsonObject getSpecificJsonObject(final Path jsonPath, final String
* mapped in the associated JSON file.
* Default {@link java.nio.charset.Charset} is UTF 8
*
* @param jsonPath the object holding the location of the file to parse.
* @param jsonPath the object holding the location of the file to parse.
* @param elementName the specific object to pull from the file
* @param charset the character set to use
* @param charset the character set to use
* @return a JSON object
*/
public static JsonObject getSpecificJsonObject(final Path jsonPath,
Expand Down Expand Up @@ -77,15 +78,15 @@ public static JsonObject getJsonObject(final Path jsonPath) {
* Getter for the JSON Object that is mapped in the associated JSON file.
*
* @param jsonPath the object holding the location of the file to parse.
* @param charset the character set to use
* @param charset the character set to use
* @return a JSON object
*/
public static JsonObject getJsonObject(final Path jsonPath, final Charset charset) {
// find the file and load it
JsonObject jsonObject = new JsonObject();

if (Files.notExists(jsonPath)) {
log.warn(String.format("No file found at %s.", jsonPath.toString()));
log.warn("No file found at {}.", jsonPath.toString());
} else {
try {
InputStream inputStream = new FileInputStream(jsonPath.toString());
Expand All @@ -106,7 +107,7 @@ public static JsonObject getJsonObject(final Path jsonPath, final Charset charse
* Default {@link java.nio.charset.Charset} is UTF 8
*
* @param jsonFilename the object holding the name of the file in classpath to parse.
* @param elementName the specific object to pull from the file
* @param elementName the specific object to pull from the file
* @return a JSON object
*/
public static JsonObject getSpecificJsonObject(final String jsonFilename, final String elementName) {
Expand All @@ -120,8 +121,8 @@ public static JsonObject getSpecificJsonObject(final String jsonFilename, final
* Default {@link java.nio.charset.Charset} is UTF 8
*
* @param jsonFilename the object holding the name of the file in classpath to parse.
* @param elementName the specific object to pull from the file
* @param charset the character set to use
* @param elementName the specific object to pull from the file
* @param charset the character set to use
* @return a JSON object
*/
public static JsonObject getSpecificJsonObject(final String jsonFilename,
Expand Down Expand Up @@ -152,7 +153,7 @@ public static JsonObject getJsonObject(final String jsonFilename) {
* Getter for the JSON Object that is mapped in the associated JSON file.
*
* @param jsonFilename the object holding the name of the file in classpath to parse.
* @param charset the character set to use
* @param charset the character set to use
* @return a JSON object
*/
public static JsonObject getJsonObject(final String jsonFilename, final Charset charset) {
Expand Down
62 changes: 41 additions & 21 deletions HIRS_Utils/src/main/java/hirs/utils/PciIds.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,24 @@
@Log4j2
public final class PciIds {

/**
* Default private constructor so checkstyles doesn't complain
*/
private PciIds() { }

/**
* This pci ids file can be in different places on different distributions.
*/
public static final List<String> PCI_IDS_PATH =
Collections.unmodifiableList(new ArrayList<>() {
private static final long serialVersionUID = 1L;

{
add("/usr/share/hwdata/pci.ids");
add("/usr/share/misc/pci.ids");
add("/tmp/pci.ids");
}
});

/**
* The PCI IDs Database object.
*
* <p>
* This only needs to be loaded one time.
*
* <p>
* The pci ids library protects the data inside the object by making it immutable.
*/
public static final PciIdsDatabase DB = new PciIdsDatabase();
Expand All @@ -65,7 +60,7 @@ private PciIds() { }
if (dbFile != null) {
InputStream is = null;
try {
is = new FileInputStream(new File(dbFile));
is = new FileInputStream(dbFile);
DB.loadStream(is);
} catch (IOException e) {
// DB will not be ready, hardware IDs will not be translated
Expand All @@ -83,9 +78,16 @@ private PciIds() { }
}
}

/**
* Default private constructor so checkstyles doesn't complain.
*/
private PciIds() {
}

/**
* Look up the vendor name from the PCI IDs list, if the input string contains an ID.
* If any part of this fails, return the original manufacturer value.
*
* @param refManufacturer DERUTF8String, likely from a ComponentIdentifier
* @return DERUTF8String with the discovered vendor name, or the original manufacturer value.
*/
Expand All @@ -103,6 +105,7 @@ public static ASN1UTF8String translateVendor(final ASN1UTF8String refManufacture
/**
* Look up the vendor name from the PCI IDs list, if the input string contains an ID.
* If any part of this fails, return the original manufacturer value.
*
* @param refManufacturer String, likely from a ComponentResult
* @return String with the discovered vendor name, or the original manufacturer value.
*/
Expand All @@ -121,8 +124,9 @@ public static String translateVendor(final String refManufacturer) {
* Look up the device name from the PCI IDs list, if the input strings contain IDs.
* The Device lookup requires the Vendor ID AND the Device ID to be valid values.
* If any part of this fails, return the original model value.
*
* @param refManufacturer ASN1UTF8String, likely from a ComponentIdentifier
* @param refModel ASN1UTF8String, likely from a ComponentIdentifier
* @param refModel ASN1UTF8String, likely from a ComponentIdentifier
* @return ASN1UTF8String with the discovered device name, or the original model value.
*/
public static ASN1UTF8String translateDevice(final ASN1UTF8String refManufacturer,
Expand All @@ -146,8 +150,9 @@ public static ASN1UTF8String translateDevice(final ASN1UTF8String refManufacture
* Look up the device name from the PCI IDs list, if the input strings contain IDs.
* The Device lookup requires the Vendor ID AND the Device ID to be valid values.
* If any part of this fails, return the original model value.
*
* @param refManufacturer String, likely from a ComponentResult
* @param refModel String, likely from a ComponentResult
* @param refModel String, likely from a ComponentResult
* @return String with the discovered device name, or the original model value.
*/
public static String translateDevice(final String refManufacturer,
Expand All @@ -169,24 +174,39 @@ public static String translateDevice(final String refManufacturer,
/**
* Look up the device class name from the PCI IDs list, if the input string contains an ID.
* If any part of this fails, return the original manufacturer value.
*
* @param refClassCode String, formatted as 2 characters (1 byte) for each of the 3 categories
* Example "010802":
* Class: "01"
* Subclass: "08"
* Programming Interface: "02"
* Example "010802":
* Class: "01"
* Subclass: "08"
* Programming Interface: "02"
* @return List<String> 3-element list with the class code
* 1st element: human-readable description of Class
* 2nd element: human-readable description of Subclass
* 3rd element: human-readable description of Programming Interface
* 1st element: human-readable description of Class
* 2nd element: human-readable description of Subclass
* 3rd element: human-readable description of Programming Interface
*/
public static List<String> translateDeviceClass(final String refClassCode) {
List<String> translatedClassCode = new ArrayList<>();

String classCode = refClassCode;
if (classCode != null && classCode.trim().matches("^[0-9A-Fa-f]{6}$")) {
String deviceClass = classCode.substring(0, 2).toLowerCase();
String deviceSubclass = classCode.substring(2, 4).toLowerCase();
String programInterface = classCode.substring(4, 6).toLowerCase();
final int startIndexOfDeviceClass = 0;
final int endIndexOfDeviceClass = 2;
String deviceClass =
classCode.substring(startIndexOfDeviceClass, endIndexOfDeviceClass).toLowerCase();

final int startIndexOfDeviceSubclass = 2;
final int endIndexOfDeviceSubclass = 4;
String deviceSubclass =
classCode.substring(startIndexOfDeviceSubclass, endIndexOfDeviceSubclass)
.toLowerCase();

final int startIndexOfProgramInterface = 4;
final int endIndexOfProgramInterface = 6;
final String programInterface =
classCode.substring(startIndexOfProgramInterface, endIndexOfProgramInterface)
.toLowerCase();

translatedClassCode.add(deviceClass);
translatedClassCode.add(deviceSubclass);
translatedClassCode.add(programInterface);
Expand Down
Loading

0 comments on commit b80d722

Please sign in to comment.