Commit 7c45e636 authored by Chris Coughlin's avatar Chris Coughlin

Merge remote-tracking branch 'origin/master'

parents 71a3965f 37629a8f
......@@ -27,6 +27,7 @@ import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.json.JSONObject;
import java.io.Closeable;
......@@ -83,6 +84,12 @@ public class RESTROIFinder implements ROIFinder, AutoCloseable {
*/
private Gson gson;
/**
* Save credentials flag - if true basic auth username and password are serialized. Default is false, i.e.
* credentials are _not_ saved.
*/
private boolean saveCredentials = false;
/**
* Constructor
* @param url REST API endpoint e.g. http://127.0.0.1:8080/api/isroi
......@@ -150,8 +157,11 @@ public class RESTROIFinder implements ROIFinder, AutoCloseable {
kryo.writeObject(output, getUrl());
kryo.writeObject(output, getConnectionTimeout());
kryo.writeObject(output, getSocketTimeout());
kryo.writeObject(output, getUsername());
kryo.writeObject(output, getPassword());
kryo.writeObject(output, saveCredentials);
if (saveCredentials) {
kryo.writeObject(output, getUsername());
kryo.writeObject(output, getPassword());
}
}
@Override
......@@ -159,8 +169,11 @@ public class RESTROIFinder implements ROIFinder, AutoCloseable {
setUrl(kryo.readObject(input, String.class));
setConnectionTimeout(kryo.readObject(input, long.class));
setSocketTimeout(kryo.readObject(input, long.class));
setUsername(kryo.readObject(input, String.class));
setPassword(kryo.readObject(input, String.class));
setSaveCredentials(kryo.readObject(input, boolean.class));
if (saveCredentials) {
setUsername(kryo.readObject(input, String.class));
setPassword(kryo.readObject(input, String.class));
}
}
/**
......@@ -294,4 +307,29 @@ public class RESTROIFinder implements ROIFinder, AutoCloseable {
byte[] asBytes = Base64.getDecoder().decode(enc);
return gson.fromJson(new String(asBytes, "utf-8"), double[].class);
}
/**
* Sets the HTTPClient implementation to use.
* @param newClient new client
*/
public void setHttpClient(HttpClient newClient) {
Unirest.setHttpClient(newClient);
}
/**
* Reports the current basic auth serialization settings.
* @return true if crentidals are serialized, false otherwise.
*/
public boolean saveCredentials() {
return saveCredentials;
}
/**
* Sets whether or not to save basic auth credentials during serialization. Default is to not save credentials -
* please consider carefully the security ramifications before enabling this feature.
* @param save true to save username and password, false otherwise.
*/
public void setSaveCredentials(boolean save) {
saveCredentials = save;
}
}
......@@ -24,6 +24,7 @@ import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* RESTROIFinderTest - tests the REST API ROI finder implementation.
......@@ -43,7 +44,20 @@ public class RESTROIFinderTest {
}
@Test
public void serialize() throws Exception {
public void serialize_with_creds() throws Exception {
File out = File.createTempFile("tmp_rst", "dat");
finder.setSaveCredentials(true);
finder.save(out);
RESTROIFinder read = new RESTROIFinder();
read.load(out);
assertModelsEqual(finder, read);
RESTROIFinder read2 = (RESTROIFinder) ROIFinder.fromFile(out, RESTROIFinder.class);
assertModelsEqual(finder, read2);
}
@Test
public void serialize_no_creds() throws Exception {
File out = File.createTempFile("tmp_rst", "dat");
finder.save(out);
RESTROIFinder read = new RESTROIFinder();
......@@ -58,8 +72,14 @@ public class RESTROIFinderTest {
assertEquals(expected.getUrl(), actual.getUrl());
assertEquals(expected.getConnectionTimeout(), actual.getConnectionTimeout());
assertEquals(expected.getSocketTimeout(), actual.getSocketTimeout());
assertEquals(expected.getUsername(), actual.getUsername());
assertEquals(expected.getPassword(), actual.getPassword());
assertEquals(expected.saveCredentials(), actual.saveCredentials());
if (expected.saveCredentials()) {
assertEquals(expected.getUsername(), actual.getUsername());
assertEquals(expected.getPassword(), actual.getPassword());
} else {
assertNotEquals(expected.getUsername(), actual.getUsername());
assertNotEquals(expected.getPassword(), actual.getPassword());
}
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment