Skip to content

Commit 9a4cff3

Browse files
committed
Adding printouts and checks to the API keys so that nulls are detected
early and dealt with without exceltion
1 parent 902ff99 commit 9a4cff3

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

src/main/java/eu/mihosoft/vrl/v3d/CSGClient.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ class CSGClient {
2727
public CSGClient(String hostname, int port, File f) throws Exception {
2828
this.hostname = hostname;
2929
this.port = port;
30-
if (f != null)
31-
if(f.exists())
32-
key = Files.readAllLines(f.toPath()).toArray(new String[0])[0];
33-
30+
if (f == null)
31+
throw new NullPointerException("API key file can not be null");
32+
if(f.exists())
33+
key = Files.readAllLines(f.toPath()).toArray(new String[0])[0];
34+
else {
35+
System.err.println("Error! API key file does not exist! "+f.getAbsolutePath());
36+
}
37+
if(key==null || key.length()==0)
38+
System.err.println("Key error, no key provided by "+f.getAbsolutePath());
39+
else
40+
System.out.println("API Key Loaded from "+f.getAbsolutePath());
3441
Socket socket = new Socket(hostname, port);
3542
socket.close();
3643

src/main/java/eu/mihosoft/vrl/v3d/CSGServer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ public class CSGServer {
4444
public CSGServer(int port, File APIKEYS) throws IOException {
4545
this.port = port;
4646
this.threadPool = Executors.newCachedThreadPool();
47-
if (APIKEYS != null) {
48-
if(APIKEYS.exists())
49-
lines = Files.readAllLines(APIKEYS.toPath()).toArray(new String[0]);
47+
48+
if (APIKEYS == null) {
49+
throw new NullPointerException("API Key file can not be null");
5050
}
51+
if(APIKEYS.exists())
52+
lines = Files.readAllLines(APIKEYS.toPath()).toArray(new String[0]);
5153
if(lines!=null) {
52-
System.out.println("Starting server with "+lines.length+" keys");
54+
System.out.println("Starting server with "+lines.length+" keys from "+APIKEYS.getAbsolutePath());
55+
}else {
56+
System.err.println("NO API KEYFILE Provided: "+APIKEYS.getAbsolutePath());
5357
}
5458
}
5559

src/main/java/eu/mihosoft/vrl/v3d/CSGServerHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public void run() {
2929
CSGRequest request = (CSGRequest) ois.readObject();
3030
System.out.println("Received request: " + request.getOperation());
3131
boolean APIPass = true;
32+
String apiKey2 = request.getAPIKey();
3233
if (getAPIKEYs() != null) {
3334
APIPass = false;
34-
for (int i = 0; i < getAPIKEYs().length; i++)
35-
if (request.getAPIKey().contentEquals(getAPIKEYs()[i])) {
36-
APIPass = true;
37-
System.out.println("API Key Match");
38-
break;
39-
}
35+
if (apiKey2!=null)
36+
for (int i = 0; i < getAPIKEYs().length; i++)
37+
if (apiKey2.contentEquals(getAPIKEYs()[i])) {
38+
APIPass = true;
39+
System.out.println("API Key Match");
40+
break;
41+
}
4042
}
4143

4244
// Process the request
@@ -53,7 +55,7 @@ public void run() {
5355
} else {
5456
response = new CSGResponse();
5557
response.setState(ServerActionState.BADAPIKEY);
56-
response.setMessage("Your API key " + request.getAPIKey() + " Does not match server's key");
58+
response.setMessage("Your API key " + apiKey2 + " Does not match server's key");
5759
}
5860
// Send back the response
5961
oos.writeObject(response);

0 commit comments

Comments
 (0)