Skip to content

Commit

Permalink
Issue 598: Update Syn configuration root element (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f authored and whikloj committed Apr 21, 2017
1 parent 274595d commit 99ed831
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions conf/syn-settings.example.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<sites version='1'>
<config version='1'>
<!--
Sites can be specified with a Key inline, or with a reference to a key
stored in a file. Both are shown in examples below.
Expand Down Expand Up @@ -30,4 +30,4 @@ my secret key
my super secret token
</token>

</sites>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;

public class Sites {
public class Config {
private int version = -1;
private List<Site> sites = new ArrayList<>();
private List<Token> tokens = new ArrayList<>();
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/ca/islandora/syn/settings/SettingsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ private static Digester getDigester() {
if (digester == null) {
digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate("sites", "ca.islandora.syn.settings.Sites");
digester.addSetProperties("sites");
digester.addObjectCreate("sites/site", "ca.islandora.syn.settings.Site");
digester.addSetProperties("sites/site");
digester.addCallMethod("sites/site", "setKey", 0);
digester.addSetNext("sites/site", "addSite", "ca.islandora.syn.settings.Site");
digester.addObjectCreate("sites/token", "ca.islandora.syn.settings.Token");
digester.addSetProperties("sites/token");
digester.addCallMethod("sites/token", "setToken", 0);
digester.addSetNext("sites/token", "addToken", "ca.islandora.syn.settings.Token");
digester.addObjectCreate("config", "ca.islandora.syn.settings.Config");
digester.addSetProperties("config");
digester.addObjectCreate("config/site", "ca.islandora.syn.settings.Site");
digester.addSetProperties("config/site");
digester.addCallMethod("config/site", "setKey", 0);
digester.addSetNext("config/site", "addSite", "ca.islandora.syn.settings.Site");
digester.addObjectCreate("config/token", "ca.islandora.syn.settings.Token");
digester.addSetProperties("config/token");
digester.addCallMethod("config/token", "setToken", 0);
digester.addSetNext("config/token", "addToken", "ca.islandora.syn.settings.Token");
}
return digester;
}
Expand Down Expand Up @@ -172,8 +172,8 @@ private static Algorithm getHmacAlgorithm(final Site site) {
}
}

private static Sites getSites(final InputStream settings) {
Sites sites;
private static Config getSites(final InputStream settings) {
Config sites;

try {
sites = getSitesObject(settings);
Expand All @@ -192,7 +192,7 @@ private static Sites getSites(final InputStream settings) {

public static Map<String, Algorithm> getSiteAlgorithms(final InputStream settings) {
final Map<String, Algorithm> algorithms = new HashMap<>();
final Sites sites = getSites(settings);
final Config sites = getSites(settings);
if (sites == null) {
return algorithms;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public static Map<String, Algorithm> getSiteAlgorithms(final InputStream setting

public static Map<String, Token> getSiteStaticTokens(final InputStream settings) {
final Map<String, Token> tokens = new HashMap<>();
final Sites sites = getSites(settings);
final Config sites = getSites(settings);
if (sites == null) {
return tokens;
}
Expand All @@ -267,8 +267,8 @@ public static Map<String, Token> getSiteStaticTokens(final InputStream settings)
return tokens;
}

static Sites getSitesObject(final InputStream settings)
static Config getSitesObject(final InputStream settings)
throws IOException, SAXException {
return (Sites) getDigester().parse(settings);
return (Config) getDigester().parse(settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class SettingsParserAlgorithmsTest {

private void testOneSiteHmacInlineKey(final String algorithm) throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='" + algorithm + "' encoding='plain'>"
, " test data"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -42,11 +42,11 @@ public void testOneSiteAllHmacInlineKey() throws Exception {
@Test
public void testInvalidSitesVersion() throws Exception {
final String testXml = String.join("\n"
, "<sites version='2'>"
, "<config version='2'>"
, " <site url='http://test.com' algorithm='HS384' encoding='plain'>"
, " test data"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -57,11 +57,11 @@ public void testInvalidSitesVersion() throws Exception {
@Test
public void testOneSiteHmacBase64() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS256' encoding='base64'>"
, " am9uYXRoYW4gaXMgYXdlc29tZQ=="
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -72,11 +72,11 @@ public void testOneSiteHmacBase64() throws Exception {
@Test
public void testOneSiteHmacInvalidBase64() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS256' encoding='base64'>"
, " this is invalid base64"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -87,11 +87,11 @@ public void testOneSiteHmacInvalidBase64() throws Exception {
@Test
public void testOneSiteHmacInvalidEncoding() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS256' encoding='badalgorithm'>"
, " this is invalid base64"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -104,9 +104,9 @@ private void testOneSiteHmacFileKey(final String algorithm) throws Exception {
final String path = key.getAbsolutePath();

final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='" + algorithm + "' encoding='plain' path='" + path + "'/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -126,11 +126,11 @@ public void testOneSiteAllHmacFileKey() throws Exception {
@Test
public void testSiteBothInlineAndPath() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS384' encoding='plain' path='foo'>"
, " test data"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -141,9 +141,9 @@ public void testSiteBothInlineAndPath() throws Exception {
@Test
public void testSiteNeitherInlineAndPath() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS384' encoding='plain'/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -154,9 +154,9 @@ public void testSiteNeitherInlineAndPath() throws Exception {
@Test
public void testSiteInvalidPath() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='HS384' encoding='plain' path='foo'/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -167,11 +167,11 @@ public void testSiteInvalidPath() throws Exception {
@Test
public void testSiteNoUrlDefault() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site algorithm='HS256' encoding='plain' default='true'>"
, " test data"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -181,7 +181,7 @@ public void testSiteNoUrlDefault() throws Exception {

private void testOneSiteRsaInlineKey(final String algorithm) throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='" + algorithm + "' encoding='PEM'>"
, "-----BEGIN PUBLIC KEY-----"
, "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEVO4MNlZG+iGYhoJd/cBpfMd9"
Expand All @@ -190,7 +190,7 @@ private void testOneSiteRsaInlineKey(final String algorithm) throws Exception {
, "KOT4nEF7MBGyOSP3KQIDAQAB"
, "-----END PUBLIC KEY-----"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand Down Expand Up @@ -223,9 +223,9 @@ private void testOneSiteRsaFileKey(final String algorithm) throws Exception {
Files.write(Paths.get(path), pemPublicKey.getBytes());

final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='" + algorithm + "' encoding='PEM' path='" + path + "'/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand All @@ -245,11 +245,11 @@ public void testOneSiteAllRsaFileKey() throws Exception {
@Test
public void testOneSiteAllRsaInvalidEncoding() throws Exception {
final String testXml = String.join("\n"
, "<sites version='1'>"
, "<config version='1'>"
, " <site url='http://test.com' algorithm='RS256' encoding='PEM'>"
, "-----BEGIN PUBLIC KEY-----"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class SettingsParserDigestTest {
@Test
public void testOneSitePath() throws Exception {
final String testXml = String.join("\n"
, "<sites version=\"12\">"
, "<config version=\"12\">"
, " <site url=\"http://test.com\" algorithm=\"RS384\" path=\"test/path.key\" encoding=\"PEM\"/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
final Sites settings = SettingsParser.getSitesObject(stream);
final Config settings = SettingsParser.getSitesObject(stream);
assertEquals(12, settings.getVersion());
assertEquals(1, settings.getSites().size());

Expand All @@ -37,16 +37,16 @@ public void testOneSitePath() throws Exception {
@Test
public void testOneSiteKey() throws Exception {
final String testXml = String.join("\n"
, "<sites>"
, "<config>"
, " <site url=\"http://test.com\" algorithm=\"RS384\" encoding=\"PEM\" default=\"true\">"
, "multiline"
, "key"
, " </site>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
final Sites settings = SettingsParser.getSitesObject(stream);
final Config settings = SettingsParser.getSitesObject(stream);
assertEquals(-1, settings.getVersion());
assertEquals(1, settings.getSites().size());

Expand All @@ -62,38 +62,38 @@ public void testOneSiteKey() throws Exception {
@Test
public void testTwoSites() throws Exception {
final String testXml = String.join("\n"
, "<sites>"
, "<config>"
, " <site/>"
, " <site/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
final Sites settings = SettingsParser.getSitesObject(stream);
final Config settings = SettingsParser.getSitesObject(stream);
assertEquals(2, settings.getSites().size());
}

@Test
public void testOneSiteUnexpectedAttribute() throws Exception {
final String testXml = String.join("\n"
, "<sites>"
, "<config>"
, " <site unexpected=\"woh\"/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
final Sites settings = SettingsParser.getSitesObject(stream);
final Config settings = SettingsParser.getSitesObject(stream);
}

@Test
public void testOneSiteUnexpectedTag() throws Exception {
final String testXml = String.join("\n"
, "<sites>"
, "<config>"
, " <islandora/>"
, "</sites>"
, "</config>"
);

final InputStream stream = new ByteArrayInputStream(testXml.getBytes());
final Sites settings = SettingsParser.getSitesObject(stream);
final Config settings = SettingsParser.getSitesObject(stream);
}
}
Loading

0 comments on commit 99ed831

Please sign in to comment.