Skip to content

Commit

Permalink
Implement PublicSuffixListFactory.download
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Mar 16, 2016
1 parent 8bf791c commit c2d3f8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.malkusch.whois-server-list</groupId>
<artifactId>public-suffix-list</artifactId>
<version>2.1.1-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<name>Public Suffix List</name>
<description>Public Suffix List API</description>

Expand Down Expand Up @@ -106,4 +106,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
Expand Down Expand Up @@ -78,8 +79,7 @@ public final class PublicSuffixListFactory {
* @return the default properties
*/
public Properties getDefaults() {
try (InputStream stream = getClass()
.getResourceAsStream(PROPERTY_FILE)) {
try (InputStream stream = getClass().getResourceAsStream(PROPERTY_FILE)) {

Properties properties = new Properties();
properties.load(stream);
Expand Down Expand Up @@ -110,12 +110,10 @@ public Properties getDefaults() {
*
* @see #getDefaults()
*/
public PublicSuffixList build(final Properties properties)
throws IOException, ClassNotFoundException {
public PublicSuffixList build(final Properties properties) throws IOException, ClassNotFoundException {

String propertyFile = properties.getProperty(PROPERTY_LIST_FILE);
try (InputStream listStream = getClass()
.getResourceAsStream(propertyFile)) {
try (InputStream listStream = getClass().getResourceAsStream(propertyFile)) {

return build(listStream, properties);

Expand All @@ -136,13 +134,38 @@ public PublicSuffixList build(final Properties properties)
public PublicSuffixList build(InputStream list) throws IOException {
try {
return build(list, getDefaults());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {

throw new RuntimeException(e);
}
}

/**
* Downloads the public suffix list.
*
* @return a public suffix list
* @throws IOException
* If the list can't be downloaded.
*/
public PublicSuffixList download() throws IOException {
Properties properties = getDefaults();

URL url;
try {
url = new URL(properties.getProperty(PROPERTY_URL));

} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}

try (InputStream listStream = url.openStream()) {
return build(listStream, properties);

} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

/**
* Builds a PublicSuffixList.
*
Expand All @@ -151,15 +174,12 @@ public PublicSuffixList build(InputStream list) throws IOException {
* @return a public suffix list
*/
private PublicSuffixList build(InputStream list, Properties properties)
throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
URL url = new URL(properties.getProperty(PROPERTY_URL));

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

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

return build(list, url, charset, indexFactory);
}
Expand All @@ -172,11 +192,9 @@ private PublicSuffixList build(InputStream list, Properties properties)
* @return the index factory
*/
private IndexFactory loadIndexFactory(String indexFactoryClassName)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
@SuppressWarnings("unchecked")
Class<IndexFactory> indexFactoryClass = (Class<IndexFactory>) Class
.forName(indexFactoryClassName);
Class<IndexFactory> indexFactoryClass = (Class<IndexFactory>) Class.forName(indexFactoryClassName);
return indexFactoryClass.newInstance();
}

Expand Down Expand Up @@ -214,9 +232,8 @@ public PublicSuffixList build() {
* @throws IOException
* The list could not be read.
*/
private PublicSuffixList build(final InputStream list, final URL url,
final Charset charset, final IndexFactory indexFactory)
throws IOException {
private PublicSuffixList build(final InputStream list, final URL url, final Charset charset,
final IndexFactory indexFactory) throws IOException {
Parser parser = new Parser();
List<Rule> rules = parser.parse(list, charset);

Expand Down

0 comments on commit c2d3f8a

Please sign in to comment.