Some refactoring, added Swagger documentation
This commit is contained in:
@ -16,90 +16,15 @@
|
||||
|
||||
package ch.dissem.bitmessage.server;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
import ch.dissem.bitmessage.entity.Plaintext;
|
||||
import ch.dissem.bitmessage.server.entities.Broadcasts;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
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
|
||||
@SpringBootApplication
|
||||
@EnableSwagger2
|
||||
@ComponentScan(basePackageClasses = JabitServerController.class)
|
||||
public class JabitServerApplication {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JabitServerApplication.class);
|
||||
|
||||
private static final long HOUR = 60 * 60 * 1000l; // in ms
|
||||
|
||||
private static final String CONFIG_FILE = "config.properties";
|
||||
private static final String PROPERTY_PORT = "port";
|
||||
|
||||
private static final int SHORTLIST_SIZE = 5;
|
||||
|
||||
@Resource
|
||||
private Set<String> whitelist;
|
||||
@Resource
|
||||
private Set<String> shortlist;
|
||||
@Resource
|
||||
private Set<String> blacklist;
|
||||
@Inject
|
||||
private BitmessageContext ctx;
|
||||
|
||||
@RequestMapping("status")
|
||||
public String status() {
|
||||
return ctx.status().toString();
|
||||
}
|
||||
|
||||
@RequestMapping("read/{broadcastAddress}")
|
||||
public Broadcasts read(@PathVariable String broadcastAddress) {
|
||||
BitmessageAddress broadcaster = ctx.addresses().getAddress(broadcastAddress);
|
||||
if (broadcaster == null) {
|
||||
broadcaster = new BitmessageAddress(broadcastAddress);
|
||||
}
|
||||
|
||||
if (!whitelist.isEmpty() && !whitelist.contains(broadcaster.getAddress())) {
|
||||
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 broadcasts(broadcaster, message("Blacklisted", "Unfortunately, " + broadcaster +
|
||||
" is on the blacklist, so it's messages can't be shown."));
|
||||
}
|
||||
|
||||
if (!broadcaster.isSubscribed()) {
|
||||
ctx.addSubscribtion(broadcaster);
|
||||
}
|
||||
List<Plaintext> messages = ctx.messages().findMessages(broadcaster);
|
||||
if (shortlist.contains(broadcaster.getAddress())) {
|
||||
while (messages.size() > SHORTLIST_SIZE) {
|
||||
ctx.messages().remove(messages.get(messages.size() - 1));
|
||||
messages.remove(messages.size() - 1);
|
||||
}
|
||||
}
|
||||
return broadcasts(broadcaster, messages);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void setUp() {
|
||||
ctx.startup();
|
||||
new Timer().scheduleAtFixedRate(new CleanupJob(ctx), 1 * HOUR, 24 * HOUR);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JabitServerApplication.class, args);
|
||||
|
@ -11,6 +11,8 @@ import ch.dissem.bitmessage.security.bc.BouncySecurity;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@ -71,4 +73,11 @@ public class JabitServerConfig {
|
||||
"# Bitmessage addresses in this file are being ignored and their broadcasts won't be returned.\n"
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Docket api() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.select()
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,96 @@
|
||||
package ch.dissem.bitmessage.server;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
import ch.dissem.bitmessage.entity.Plaintext;
|
||||
import ch.dissem.bitmessage.server.entities.Broadcasts;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
|
||||
import static ch.dissem.bitmessage.server.Converter.broadcasts;
|
||||
import static ch.dissem.bitmessage.server.Converter.message;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||
|
||||
/**
|
||||
* @author Christian Basler
|
||||
*/
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
public class JabitServerController {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JabitServerController.class);
|
||||
|
||||
private static final long HOUR = 60 * 60 * 1000l; // in ms
|
||||
|
||||
private static final String CONFIG_FILE = "config.properties";
|
||||
private static final String PROPERTY_PORT = "port";
|
||||
|
||||
private static final int SHORTLIST_SIZE = 5;
|
||||
|
||||
@Resource
|
||||
private Set<String> whitelist;
|
||||
@Resource
|
||||
private Set<String> shortlist;
|
||||
@Resource
|
||||
private Set<String> blacklist;
|
||||
@Inject
|
||||
private BitmessageContext ctx;
|
||||
|
||||
@RequestMapping(value = "status", method = GET, produces = "application/json")
|
||||
public String status() {
|
||||
return ctx.status().toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "read/{broadcastAddress}", method = GET)
|
||||
public Broadcasts read(@PathVariable String broadcastAddress) {
|
||||
if ("test".equalsIgnoreCase(broadcastAddress)) {
|
||||
return broadcasts(
|
||||
new BitmessageAddress("BM-2cWhyaPxydemCeM8dWJUBmEo8iu7v2JptK"),
|
||||
message("Test", "This is a test message. The rest service is running."),
|
||||
message("Another Test", "And because it's such fun, a second message.")
|
||||
);
|
||||
}
|
||||
|
||||
BitmessageAddress broadcaster = ctx.addresses().getAddress(broadcastAddress);
|
||||
if (broadcaster == null) {
|
||||
broadcaster = new BitmessageAddress(broadcastAddress);
|
||||
}
|
||||
|
||||
if (!whitelist.isEmpty() && !whitelist.contains(broadcaster.getAddress())) {
|
||||
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 broadcasts(broadcaster, message("Blacklisted", "Unfortunately, " + broadcaster +
|
||||
" is on the blacklist, so it's messages can't be shown."));
|
||||
}
|
||||
|
||||
if (!broadcaster.isSubscribed()) {
|
||||
ctx.addSubscribtion(broadcaster);
|
||||
}
|
||||
List<Plaintext> messages = ctx.messages().findMessages(broadcaster);
|
||||
if (shortlist.contains(broadcaster.getAddress())) {
|
||||
while (messages.size() > SHORTLIST_SIZE) {
|
||||
ctx.messages().remove(messages.get(messages.size() - 1));
|
||||
messages.remove(messages.size() - 1);
|
||||
}
|
||||
}
|
||||
return broadcasts(broadcaster, messages);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void setUp() {
|
||||
ctx.startup();
|
||||
new Timer().scheduleAtFixedRate(new CleanupJob(ctx), 1 * HOUR, 24 * HOUR);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user