big change: move the json loading and random server selection to main PieCannon class (so that it has a reason to live :)) and implement loading the json file in the android app.

I deliberated between re-using the json file in the android app and using a sqlite database or some such thing, given that a UI for editing these will have to be implemented at some point. I figure the json file is more config than data, and probably should be as consistent as possible between platforms.

For now at least this makes the app usable for me, without having to hard-code my server credentials.
This commit is contained in:
2020-10-30 06:52:53 -05:00
parent 432cfa4d21
commit da39586c4d
19 changed files with 225 additions and 74 deletions

View File

@@ -27,11 +27,6 @@
<artifactId>xdg-java</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@@ -3,34 +3,22 @@ package net.monarchpass.piecannon;
import java.net.URI;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import org.freedesktop.BaseDirectory;
import com.google.common.collect.Streams;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.java.Log;
import java.util.logging.Level;
import net.monarchpass.piecannon.util.ServerFactory;
@Log
public class App {
public static void main (final String... args) throws Exception {
final File serversJson = getServersJson();
final List<Server> servers = loadServersFrom(serversJson);
final PieCannon cannon = new PieCannon();
final List<Server> servers = cannon.loadServersFrom(serversJson);
log.log(Level.INFO, "{0} servers loaded from {1}", new Object[] {
servers.size(), serversJson
});
@@ -40,7 +28,7 @@ public class App {
System.exit(1);
}
final Server server = servers.get(new Random().nextInt(servers.size()));
final Server server = cannon.selectServer();
log.log(Level.INFO, "Randomly selected server: {0}", server.getLabel());
if (args.length == 0) {
@@ -57,23 +45,7 @@ public class App {
final URI result = server.upload(source);
System.out.println(result);
}
public static List<Server> loadServersFrom (final File serversJson) throws IOException {
if (!serversJson.exists()) {
return Collections.EMPTY_LIST;
}
final ServerFactory factory = new ServerFactory();
try (final InputStream in = new FileInputStream(serversJson)) {
final JsonParser parser = new JsonParser();
return Streams.stream(parser.parse(new InputStreamReader(in)).getAsJsonArray())
.filter(JsonElement::isJsonObject)
.map(JsonObject.class::cast)
.map(factory)
.collect(Collectors.toList());
}
}
public static File getServersJson () {
return new File(getDataDirectory(), "servers.json");
}

View File

@@ -1,29 +0,0 @@
package net.monarchpass.piecannon;
import java.net.URI;
import java.util.function.Function;
import com.google.gson.JsonObject;
import net.monarchpass.piecannon.impl.SftpServer;
public class ServerFactory implements Function<JsonObject, Server> {
public Server apply (final JsonObject object) {
return makeSftpServer(object);
}
private Server makeSftpServer (final JsonObject object) {
final String host = object.getAsJsonPrimitive("host").getAsString();
final String label = object.has("label") ? object.getAsJsonPrimitive("label").getAsString() : host;
final int port = object.has("port") ? object.getAsJsonPrimitive("port").getAsInt() : 22;
final String username = object.getAsJsonPrimitive("username").getAsString();
final String password = object.getAsJsonPrimitive("password").getAsString();
final String path = object.getAsJsonPrimitive("path").getAsString();
final String url = object.getAsJsonPrimitive("url").getAsString();
return new SftpServer(
label, host, port, username, password,
path, URI.create(url)
);
}
}