Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,19 @@ private Constants() {}
"The id of the pillar where the operation should be performed. " +
"If undefined the operation is performed on all pillars.";

public static final String REQUEST_CHECKSUM_TYPE_DESC =
"[OPTIONAL] Request the use of a specific checksum algorithm in the response from the pillars. " +
"E.g. '-" + REQUEST_CHECKSUM_TYPE_ARG + " SHA1'.";

public static final String REQUEST_CHECKSUM_SALT_DESC =
"[OPTIONAL] A salt for the requested checksum specified by the checksum type argument " +
"[-" + REQUEST_CHECKSUM_TYPE_ARG + " REQUIRED]. Must be hexadecimal string of even length. " +
"E.g. '-" + REQUEST_CHECKSUM_TYPE_ARG + " HMAC_SHA1 -" + REQUEST_CHECKSUM_SALT_ARG + " 0123abcd'";

public static final String DELETE_FILE_DESC = "If this flag is present the file will be removed from " +
"the file exchange when the chosen operation is complete.";

public static final int EXIT_SUCCESS = 0;
public static final int EXIT_ARGUMENT_FAILURE = 1;
public static final int EXIT_OPERATION_FAILURE = -1;

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ protected void createOptionsForCmdArgumentHandler() {
cmdHandler.addOption(checksumOption);

Option checksumTypeOption = new Option(Constants.REQUEST_CHECKSUM_TYPE_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] The algorithm of checksum to request in the response from the pillars.");
Constants.REQUEST_CHECKSUM_TYPE_DESC);
checksumTypeOption.setRequired(Constants.ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumTypeOption);

Option checksumSaltOption = new Option(Constants.REQUEST_CHECKSUM_SALT_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] The salt of checksum to request in the response. Requires the ChecksumType argument.");
Constants.REQUEST_CHECKSUM_SALT_DESC);
checksumSaltOption.setRequired(Constants.ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumSaltOption);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ protected void createOptionsForCmdArgumentHandler() {
cmdHandler.addOption(pillarOption);

Option checksumTypeOption = new Option(Constants.REQUEST_CHECKSUM_TYPE_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] The algorithm of checksum to request in the response from the pillars. "
+ "If no such argument is given, then the default from settings is retrieved.");
Constants.REQUEST_CHECKSUM_TYPE_DESC
+ " If no such argument is given, then the default from settings is retrieved.");
checksumTypeOption.setRequired(Constants.ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumTypeOption);

Option checksumSaltOption = new Option(Constants.REQUEST_CHECKSUM_SALT_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] The salt of checksum to request in the response. Requires the ChecksumType argument.");
Constants.REQUEST_CHECKSUM_SALT_DESC);
checksumSaltOption.setRequired(Constants.ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumSaltOption);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ protected void createOptionsForCmdArgumentHandler() {
cmdHandler.addOption(checksumOption);

Option checksumTypeOption = new Option(Constants.REQUEST_CHECKSUM_TYPE_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] Used to request a specific checksum algorithm in the response from the pillars.");
Constants.REQUEST_CHECKSUM_TYPE_DESC);
checksumTypeOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumTypeOption);

Option checksumSaltOption = new Option(Constants.REQUEST_CHECKSUM_SALT_ARG, Constants.HAS_ARGUMENT,
"[OPTIONAL] Used to request a salted checksum in the response. Requires the ChecksumType argument.");
Constants.REQUEST_CHECKSUM_SALT_DESC);
checksumSaltOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumSaltOption);

Option deleteOption = new Option(Constants.DELETE_FILE_ARG, Constants.NO_ARGUMENT,
"If this argument is present, then the file will be removed from the server, when the chosen operation is complete.");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking now we're at it: remove the second comma, the one between "server" and "when".

Option deleteOption = new Option(Constants.DELETE_FILE_ARG, Constants.NO_ARGUMENT, Constants.DELETE_FILE_DESC);
deleteOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(deleteOption);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.net.URL;

import static org.bitrepository.commandline.Constants.ARGUMENT_IS_NOT_REQUIRED;
import static org.bitrepository.commandline.Constants.ARGUMENT_IS_REQUIRED;
import static org.bitrepository.commandline.Constants.CHECKSUM_ARG;
import static org.bitrepository.commandline.Constants.DELETE_FILE_ARG;
import static org.bitrepository.commandline.Constants.EXIT_ARGUMENT_FAILURE;
Expand Down Expand Up @@ -111,18 +110,16 @@ protected void createOptionsForCmdArgumentHandler() {
cmdHandler.addOption(replaceChecksumOption);

Option checksumTypeOption = new Option(REQUEST_CHECKSUM_TYPE_ARG, HAS_ARGUMENT,
"[OPTIONAL] The algorithm of checksum to request in the response from the pillars.");
Constants.REQUEST_CHECKSUM_TYPE_DESC);
checksumTypeOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumTypeOption);

Option checksumSaltOption = new Option(REQUEST_CHECKSUM_SALT_ARG, HAS_ARGUMENT,
"[OPTIONAL] The salt of checksum to request in the response. " +
"Requires the ChecksumType argument.");
Constants.REQUEST_CHECKSUM_SALT_DESC);
checksumSaltOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(checksumSaltOption);

Option deleteOption = new Option(DELETE_FILE_ARG, NO_ARGUMENT,
"If this argument is present, then the file will be removed from the server, when the operation is complete.");
Option deleteOption = new Option(DELETE_FILE_ARG, NO_ARGUMENT, Constants.DELETE_FILE_DESC);
deleteOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(deleteOption);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package org.bitrepository.common.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.bitrepository.common.ArgumentValidator;

/**
Expand All @@ -40,35 +42,23 @@ public static String decodeBase16(byte[] data) {
if (data == null) {
return null;
}

StringBuilder sb = new StringBuilder(data.length * 2);
for (byte datum : data) {
int v = datum & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString();
// TODO Java 17 has HexFormat.of().formatHex(bytes) - consider using instead
return Hex.encodeHexString(data);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to below one may consider adding a comment along the lines of TODO Java 17 has HexFormat.of().formatHex(bytes); - consider using instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually did write this, but somehow I deleted it again. Will add. :)

}

/**
* Encoding a hex string to base16.
* Encodes a hex string to a set of base16 bytes.
*
* @param hexString The string to encode to base16.
* @return The string encoded to base16.
*/
public static byte[] encodeBase16(String hexString) {
ArgumentValidator.checkNotNullOrEmpty(hexString, "String hexString");
ArgumentValidator.checkTrue((hexString.length() % 2) == 0, "String hexString, '" + hexString
+ "', must be an even number of characters.");

int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
// TODO Java 17 has HexFormat.of().parseHex(s) - consider using instead
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this comment.

try {
return Hex.decodeHex(hexString);
} catch (DecoderException e) {
throw new IllegalArgumentException("Bad hex-string '" + hexString + "': " + e.getMessage());
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private ChecksumSpecTYPE getChecksumSpecWithRandomSalt() {
break;
}

String salt = UUID.randomUUID().toString();
String salt = UUID.randomUUID().toString().replace("-", "");
res.setChecksumSalt(Base16Utils.encodeBase16(salt));

return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void testSuccess() throws Exception {
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null));
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testOneComponentFailureAndTwoOtherAgreeOnChecksum() throws Exception
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
ContributorEvent e1 = new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e2 = new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e3 = new ContributorFailedEvent(PILLAR_3, TEST_COLLECTION, ResponseCode.FAILURE);
Expand Down Expand Up @@ -207,8 +207,8 @@ public void testOneComponentFailureAndTwoOtherDisagreeOnChecksum() throws Except
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res1 = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res2 = createResultingChecksums((String) invocation.getArguments()[3], "muskcehc");
ResultingChecksums res1 = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
ResultingChecksums res2 = createResultingChecksums((String) invocation.getArguments()[3], "fedcba");
ContributorEvent e1 = new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, res1, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e2 = new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, res2, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e3 = new ContributorFailedEvent(PILLAR_3, TEST_COLLECTION, ResponseCode.FAILURE);
Expand Down Expand Up @@ -251,8 +251,8 @@ public void testInconsistentChecksums() throws Exception {
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res1 = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res2 = createResultingChecksums((String) invocation.getArguments()[3], "muskcehc");
ResultingChecksums res1 = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
ResultingChecksums res2 = createResultingChecksums((String) invocation.getArguments()[3], "fedcba");
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, res1, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, res2, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testFullData() throws Exception {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];

ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_3, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
Expand Down Expand Up @@ -149,7 +149,7 @@ public void testComponentFailure() throws Exception {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];

ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "abcdef");
ContributorEvent e1 = new ChecksumsCompletePillarEvent(TEST_PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e2 = new ChecksumsCompletePillarEvent(TEST_PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e3 = new ContributorFailedEvent(TEST_PILLAR_3, TEST_COLLECTION, ResponseCode.REQUEST_NOT_UNDERSTOOD_FAILURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void getFileWithFilePartTest() throws IOException {
localFileIS.skip(offsetAndLength);
localFileIS.read(localFilePartContent, 0, offsetAndLength);
String getFileContent = IOUtils.toString(getFileIS, StandardCharsets.UTF_8);
assertEquals(getFileContent, new String(localFilePartContent),
assertEquals(getFileContent, new String(localFilePartContent, StandardCharsets.UTF_8),
"Differing content between original file and file from GetFileRequest");
}
}
Expand Down