Code cleanup

This commit is contained in:
Christian Basler 2016-02-24 23:10:04 +01:00
parent 4dd639e651
commit 9f1e0057c9
12 changed files with 33 additions and 21 deletions

View File

@ -25,6 +25,7 @@ import ch.dissem.bitmessage.entity.payload.Pubkey.Feature;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.ports.*;
@ -119,7 +120,7 @@ public class BitmessageContext {
public void addDistributedMailingList(String address, String alias) {
// TODO
throw new RuntimeException("not implemented");
throw new ApplicationException("not implemented");
}
public void broadcast(final BitmessageAddress from, final String subject, final String message) {
@ -187,7 +188,7 @@ public class BitmessageContext {
case BROADCAST:
return Factory.getBroadcast(msg);
default:
throw new RuntimeException("Unknown message type " + msg.getType());
throw new ApplicationException("Unknown message type " + msg.getType());
}
}

View File

@ -103,7 +103,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
address.setPubkey(pubkey);
LOG.info("Got pubkey for contact " + address);
ctx.getAddressRepository().save(address);
List<Plaintext> messages = ctx.getMessageRepository().findMessages(Plaintext.Status.PUBKEY_REQUESTED, address);
List<Plaintext> messages = ctx.getMessageRepository().findMessages(PUBKEY_REQUESTED, address);
LOG.info("Sending " + messages.size() + " messages for contact " + address);
for (Plaintext msg : messages) {
msg.setStatus(DOING_PROOF_OF_WORK);

View File

@ -16,6 +16,7 @@
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.utils.AccessCounter;
import ch.dissem.bitmessage.utils.Encode;
@ -66,7 +67,7 @@ public class CustomMessage implements MessagePayload {
write(out);
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
}
}
@ -77,7 +78,7 @@ public class CustomMessage implements MessagePayload {
Encode.varString(command, out);
out.write(data);
} else {
throw new RuntimeException("Tried to write custom message without data. " +
throw new ApplicationException("Tried to write custom message without data. " +
"Programmer: did you forget to override #write()?");
}
}
@ -90,7 +91,7 @@ public class CustomMessage implements MessagePayload {
try {
return new CustomMessage(COMMAND_ERROR, message.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
}
}

View File

@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.utils.Decode;
import ch.dissem.bitmessage.utils.Encode;
@ -111,9 +112,9 @@ public class Plaintext implements Streamable {
public void setTo(BitmessageAddress to) {
if (this.to.getVersion() != 0)
throw new RuntimeException("Correct address already set");
throw new IllegalStateException("Correct address already set");
if (!Arrays.equals(this.to.getRipe(), to.getRipe())) {
throw new RuntimeException("RIPEs don't match");
throw new IllegalArgumentException("RIPEs don't match");
}
this.to = to;
}
@ -223,7 +224,7 @@ public class Plaintext implements Streamable {
}
return text;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
}
@ -402,7 +403,7 @@ public class Plaintext implements Streamable {
this.encoding = Encoding.SIMPLE.getCode();
this.message = ("Subject:" + subject + '\n' + "Body:" + message).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
return this;
}

View File

@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.utils.Bytes;
import ch.dissem.bitmessage.utils.Decode;
@ -71,7 +72,7 @@ public class PrivateKey implements Streamable {
this.pubkey = security().createPubkey(version, stream, privateSigningKey, privateEncryptionKey,
nonceTrialsPerByte, extraBytes, features);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
}

View File

@ -23,4 +23,8 @@ public class ApplicationException extends RuntimeException {
public ApplicationException(Throwable cause) {
super(cause);
}
public ApplicationException(String message) {
super(message);
}
}

View File

@ -16,6 +16,7 @@
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.utils.Bytes;
import java.security.MessageDigest;
@ -37,7 +38,7 @@ public class SimplePOWEngine implements ProofOfWorkEngine {
try {
mda = MessageDigest.getInstance("SHA-512");
} catch (Exception e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
do {
inc(nonce);

View File

@ -20,12 +20,10 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.payload.GetPubkey;
import ch.dissem.bitmessage.entity.payload.Msg;
import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Singleton;
import ch.dissem.bitmessage.utils.TestUtils;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@ -39,6 +37,7 @@ import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.*;
/**

View File

@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.CustomMessage;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.payload.CryptoBox;
import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.exception.DecryptionFailedException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.utils.Encode;
@ -134,9 +135,9 @@ public class CryptoCustomMessage<T extends Streamable> extends CustomMessage {
return read;
}
public void checkSignature(Pubkey pubkey) throws IOException, RuntimeException {
public void checkSignature(Pubkey pubkey) throws IOException, IllegalStateException {
if (!security().isSignatureValid(out.toByteArray(), varBytes(wrapped), pubkey)) {
throw new RuntimeException("Signature check failed");
throw new IllegalStateException("Signature check failed");
}
}
}

View File

@ -252,6 +252,7 @@ class Connection {
case CUSTOM:
case VERACK:
case VERSION:
default:
throw new RuntimeException("Unexpectedly received '" + messagePayload.getCommand() + "' command");
}
}

View File

@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.ports.MessageRepository;
import ch.dissem.bitmessage.utils.Strings;
import org.slf4j.Logger;
@ -53,7 +54,7 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
result.add(getLabel(rs));
}
} catch (SQLException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
return result;
}
@ -118,7 +119,7 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
case 1:
return plaintexts.get(0);
default:
throw new RuntimeException("This shouldn't happen, found " + plaintexts.size() +
throw new ApplicationException("This shouldn't happen, found " + plaintexts.size() +
" messages, one or none was expected");
}
}
@ -225,10 +226,10 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito
} catch (SQLException e1) {
LOG.debug(e1.getMessage(), e);
}
throw new RuntimeException(e);
throw new ApplicationException(e);
}
} catch (SQLException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
}

View File

@ -18,6 +18,7 @@ package ch.dissem.bitmessage.wif;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.exception.ApplicationException;
import ch.dissem.bitmessage.utils.Base58;
import org.ini4j.Ini;
import org.ini4j.Profile;
@ -95,7 +96,7 @@ public class WifExporter {
try {
ini.store(writer);
} catch (IOException e) {
throw new RuntimeException(e);
throw new ApplicationException(e);
}
return writer.toString();
}