gradle module and build

This commit is contained in:
2015-10-03 09:05:53 +02:00
parent fe563e32ee
commit 1fc29e9916
49 changed files with 771 additions and 262 deletions

View File

@ -1,7 +1,24 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.networking.DefaultNetworkHandler;
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
import ch.dissem.bitmessage.repository.JdbcAddressRepository;
@ -10,15 +27,26 @@ 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.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 java.util.Arrays;
import java.util.List;
import java.util.Set;
@CrossOrigin
@RestController
@EnableAutoConfiguration
public class JabitServerApplication {
private static final int SHORTLIST_SIZE = 5;
private final Set<String> whitelist;
private final Set<String> shortlist;
private final Set<String> blacklist;
private BitmessageContext ctx;
@RequestMapping("status")
@ -32,13 +60,48 @@ public class JabitServerApplication {
if (broadcaster == null) {
broadcaster = new BitmessageAddress(broadcastAddress);
}
if (!whitelist.isEmpty() && !whitelist.contains(broadcaster.getAddress())) {
return new Broadcasts(broadcaster, new 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 +
" is on the blacklist, so it's messages can't be shown."));
}
if (!broadcaster.isSubscribed()) {
ctx.addSubscribtion(broadcaster);
}
return new Broadcasts(broadcaster, ctx.messages().findMessages(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 new Broadcasts(broadcaster, messages);
}
public JabitServerApplication() {
whitelist = Utils.readOrCreateList(
"whitelist.conf",
"# If there are any Bitmessage addresses in the whitelist, only those will be shown.\n" +
"# blacklist.conf will be ignored, but shortlist.conf will be applied to whitelisted addresses.\n"
);
shortlist = Utils.readOrCreateList(
"shortlist.conf",
"# Broadcasts of these addresses will be restricted to the last " + SHORTLIST_SIZE + " entries.\n\n" +
"# Time Service:\n" +
"BM-BcbRqcFFSQUUmXFKsPJgVQPSiFA3Xash\n\n" +
"# Q's Aktivlist:\n" +
"BM-GtT7NLCCAu3HrT7dNTUTY9iDns92Z2ND\n"
);
blacklist = Utils.readOrCreateList(
"blacklist.conf",
"# Bitmessage addresses in this file are being ignored and their broadcasts won't be returned.\n"
);
JdbcConfig config = new JdbcConfig();
ctx = new BitmessageContext.Builder()
.addressRepo(new JdbcAddressRepository(config))

View File

@ -1,3 +1,19 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.server;
import org.springframework.boot.builder.SpringApplicationBuilder;

View File

@ -0,0 +1,41 @@
package ch.dissem.bitmessage.server;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Utils {
public static Set<String> readOrCreateList(String filename, String content) {
try {
File file = new File(filename);
if (!file.exists()) {
if (file.createNewFile()) {
try (FileWriter fw = new FileWriter(file)) {
fw.write(content);
}
}
}
return readList(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Set<String> readList(File file) {
Set<String> result = new HashSet<>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.startsWith("BM-")) {
result.add(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
}

View File

@ -1,3 +1,19 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.server.entities;
import ch.dissem.bitmessage.entity.BitmessageAddress;
@ -18,9 +34,15 @@ public class Broadcasts {
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;
}

View File

@ -1,43 +1,66 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.server.entities;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.utils.UnixTime;
import java.time.Instant;
import java.time.ZonedDateTime;
/**
* Created by chrigu on 30.09.15.
*/
public class Message {
private Object id;
private Long received;
private String subject;
private String body;
public Message(){}
public Message(Plaintext plaintext) {
this.id = plaintext.getId();
this.subject = plaintext.getSubject();
this.body = plaintext.getText();
public Message() {
}
public void setId(Object id) {
this.id = id;
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 void setSubject(String subject) {
this.subject = subject;
}
public String getSubject() {
return subject;
}
public void setBody(String body) {
this.body = body;
}
public String getBody() {
return body;
}
public Long getReceived() {
return received;
}
}

View File

@ -1,3 +1,19 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.server.entities;
import ch.dissem.bitmessage.entity.BitmessageAddress;