Refactored JSON entities to better reflect the actual JSON structure

added README
This commit is contained in:
Christian Basler 2015-10-16 06:41:23 +02:00
parent 3df853734c
commit ca2bb89cee
6 changed files with 115 additions and 85 deletions

41
README.md Normal file
View File

@ -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`.

View File

@ -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<Plaintext> 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;
}
}

View File

@ -27,7 +27,6 @@ import ch.dissem.bitmessage.repository.JdbcInventory;
import ch.dissem.bitmessage.repository.JdbcMessageRepository; import ch.dissem.bitmessage.repository.JdbcMessageRepository;
import ch.dissem.bitmessage.security.bc.BouncySecurity; import ch.dissem.bitmessage.security.bc.BouncySecurity;
import ch.dissem.bitmessage.server.entities.Broadcasts; import ch.dissem.bitmessage.server.entities.Broadcasts;
import ch.dissem.bitmessage.server.entities.Message;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
@ -46,6 +45,9 @@ import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.Timer; import java.util.Timer;
import static ch.dissem.bitmessage.server.Converter.broadcasts;
import static ch.dissem.bitmessage.server.Converter.message;
@CrossOrigin @CrossOrigin
@RestController @RestController
@EnableAutoConfiguration @EnableAutoConfiguration
@ -77,11 +79,11 @@ public class JabitServerApplication {
} }
if (!whitelist.isEmpty() && !whitelist.contains(broadcaster.getAddress())) { 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.")); " can't be shown, as the sender isn't on the whitelist."));
} }
if (blacklist.contains(broadcaster.getAddress())) { 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.")); " is on the blacklist, so it's messages can't be shown."));
} }
@ -95,7 +97,7 @@ public class JabitServerApplication {
messages.remove(messages.size() - 1); messages.remove(messages.size() - 1);
} }
} }
return new Broadcasts(broadcaster, messages); return broadcasts(broadcaster, messages);
} }
public JabitServerApplication() { public JabitServerApplication() {
@ -138,7 +140,7 @@ public class JabitServerApplication {
LOG.error("Couldn't read port property - is it a number?", e); 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() ctx = new BitmessageContext.Builder()
.addressRepo(new JdbcAddressRepository(config)) .addressRepo(new JdbcAddressRepository(config))
.inventory(new JdbcInventory(config)) .inventory(new JdbcInventory(config))

View File

@ -22,32 +22,9 @@ import ch.dissem.bitmessage.entity.Plaintext;
import java.util.Collection; import java.util.Collection;
/** /**
* Created by chrigu on 30.09.15. * JSON representation for the broadcasts of a specific sender
*/ */
public class Broadcasts { public class Broadcasts {
private final Sender sender; public Sender sender;
private final Message[] messages; public 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;
}
} }

View File

@ -23,44 +23,11 @@ import java.time.Instant;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
/** /**
* Created by chrigu on 30.09.15. * JSON representation for plaintext messages
*/ */
public class Message { public class Message {
private Object id; public Object id;
private Long received; public Long received;
private String subject; public String subject;
private String body; public 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;
}
} }

View File

@ -19,22 +19,9 @@ package ch.dissem.bitmessage.server.entities;
import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.BitmessageAddress;
/** /**
* Created by chrigu on 30.09.15. * JSON representation for a BitmessageAddress
*/ */
public class Sender { public class Sender {
private final String address; public String address;
private final String alias; public String alias;
public Sender(BitmessageAddress address) {
this.address = address.getAddress();
this.alias = address.toString();
}
public String getAddress() {
return address;
}
public String getAlias() {
return alias;
}
} }