Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.bitrepository.audittrails.preserver;

import org.apache.commons.codec.DecoderException;
import org.bitrepository.common.TimerTaskSchedule;
import org.bitrepository.audittrails.store.AuditTrailStore;
import org.bitrepository.audittrails.webservice.PreservationInfo;
Expand Down Expand Up @@ -185,13 +186,15 @@ private synchronized void performAuditTrailPreservation(String collectionID) {
throw new CoordinationLayerException("Cannot perform the preservation of audit trails.", e);
} catch (OperationFailedException e) {
throw new CoordinationLayerException("Failed to put the packed audit trails.", e);
} catch (DecoderException e) {
throw new CoordinationLayerException("Failed to encode the checksum.", e);
}
}

/**
* Helper method to make a checksum for the PutFile call.
*/
private ChecksumDataForFileTYPE getValidationChecksumDataForFile(File file) {
private ChecksumDataForFileTYPE getValidationChecksumDataForFile(File file) throws DecoderException {
ChecksumSpecTYPE csSpec = ChecksumUtils.getDefault(settings);
String checksum = ChecksumUtils.generateChecksum(file, csSpec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.apache.commons.codec.DecoderException;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.commandline.output.DefaultOutputHandler;
Expand All @@ -48,16 +49,17 @@
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;

/**
* Defines the common functionality for command-line-clients.
*/
public abstract class CommandLineClient {
private final String componentID;
protected final OutputHandler output = new DefaultOutputHandler(getClass());
protected final Settings settings;
protected final SecurityManager securityManager;
protected final CommandLineArgumentsHandler cmdHandler;
protected OutputHandler output = new DefaultOutputHandler(getClass());
protected Settings settings;
protected SecurityManager securityManager;
protected CommandLineArgumentsHandler cmdHandler;
private final FileIDValidator fileIDValidator;

/**
Expand Down Expand Up @@ -132,7 +134,7 @@ protected String getComponentID() {
protected abstract boolean isFileIDArgumentRequired();

/**
* Creates the options for the command line argument handler. May be override.
* Creates the options for the command line argument handler. May be overridden.
*/
protected void createOptionsForCmdArgumentHandler() {
cmdHandler.createDefaultOptions();
Expand All @@ -159,15 +161,17 @@ protected void validateArguments() {
String collectionArgument = cmdHandler.getOptionValue(Constants.COLLECTION_ID_ARG);
if (!collections.contains(collectionArgument)) {
throw new IllegalArgumentException(
collectionArgument + " is not a valid collection." + "\nThe following collections are defined: " + collections);
String.format(Locale.ROOT, "'%s' is not a valid collection.\nValid collections are:\n%s",
collectionArgument, collections));
}
}
if (cmdHandler.hasOption(Constants.PILLAR_ARG)) {
String pillarArgument = cmdHandler.getOptionValue(Constants.PILLAR_ARG);
List<String> pillarsInCollection = SettingsUtils.getPillarIDsForCollection(getCollectionID());
if (!pillarsInCollection.contains(pillarArgument)) {
throw new IllegalArgumentException(pillarArgument + " is not a valid pillar for collection " + getCollectionID() +
"\nThe collection contains the following pillars: " + pillarsInCollection);
throw new IllegalArgumentException(String.format(Locale.ROOT,
"'%s' is not a valid pillar for collection '%s'\nAvailable pillars for the collection are:\n%s", pillarArgument,
getCollectionID(), pillarsInCollection));
}
}
}
Expand Down Expand Up @@ -240,17 +244,16 @@ protected ChecksumSpecTYPE getRequestChecksumSpecOrNull() {
*/
private ChecksumSpecTYPE getRequestChecksumSpec() {
ChecksumSpecTYPE res = new ChecksumSpecTYPE();
res.setChecksumType(ChecksumExtractionUtils.extractChecksumType(cmdHandler, settings, output));

if (cmdHandler.hasOption(Constants.REQUEST_CHECKSUM_SALT_ARG)) {
res.setChecksumSalt(Base16Utils.encodeBase16(cmdHandler.getOptionValue(Constants.REQUEST_CHECKSUM_SALT_ARG)));
}

try {
res.setChecksumType(ChecksumExtractionUtils.extractChecksumType(cmdHandler, settings, output));

if (cmdHandler.hasOption(Constants.REQUEST_CHECKSUM_SALT_ARG)) {
res.setChecksumSalt(Base16Utils.encodeBase16(cmdHandler.getOptionValue(Constants.REQUEST_CHECKSUM_SALT_ARG)));
}
ChecksumUtils.verifyAlgorithm(res);
} catch (NoSuchAlgorithmException e) {
output.error("Invalid checksum algorithm: " + e.getMessage());
throw new IllegalStateException("Invalid checksumSpec for '" + res + "'", e);
} catch (NoSuchAlgorithmException | DecoderException | IllegalStateException e) {
output.error(e.getMessage());
System.exit(1);
}

return res;
Expand Down Expand Up @@ -288,8 +291,13 @@ protected ChecksumDataForFileTYPE getChecksumDataForDeleteValidation() {

ChecksumDataForFileTYPE res = new ChecksumDataForFileTYPE();
res.setCalculationTimestamp(CalendarUtils.getNow());
try {
res.setChecksumValue(Base16Utils.encodeBase16(cmdHandler.getOptionValue(Constants.CHECKSUM_ARG)));
} catch (DecoderException e) {
output.error(e.getMessage());
System.exit(1);
}
res.setChecksumSpec(ChecksumUtils.getDefault(settings));
res.setChecksumValue(Base16Utils.encodeBase16(cmdHandler.getOptionValue(Constants.CHECKSUM_ARG)));

return res;
}
Expand All @@ -304,8 +312,7 @@ protected void deleteFileAfterwards(URL url) {
FileExchange fileexchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
fileexchange.deleteFile(url);
} catch (Exception e) {
System.err.println("Issue regarding removing file from server: " + e.getMessage());
e.printStackTrace();
output.error("Issue regarding removing file from server: " + e.getMessage());
}
}

Expand Down Expand Up @@ -351,12 +358,18 @@ protected long getSizeOfFileOrZero() {
*/
protected ChecksumDataForFileTYPE getValidationChecksumDataForFile(File file) {
ChecksumSpecTYPE csSpec = ChecksumUtils.getDefault(settings);

String checksum = ChecksumUtils.generateChecksum(file, csSpec);

ChecksumDataForFileTYPE res = new ChecksumDataForFileTYPE();
res.setCalculationTimestamp(CalendarUtils.getNow());
res.setChecksumSpec(csSpec);
res.setChecksumValue(Base16Utils.encodeBase16(checksum));
try {
res.setChecksumValue(Base16Utils.encodeBase16(checksum));
} catch (DecoderException e) {
output.error(e.getMessage());
System.exit(1);
}

return res;
}
Expand All @@ -378,7 +391,12 @@ protected ChecksumDataForFileTYPE getValidationChecksumDataFromArgument(String a
ChecksumDataForFileTYPE res = new ChecksumDataForFileTYPE();
res.setCalculationTimestamp(CalendarUtils.getNow());
res.setChecksumSpec(csSpec);
res.setChecksumValue(Base16Utils.encodeBase16(cmdHandler.getOptionValue(arg)));
try {
res.setChecksumValue(Base16Utils.encodeBase16(cmdHandler.getOptionValue(arg)));
} catch (DecoderException e) {
System.exit(1);
}


return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.ChecksumUtils;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Utility class for extraction of checksum parameters from the commandline arguments.
Expand All @@ -44,13 +48,13 @@ public class ChecksumExtractionUtils {
* @param output The OutputHandler, where output and logging information is delivered.
* @return The checksum type.
*/
public static ChecksumType extractChecksumType(CommandLineArgumentsHandler cmdHandler, Settings settings,
OutputHandler output) {
public static ChecksumType extractChecksumType(CommandLineArgumentsHandler cmdHandler, Settings settings, OutputHandler output)
throws NoSuchAlgorithmException {
String type;
if (cmdHandler.hasOption(Constants.REQUEST_CHECKSUM_TYPE_ARG)) {
type = cmdHandler.getOptionValue(Constants.REQUEST_CHECKSUM_TYPE_ARG).toUpperCase(Locale.ROOT);
} else {
type = ChecksumUtils.getDefault(settings).getChecksumType().name();
type = ChecksumUtils.getDefault(settings).getChecksumType().name().toUpperCase(Locale.ROOT);
}

if (cmdHandler.hasOption(Constants.REQUEST_CHECKSUM_SALT_ARG)) {
Expand All @@ -65,6 +69,14 @@ public static ChecksumType extractChecksumType(CommandLineArgumentsHandler cmdHa
}
}

return ChecksumType.fromValue(type);
List<String> availableChecksums = Stream.of(ChecksumType.values()).filter(c -> !c.equals(ChecksumType.OTHER))
.map(ChecksumType::value).collect(Collectors.toList());

if (availableChecksums.contains(type)) {
return ChecksumType.fromValue(type);
} else {
throw new NoSuchAlgorithmException(
"The algorithm '" + type + "' is not valid.\nValid checksum algorithms are:\n" + availableChecksums);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.bitrepository.client.conversation.ConversationContext;
import org.bitrepository.client.conversation.PerformingOperationState;
import org.bitrepository.client.conversation.selector.SelectedComponentInfo;
import org.bitrepository.client.exceptions.UnexpectedResponseException;
import org.bitrepository.common.utils.ChecksumUtils;

import java.util.Collection;
Expand All @@ -57,7 +56,7 @@ public ReplacingFile(ReplaceFileConversationContext context, Collection<Selected
}

@Override
protected void generateContributorCompleteEvent(MessageResponse msg) throws UnexpectedResponseException {
protected void generateContributorCompleteEvent(MessageResponse msg) {
ReplaceFileFinalResponse response = (ReplaceFileFinalResponse) msg;
getContext().getMonitor().contributorComplete(new ReplaceFileCompletePillarEvent(
response.getPillarID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public static String decodeBase16(byte[] data) {
* @param hexString The string to encode to base16.
* @return The string encoded to base16.
*/
public static byte[] encodeBase16(String hexString) {
public static byte[] encodeBase16(String hexString) throws DecoderException {
ArgumentValidator.checkNotNullOrEmpty(hexString, "String hexString");
// TODO Java 17 has HexFormat.of().parseHex(s) - consider using instead
try {
return Hex.decodeHex(hexString);
} catch (DecoderException e) {
throw new IllegalArgumentException("Bad hex-string '" + hexString + "': " + e.getMessage());
throw new DecoderException("The string '" + hexString + "' is not a valid hex-string.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ public static boolean requiresSalt(ChecksumType algorithm) throws NoSuchAlgorith
*/
public static ChecksumSpecTYPE getDefault(Settings settings) {
ChecksumSpecTYPE res = new ChecksumSpecTYPE();
res.setChecksumType(ChecksumType.valueOf(
settings.getRepositorySettings().getProtocolSettings().getDefaultChecksumType()));
res.setChecksumType(ChecksumType.valueOf(settings.getRepositorySettings().getProtocolSettings().getDefaultChecksumType()));

try {
verifyAlgorithm(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.bitrepository.common.utils;

import org.apache.commons.codec.DecoderException;
import org.jaccept.structure.ExtendedTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -48,31 +49,20 @@ public void encodeChecksum() throws Exception {
}

@Test(groups = { "regressiontest" })
public void decodeChecksum() throws Exception {
public void decodeChecksum() {
addDescription("Validating the decoding of the checksums.");
addStep("Decode the checksum and validate.", "It should match the precalculated constant.");
String decodedChecksum = Base16Utils.decodeBase16(ENCODED_CHECKSUM);
Assert.assertEquals(decodedChecksum, DECODED_CHECKSUM);
}

@Test(groups = { "regressiontest" })
public void badArgumentTest() throws Exception {
public void badArgumentTest() {
addDescription("Test bad arguments");
Assert.assertNull(Base16Utils.decodeBase16(null));

try {
Base16Utils.encodeBase16(null);
Assert.fail("Should throw an exception");
} catch (IllegalArgumentException e) {
// expected
}

addStep("Test with a odd number of characters.", "Should throw an exception");
try {
Base16Utils.encodeBase16("123");
Assert.fail("Should throw an exception");
} catch (IllegalArgumentException e) {
// expected
}
Assert.assertThrows(IllegalArgumentException.class, () -> Base16Utils.encodeBase16(null));

addStep("Test with an odd number of characters.", "Should throw a decoder exception");
Assert.assertThrows(DecoderException.class, () -> Base16Utils.encodeBase16("123"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
* Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.bitrepository.common.utils;

import org.apache.commons.codec.DecoderException;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumType;
Expand Down Expand Up @@ -48,8 +49,12 @@ public static ChecksumDataForFileTYPE getDefaultFileChecksum() {
ChecksumSpecTYPE checksumSpecTYPE = new ChecksumSpecTYPE();
checksumSpecTYPE.setChecksumType(ChecksumType.MD5);
checksumData.setChecksumSpec(checksumSpecTYPE);
checksumData.setChecksumValue(Base16Utils.encodeBase16(
ChecksumUtils.generateChecksum(getDefaultFile(), checksumSpecTYPE)));
try {
checksumData.setChecksumValue(Base16Utils.encodeBase16(ChecksumUtils.generateChecksum(getDefaultFile(), checksumSpecTYPE)));
} catch (DecoderException e) {
System.err.println(e.getMessage());
}

return checksumData;
}

Expand All @@ -58,7 +63,7 @@ public static long getFileSize(File file) {
}

public static InputStream getFile(String name) {
String fullName = TEST_RESOURCES_PATH+name;
String fullName = TEST_RESOURCES_PATH + name;
InputStream fileStream = TestFileHelper.class.getClassLoader().getResourceAsStream(fullName);
assert (fileStream != null) : "Unable to find " + fullName + " in classpath";
return fileStream;
Expand All @@ -71,20 +76,20 @@ public static String createUniquePrefix(String testName) {
public static String[] createFileIDs(int numberToCreate, String testName) {
String uniquePrefix = createUniquePrefix(testName);
String[] fileIDs = new String[numberToCreate];
for (int i = 0 ; i<numberToCreate ; i++) {
fileIDs[i] = uniquePrefix + "-" + (i+1) +".txt";
for (int i = 0; i < numberToCreate; i++) {
fileIDs[i] = uniquePrefix + "-" + (i + 1) + ".txt";
}
return fileIDs;
}

public static List<File> getAllFilesFromSubDirs(File dir) {
List<File> res = new ArrayList<>();
if(dir.isDirectory()) {
for(File f : dir.listFiles()) {
if(f.isFile()) {
if (dir.isDirectory()) {
for (File f : dir.listFiles()) {
if (f.isFile()) {
res.add(f);
} else {
for(File sf : getAllFilesFromSubDirs(f)) {
for (File sf : getAllFilesFromSubDirs(f)) {
res.add(sf);
}
}
Expand Down
Loading