Skip to content

Commit

Permalink
Got the project compiling with java 11 and removed a bunch of warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohlski committed Aug 5, 2021
1 parent 6f46b8b commit d8ae8ce
Show file tree
Hide file tree
Showing 34 changed files with 238 additions and 179 deletions.
Expand Up @@ -77,7 +77,7 @@ public void alarmExceptionTest() throws Exception {
Assert.assertEquals(model.getFileID(), defaultFileID);

addStep("Test the MaxCount", "Should be able to put a new one in and extract it again.");
Integer defaultMaxCount = new Integer(192837456);
Integer defaultMaxCount = 192837456;
model.setMaxCount(defaultMaxCount);
Assert.assertEquals(model.getMaxCount(), defaultMaxCount);

Expand Down
6 changes: 3 additions & 3 deletions bitrepository-client/README.md
Expand Up @@ -156,12 +156,12 @@ import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.BasicMessageAuthenticator;
import org.bitrepository.protocol.security.BasicMessageSigner;
import org.bitrepository.protocol.security.BasicOperationAuthorizor;
import org.bitrepository.protocol.security.BasicOperationAuthorizer;
import org.bitrepository.protocol.security.SecurityManager;
import org.bitrepository.protocol.security.BasicSecurityManager;
import org.bitrepository.protocol.security.MessageAuthenticator;
import org.bitrepository.protocol.security.MessageSigner;
import org.bitrepository.protocol.security.OperationAuthorizor;
import org.bitrepository.protocol.security.OperationAuthorizer;
import org.bitrepository.protocol.security.PermissionStore;

public class BitrepositoryClientExample {
Expand Down Expand Up @@ -226,7 +226,7 @@ public class BitrepositoryClientExample {
PermissionStore permissionStore = new PermissionStore();
MessageAuthenticator authenticator = new BasicMessageAuthenticator(permissionStore);
MessageSigner signer = new BasicMessageSigner();
OperationAuthorizor authorizer = new BasicOperationAuthorizor(permissionStore);
OperationAuthorizer authorizer = new BasicOperationAuthorizer(permissionStore);
SecurityManager securityManager = new BasicSecurityManager(
settings.getRepositorySettings(), certificateFile, authenticator,
signer, authorizer, permissionStore, settings.getComponentID());
Expand Down
Expand Up @@ -52,7 +52,7 @@ private ComponentIDFactory createClientIDFactory(String clientIDFactoryClass) {
return new DefaultCommandlineComponentID();
} else {
try {
return (ComponentIDFactory)Class.forName(clientIDFactoryClass).newInstance();
return (ComponentIDFactory)Class.forName(clientIDFactoryClass).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Unable to instantiate ClientIDFactory " +
clientIDFactoryClass +
Expand Down
Expand Up @@ -49,7 +49,7 @@ public IdentifyPillarsForPutFile(PutFileConversationContext context) {
}

/**
* Extends the default behaviour with a idempotent aspects. This assumes that the put to a pillar is successful if
* Extends the default behaviour with an idempotent aspect. This assumes that the put to a pillar is successful if
* the same file already exists.
*
* The existence of a different file on the other hand is a fatal problem.
Expand Down
Expand Up @@ -72,9 +72,9 @@ public XMLFileSettingsLoader(String pathToSettingsFiles) {
public <T> T loadSettings(Class<T> settingsClass) {
StringBuilder fileLocationBuilder = new StringBuilder();
if (pathToSettingsFiles != null && !pathToSettingsFiles.equals("")) {
fileLocationBuilder.append(pathToSettingsFiles + DIRECTORY_SEPERATOR);
fileLocationBuilder.append(pathToSettingsFiles).append(DIRECTORY_SEPERATOR);
}
fileLocationBuilder.append(settingsClass.getSimpleName() + XML_FILE_EXTENSION);
fileLocationBuilder.append(settingsClass.getSimpleName()).append(XML_FILE_EXTENSION);
String fileLocation = fileLocationBuilder.toString();
String schemaLocation = settingsClass.getSimpleName() + XSD_FILE_EXTENSION;
JaxbHelper jaxbHelper = new JaxbHelper(XSD_SCHEMA_DIR, schemaLocation);
Expand All @@ -94,7 +94,7 @@ public <T> T loadSettings(Class<T> settingsClass) {
log.debug("Loading the settings file '" + fileLocation + "'.");
try {
jaxbHelper.validate(configStreamValidate);
return (T) jaxbHelper.loadXml(settingsClass, configStreamLoad);
return jaxbHelper.loadXml(settingsClass, configStreamLoad);
} catch (SAXException e) {
throw new RuntimeException("Unable to validate settings from " +
Thread.currentThread().getContextClassLoader().getResource(fileLocation), e);
Expand Down
Expand Up @@ -59,7 +59,8 @@ private ReceiverDestinationIDFactory createReceiverDestinationIDFactory(
return new DefaultReceiverDestinationIDFactory();
} else {
try {
return (ReceiverDestinationIDFactory)Class.forName(receiverDestinationIDFactoryClass).newInstance();
return (ReceiverDestinationIDFactory) Class.forName(receiverDestinationIDFactoryClass)
.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Unable to instantiate ReceiverDestinationIDFactory " +
receiverDestinationIDFactoryClass +
Expand Down
Expand Up @@ -56,7 +56,7 @@ public BasicMessageAuthenticator(PermissionStore permissionStore) {
public SignerId authenticateMessage(byte[] messageData, byte[] signatureData) throws MessageAuthenticationException {
try {
CMSSignedData s = new CMSSignedData(new CMSProcessableByteArray(messageData), signatureData);
SignerInformation signer = (SignerInformation) s.getSignerInfos().getSigners().iterator().next();
SignerInformation signer = s.getSignerInfos().getSigners().iterator().next();
X509Certificate signingCert = permissionStore.getCertificate(signer.getSID());
SignerInformationVerifier verifier = new JcaSimpleSignerInfoVerifierBuilder().setProvider(
SecurityModuleConstants.BC).build(signingCert);
Expand All @@ -70,9 +70,7 @@ public SignerId authenticateMessage(byte[] messageData, byte[] signatureData) th
return signer.getSID();
} catch (PermissionStoreException e) {
throw new MessageAuthenticationException(e.getMessage(), e);
} catch (CMSException e) {
throw new SecurityException(e.getMessage(), e);
} catch (OperatorCreationException e) {
} catch (CMSException | OperatorCreationException e) {
throw new SecurityException(e.getMessage(), e);
}
}
Expand Down
Expand Up @@ -32,7 +32,7 @@
/**
* Class to check permissions based on the signer of a MessageRequest and the type of request.
*/
public class BasicOperationAuthorizor implements OperationAuthorizor {
public class BasicOperationAuthorizer implements OperationAuthorizer {

/** Mapper from operation type to needed permission */
private RequestToOperationPermissionMapper requestToPermissionMapper;
Expand All @@ -42,7 +42,7 @@ public class BasicOperationAuthorizor implements OperationAuthorizor {
/**
* @param permissionStore permissionStore which holds the permissions to check against.
*/
public BasicOperationAuthorizor(PermissionStore permissionStore) {
public BasicOperationAuthorizer(PermissionStore permissionStore) {
requestToPermissionMapper = new RequestToOperationPermissionMapper();
this.permissionStore = permissionStore;
}
Expand Down
Expand Up @@ -98,7 +98,7 @@ public class BasicSecurityManager implements SecurityManager {
/** Object to sign messages */
private final MessageSigner signer;
/** Object to authorize operations */
private final OperationAuthorizor authorizer;
private final OperationAuthorizer authorizer;
/** Object storing permissions and certificates */
private final PermissionStore permissionStore;
/** int value to keep track of the next keystore alias */
Expand All @@ -122,7 +122,7 @@ public class BasicSecurityManager implements SecurityManager {
* @param componentID the component ID
*/
public BasicSecurityManager(RepositorySettings repositorySettings, String privateKeyFile, MessageAuthenticator authenticator,
MessageSigner signer, OperationAuthorizor authorizer, PermissionStore permissionStore, String componentID) {
MessageSigner signer, OperationAuthorizer authorizer, PermissionStore permissionStore, String componentID) {
ArgumentValidator.checkNotNull(repositorySettings, "repositorySettings");
ArgumentValidator.checkNotNull(authenticator, "authenticator");
ArgumentValidator.checkNotNull(signer, "signer");
Expand Down Expand Up @@ -185,51 +185,56 @@ public String signMessage(String message) throws MessageSigningException {
public void authorizeCertificateUse(String certificateUser, String messageData, String signature)
throws CertificateUseException {
if(repositorySettings.getProtocolSettings().isRequireOperationAuthorization()) {
byte[] decodeSig = Base64.decode(signature.getBytes(StandardCharsets.UTF_8));
CMSSignedData s;
try {
s = new CMSSignedData(new CMSProcessableByteArray(messageData.getBytes(StandardCharsets.UTF_8)), decodeSig);
} catch (CMSException e) {
throw new SecurityException(e.getMessage(), e);
}

SignerInformation signer = (SignerInformation) s.getSignerInfos().getSigners().iterator().next();
CMSSignedData s = makeSignedData(messageData, signature);

SignerInformation signer = s.getSignerInfos().getSigners().iterator().next();
authorizer.authorizeCertificateUse(certificateUser, signer.getSID());
}
}

@Override
public String getCertificateFingerprint(SignerId signer) throws UnregisteredPermissionException {
return permissionStore.getCertificateFingerprint(signer);
}

/**
* Method to authorize an operation
* Method to authorize an operation
* @param operationType the type of operation that is to be authorized.
* @param messageData the data of the message request.
* @param signature the signature belonging to the message request.
* @throws OperationAuthorizationException in case of failure.
* @throws OperationAuthorizationException in case of failure.
*/
public void authorizeOperation(String operationType, String messageData, String signature, String collectionID)
public void authorizeOperation(String operationType, String messageData, String signature, String collectionID)
throws OperationAuthorizationException {
if(repositorySettings.getProtocolSettings().isRequireOperationAuthorization()) {
byte[] decodeSig = Base64.decode(signature.getBytes(StandardCharsets.UTF_8));
CMSSignedData s;
try {
s = new CMSSignedData(new CMSProcessableByteArray(messageData.getBytes(StandardCharsets.UTF_8)), decodeSig);
} catch (CMSException e) {
throw new SecurityException(e.getMessage(), e);
}

SignerInformation signer = (SignerInformation) s.getSignerInfos().getSigners().iterator().next();
CMSSignedData s = makeSignedData(messageData, signature);

SignerInformation signer = s.getSignerInfos().getSigners().iterator().next();
try {
authorizer.authorizeOperation(operationType, signer.getSID(), collectionID);
authorizer.authorizeOperation(operationType, signer.getSID(), collectionID);
} catch (UnregisteredPermissionException e) {
log.info(e.getMessage());
}
}
}


/**
* Encapsulates the data of a message request and a signature into a CMSSignedData object.
* @param messageData the data of a message request.
* @param signature the signature belonging to the message request.
* @return the signed data.
*/
private CMSSignedData makeSignedData(String messageData, String signature) {
byte[] decodeSig = Base64.decode(signature.getBytes(StandardCharsets.UTF_8));
CMSSignedData signedData;
try {
signedData = new CMSSignedData(new CMSProcessableByteArray(messageData.getBytes(StandardCharsets.UTF_8)), decodeSig);
} catch (CMSException e) {
throw new SecurityException(e.getMessage(), e);
}
return signedData;
}

@Override
public String getCertificateFingerprint(SignerId signer) throws UnregisteredPermissionException {
return permissionStore.getCertificateFingerprint(signer);
}

/**
* Do initialization work
* - Creates keystore
Expand Down
Expand Up @@ -29,7 +29,7 @@
/**
* Class to authorize an operation based on the certificate which has signed the operation request.
*/
public interface OperationAuthorizor {
public interface OperationAuthorizer {

/**
* Authorize an operation based on its signature
Expand All @@ -39,7 +39,7 @@ public interface OperationAuthorizor {
* @throws OperationAuthorizationException if the authorization fails.
* @throws UnregisteredPermissionException if no permissions could be found for the signer
*/
abstract void authorizeOperation(String operationType, SignerId signer, String collectionID)
void authorizeOperation(String operationType, SignerId signer, String collectionID)
throws OperationAuthorizationException, UnregisteredPermissionException;

/**
Expand All @@ -49,6 +49,6 @@ abstract void authorizeOperation(String operationType, SignerId signer, String c
* @throws CertificateUseException in case the message has been signed by the wrong user.
*
*/
abstract void authorizeCertificateUse(String certificateUser, SignerId signer) throws CertificateUseException;
void authorizeCertificateUse(String certificateUser, SignerId signer) throws CertificateUseException;

}
Expand Up @@ -30,7 +30,7 @@ public static SecurityManager getSecurityManager(Settings settings, Path compone
PermissionStore permissionStore = new PermissionStore();
MessageAuthenticator authenticator = new BasicMessageAuthenticator(permissionStore);
MessageSigner signer = new BasicMessageSigner();
OperationAuthorizor authorizer = new BasicOperationAuthorizor(permissionStore);
OperationAuthorizer authorizer = new BasicOperationAuthorizer(permissionStore);
return new BasicSecurityManager(settings.getRepositorySettings(), componentCertificate.toString(),
authenticator, signer, authorizer, permissionStore, componentID);
}
Expand Down
Expand Up @@ -30,9 +30,10 @@ public void testUncaughtExceptionHandler() throws Exception {
ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

//We mock an appender so we can catch the log messages
@SuppressWarnings("unchecked")
final Appender<ILoggingEvent> mockAppender = mock(Appender.class);

//Nessesary for the logback framework, all appenders must have a name
//Necessary for the logback framework, all appenders must have a name
when(mockAppender.getName()).thenReturn("MOCK");
//Add the appender to the root logger
rootLogger.addAppender(mockAppender);
Expand Down
Expand Up @@ -21,12 +21,12 @@
*/
package org.bitrepository.common;

import org.testng.Assert;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.testng.Assert;

public class TestValidationUtils {
/**
* Validates that only one constructor is declared and it is private and inaccessible.
Expand All @@ -39,7 +39,7 @@ public class TestValidationUtils {
public static void validateUtilityClass(Class utilityClass) {
Constructor[] constructors = utilityClass.getDeclaredConstructors();
Assert.assertEquals(constructors.length, 1);
Assert.assertFalse(constructors[0].isAccessible(), "The constructor should not be accessible.");
Assert.assertFalse(constructors[0].canAccess(null), "The constructor should not be accessible.");
Assert.assertNotEquals((constructors[0].getModifiers() & Modifier.PRIVATE), 0,
"The constructor should be private: " + constructors[0]);

Expand Down
Expand Up @@ -46,9 +46,9 @@ public static Connection takeDatabase(File jarfile, String dbname, File dbUnzipD

/* Set DB name */
String driverName = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driverName).newInstance();
Class.forName(driverName).getDeclaredConstructor().newInstance();

String dburi = "jdbc:derby:" + dbfile;
return DriverManager.getConnection(dburi);
String dbUri = "jdbc:derby:" + dbfile;
return DriverManager.getConnection(dbUri);
}
}
Expand Up @@ -73,7 +73,8 @@ public void badDateMessageTest() throws IOException, SAXException, JAXBException
addDescription("Test to ensure that messages carrying dates must provide offset.");
String messagePath = ExampleMessageFactory.PATH_TO_EXAMPLES + "BadMessages/" +
"BadDateAlarmMessage" + ExampleMessageFactory.EXAMPLE_FILE_POSTFIX;
String message = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(messagePath));
InputStream messageIS = Thread.currentThread().getContextClassLoader().getResourceAsStream(messagePath);
String message = IOUtils.toString(messageIS, StandardCharsets.UTF_8);
JaxbHelper jaxbHelper = new JaxbHelper(ExampleMessageFactory.PATH_TO_SCHEMA, ExampleMessageFactory.SCHEMA_NAME);
jaxbHelper.validate(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)));
AlarmMessage am = jaxbHelper.loadXml(AlarmMessage.class, new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)));
Expand Down Expand Up @@ -123,8 +124,7 @@ public String getNamespaceURI(String prefix) {
}

// Dummy implementation - not used!
@SuppressWarnings("rawtypes")
public Iterator getPrefixes(String val) {
public Iterator<String> getPrefixes(String val) {
return null;
}

Expand Down
Expand Up @@ -128,7 +128,7 @@ public void getFileByInputStreamTest() throws IOException {
URL testFileUrl = testFile.toURI().toURL();

InputStream is = lfe.getFile(testFileUrl);
String fileContent = IOUtils.toString(is);
String fileContent = IOUtils.toString(is, StandardCharsets.UTF_8);
Assert.assertEquals(fileContent, testFileContent);
}

Expand Down
Expand Up @@ -56,8 +56,8 @@ public static <T> T createMessage(Class<T> messageType) throws Exception {
*/
private static String loadXMLExample(String messageName) throws Exception {
String filePath = PATH_TO_EXAMPLES + messageName + EXAMPLE_FILE_POSTFIX;
InputStream f = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
return IOUtils.toString(f);
InputStream fileIS = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
return IOUtils.toString(fileIS, StandardCharsets.UTF_8);
}

}

0 comments on commit d8ae8ce

Please sign in to comment.