Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to select wildcard rule #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public final class PublicSuffixListFactory {
*/
public static final String PROPERTY_INDEX_FACTORY = "psl.indexFactory";

/**
* Use wildcard rule.
*/
public static final String PROPERTY_USE_WILDCARD = "psl.use.wildcard";

/**
* Location of the default properties.
*
Expand Down Expand Up @@ -124,7 +129,7 @@ public PublicSuffixList build(final Properties properties) throws IOException, C

/**
* Builds a PublicSuffixList.
*
*
* @param list
* The list.
* @return a public suffix list
Expand All @@ -142,7 +147,7 @@ public PublicSuffixList build(InputStream list) throws IOException {

/**
* Downloads the public suffix list.
*
*
* @return a public suffix list
* @throws IOException
* If the list can't be downloaded.
Expand All @@ -168,25 +173,27 @@ public PublicSuffixList download() throws IOException {

/**
* Builds a PublicSuffixList.
*
*
* @param list
* The list.
* @return a public suffix list
*/
private PublicSuffixList build(InputStream list, Properties properties)
public PublicSuffixList build(InputStream list, Properties properties)
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
URL url = new URL(properties.getProperty(PROPERTY_URL));

Charset charset = Charset.forName(properties.getProperty(PROPERTY_CHARSET));

IndexFactory indexFactory = loadIndexFactory(properties.getProperty(PROPERTY_INDEX_FACTORY));

return build(list, url, charset, indexFactory);
boolean useDefaultRule = Boolean.parseBoolean(properties.getProperty(PROPERTY_USE_WILDCARD));

return build(list, url, charset, indexFactory, useDefaultRule);
}

/**
* Loads the index factory.
*
*
* @param indexFactoryClassName
* the class name of the index factory.
* @return the index factory
Expand Down Expand Up @@ -218,7 +225,7 @@ public PublicSuffixList build() {

/**
* Builds a PublicSuffixList.
*
*
* @param list
* The list.
* @param url
Expand All @@ -233,12 +240,14 @@ public PublicSuffixList build() {
* The list could not be read.
*/
private PublicSuffixList build(final InputStream list, final URL url, final Charset charset,
final IndexFactory indexFactory) throws IOException {
final IndexFactory indexFactory, boolean useDefaultRule) throws IOException {
Parser parser = new Parser();
List<Rule> rules = parser.parse(list, charset);

// add default rule
rules.add(Rule.DEFAULT);
if (useDefaultRule) {
rules.add(Rule.DEFAULT);
}

Index index = indexFactory.build(rules);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/psl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ psl.url = https://publicsuffix.org/list/effective_tld_names.dat
psl.file = /effective_tld_names.dat
psl.charset = UTF-8
psl.indexFactory = de.malkusch.whoisServerList.publicSuffixList.index.tree.TreeIndexFactory
psl.use.wildcard = true
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Properties;

import org.junit.Test;

Expand Down Expand Up @@ -36,4 +37,22 @@ public void testExample() throws IOException {
assertEquals("xn--85x722f.com.cn", suffixList.getRegistrableDomain("xn--85x722f.com.cn"));
}

@Test
public void testExampleWithoutWildcardRule() throws IOException, ClassNotFoundException {
PublicSuffixListFactory factory = new PublicSuffixListFactory();
Properties properties = factory.getDefaults();
properties.setProperty("psl.use.wildcard", "false");
PublicSuffixList suffixList = factory.build(properties);

assertTrue(suffixList.isPublicSuffix("net"));
assertFalse(suffixList.isPublicSuffix("bad"));

assertNull(suffixList.getRegistrableDomain("net"));
assertNull(suffixList.getRegistrableDomain("example.example"));
assertNull(suffixList.getRegistrableDomain("bad.bad"));
assertEquals("example.net", suffixList.getRegistrableDomain("www.example.net"));
assertEquals("example.co.uk", suffixList.getRegistrableDomain("example.co.uk"));
assertEquals("example.co.uk", suffixList.getRegistrableDomain("www.example.co.uk"));
}

}