From ca2bb89cee5e1331f58692bcbd9af751c8bbe665 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 16 Oct 2015 06:41:23 +0200 Subject: [PATCH] Refactored JSON entities to better reflect the actual JSON structure added README --- README.md | 41 ++++++++++++++ .../dissem/bitmessage/server/Converter.java | 56 +++++++++++++++++++ .../server/JabitServerApplication.java | 12 ++-- .../server/entities/Broadcasts.java | 29 +--------- .../bitmessage/server/entities/Message.java | 43 ++------------ .../bitmessage/server/entities/Sender.java | 19 +------ 6 files changed, 115 insertions(+), 85 deletions(-) create mode 100644 README.md create mode 100644 src/main/java/ch/dissem/bitmessage/server/Converter.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..9679600 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +Jabit Server +============ + +This is the server node using the Jabit library. You can run it by calling +``` +java -jar jabit-server.jar +``` +The interface will be available on port 9000, Bitmessage as usual on Port 8444. + +There are still a few problems with the interface (the idea is to allow collecting +and displaying broadcasts). + +On first startup it will create a config file (allowing you to configure the +Bitmessage port), a whitelist, a blacklist and a shortlist. If the whitelist isn't +empty, the blacklist will be irrelevant. You can disable the feature by simply +adding a valid Bitmessage address to the whitelist. For shortlisted addresses, only +the last five broadcasts are displayed and stored (useful e.g. for time services or +Q's Aktivlist). + +Building / Development +---------------------- + +You can build the jar file with +``` +./gradlew build +``` +As there is a problem with the build order, you'll need to do this twice. + +To deploy on a Ubuntu server (might work on other Linuxes as well), create a file +`/etc/init/jabit.conf` with the following contents: +``` +chdir /srv/jabit + +exec su -s /bin/sh -c 'exec "$0" "$@"' jabit -- /usr/bin/java -jar jabit-server.jar --server.port=9000 > /dev/null + +start on runlevel [2345] +stop on runlevel [^2345] + +``` +there must be a user jabit and a folder `/srv/jabit` where this user has write +permission containing `jabit-server.jar`. \ No newline at end of file diff --git a/src/main/java/ch/dissem/bitmessage/server/Converter.java b/src/main/java/ch/dissem/bitmessage/server/Converter.java new file mode 100644 index 0000000..0e387c4 --- /dev/null +++ b/src/main/java/ch/dissem/bitmessage/server/Converter.java @@ -0,0 +1,56 @@ +package ch.dissem.bitmessage.server; + +import ch.dissem.bitmessage.entity.BitmessageAddress; +import ch.dissem.bitmessage.entity.Plaintext; +import ch.dissem.bitmessage.server.entities.Broadcasts; +import ch.dissem.bitmessage.server.entities.Message; +import ch.dissem.bitmessage.server.entities.Sender; +import ch.dissem.bitmessage.utils.UnixTime; + +import java.util.Collection; + +public class Converter { + public static Broadcasts broadcasts(BitmessageAddress sender, Collection messages) { + Broadcasts result = new Broadcasts(); + result.sender = sender(sender); + result.messages = new Message[messages.size()]; + int i = 0; + for (Plaintext msg : messages) { + result.messages[i] = message(msg); + i++; + } + return result; + } + + public static Broadcasts broadcasts(BitmessageAddress sender, Message... messages) { + Broadcasts result = new Broadcasts(); + result.sender = sender(sender); + result.messages = messages; + return result; + } + + public static Sender sender(BitmessageAddress sender) { + Sender result = new Sender(); + result.address = sender.getAddress(); + result.alias = sender.toString(); + return result; + } + + public static Message message(String subject, String body) { + Message result = new Message(); + result.id = 0; + result.received = UnixTime.now(); + result.subject = subject; + result.body = body; + return result; + } + + public static Message message(Plaintext plaintext) { + Message result = new Message(); + result.id = plaintext.getId(); + result.received = plaintext.getReceived(); + result.subject = plaintext.getSubject(); + result.body = plaintext.getText(); + return result; + } +} diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java index 18e5b46..bca5b32 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java @@ -27,7 +27,6 @@ import ch.dissem.bitmessage.repository.JdbcInventory; import ch.dissem.bitmessage.repository.JdbcMessageRepository; import ch.dissem.bitmessage.security.bc.BouncySecurity; import ch.dissem.bitmessage.server.entities.Broadcasts; -import ch.dissem.bitmessage.server.entities.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; @@ -46,6 +45,9 @@ import java.util.Properties; import java.util.Set; import java.util.Timer; +import static ch.dissem.bitmessage.server.Converter.broadcasts; +import static ch.dissem.bitmessage.server.Converter.message; + @CrossOrigin @RestController @EnableAutoConfiguration @@ -77,11 +79,11 @@ public class JabitServerApplication { } if (!whitelist.isEmpty() && !whitelist.contains(broadcaster.getAddress())) { - return new Broadcasts(broadcaster, new Message("Not Whitelisted", "Messages for " + broadcaster + + return broadcasts(broadcaster, message("Not Whitelisted", "Messages for " + broadcaster + " can't be shown, as the sender isn't on the whitelist.")); } if (blacklist.contains(broadcaster.getAddress())) { - return new Broadcasts(broadcaster, new Message("Blacklisted", "Unfortunately, " + broadcaster + + return broadcasts(broadcaster, message("Blacklisted", "Unfortunately, " + broadcaster + " is on the blacklist, so it's messages can't be shown.")); } @@ -95,7 +97,7 @@ public class JabitServerApplication { messages.remove(messages.size() - 1); } } - return new Broadcasts(broadcaster, messages); + return broadcasts(broadcaster, messages); } public JabitServerApplication() { @@ -138,7 +140,7 @@ public class JabitServerApplication { LOG.error("Couldn't read port property - is it a number?", e); } - JdbcConfig config = new JdbcConfig(); + JdbcConfig config = new JdbcConfig("jdbc:h2:file:jabit;AUTO_SERVER=TRUE", "sa", null); ctx = new BitmessageContext.Builder() .addressRepo(new JdbcAddressRepository(config)) .inventory(new JdbcInventory(config)) diff --git a/src/main/java/ch/dissem/bitmessage/server/entities/Broadcasts.java b/src/main/java/ch/dissem/bitmessage/server/entities/Broadcasts.java index c69dd78..1349a0f 100644 --- a/src/main/java/ch/dissem/bitmessage/server/entities/Broadcasts.java +++ b/src/main/java/ch/dissem/bitmessage/server/entities/Broadcasts.java @@ -22,32 +22,9 @@ import ch.dissem.bitmessage.entity.Plaintext; import java.util.Collection; /** - * Created by chrigu on 30.09.15. + * JSON representation for the broadcasts of a specific sender */ public class Broadcasts { - private final Sender sender; - private final Message[] messages; - - public Broadcasts(BitmessageAddress sender, Collection<Plaintext> messages) { - this.sender = new Sender(sender); - this.messages = new Message[messages.size()]; - int i = 0; - for (Plaintext msg : messages) { - this.messages[i] = new Message(msg); - i++; - } - } - - public Broadcasts(BitmessageAddress sender, Message... messages) { - this.sender = new Sender(sender); - this.messages = messages; - } - - public Sender getSender() { - return sender; - } - - public Message[] getMessages() { - return messages; - } + public Sender sender; + public Message[] messages; } diff --git a/src/main/java/ch/dissem/bitmessage/server/entities/Message.java b/src/main/java/ch/dissem/bitmessage/server/entities/Message.java index 3f6f94c..07038d6 100644 --- a/src/main/java/ch/dissem/bitmessage/server/entities/Message.java +++ b/src/main/java/ch/dissem/bitmessage/server/entities/Message.java @@ -23,44 +23,11 @@ import java.time.Instant; import java.time.ZonedDateTime; /** - * Created by chrigu on 30.09.15. + * JSON representation for plaintext messages */ public class Message { - private Object id; - private Long received; - private String subject; - private String body; - - public Message() { - } - - public Message(String subject, String body) { - this.id = 0; - this.received = UnixTime.now(); - this.subject = subject; - this.body = body; - } - - public Message(Plaintext plaintext) { - this.id = plaintext.getId(); - this.received = plaintext.getReceived(); - this.subject = plaintext.getSubject(); - this.body = plaintext.getText(); - } - - public Object getId() { - return id; - } - - public String getSubject() { - return subject; - } - - public String getBody() { - return body; - } - - public Long getReceived() { - return received; - } + public Object id; + public Long received; + public String subject; + public String body; } diff --git a/src/main/java/ch/dissem/bitmessage/server/entities/Sender.java b/src/main/java/ch/dissem/bitmessage/server/entities/Sender.java index 5746567..b61833c 100644 --- a/src/main/java/ch/dissem/bitmessage/server/entities/Sender.java +++ b/src/main/java/ch/dissem/bitmessage/server/entities/Sender.java @@ -19,22 +19,9 @@ package ch.dissem.bitmessage.server.entities; import ch.dissem.bitmessage.entity.BitmessageAddress; /** - * Created by chrigu on 30.09.15. + * JSON representation for a BitmessageAddress */ public class Sender { - private final String address; - private final String alias; - - public Sender(BitmessageAddress address) { - this.address = address.getAddress(); - this.alias = address.toString(); - } - - public String getAddress() { - return address; - } - - public String getAlias() { - return alias; - } + public String address; + public String alias; }