Skip to content

Ianafiles #8

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

Merged
merged 9 commits into from
Aug 27, 2020
Merged
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ Note that this is a system property and is not part of the `resouce_files.proper
git clone https://github.com/arineng/rdap_bootstrap_server.git
cd rdap_bootstrap_server
git checkout springboot
./gradlew clean build test bootRun --info
./gradlew clean build test --info
./gradlew bootRun [-Dproperty=value ...] --info

### Sample Queries

Expand Down Expand Up @@ -307,3 +308,19 @@ entities. The `/help` query returns statistics for ARIN RDAP Bootstrap service.
#### /help

http://localhost:8080/rdapbootstrap/help (200 returning ARIN RDAP Bootstrap service statistics)

### System Properties

arin.rdapbootstrap.match_scheme_on_redirect=false (default) | true
arin.rdapbootstrap.download_bootstrap_files=false (default) | true
arin.rdapbootstrap.download_asn_file_url=URL
arin.rdapbootstrap.download_domain_file_url=URL
arin.rdapbootstrap.download_ipv4_file_url=URL
arin.rdapbootstrap.download_ipv6_file_url=URL
arin.rdapbootstrap.download_directory=FULL_DIRECTORY_PATH
arin.rdapbootstrap.download_interval=86400 (default) (in seconds)
arin.rdapbootstrap.bootfile.domain_bootstrap=FULL_FILE_PATH
arin.rdapbootstrap.bootfile.v4_bootstrap=FULL_FILE_PATH
arin.rdapbootstrap.bootfile.v6_bootstrap=FULL_FILE_PATH
arin.rdapbootstrap.bootfile.as_bootstrap=FULL_FILE_PATH
arin.rdapbootstrap.bootfile.entity_bootstrap=FULL_FILE_PATH
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation group: 'com.googlecode.java-ipv6', name: 'java-ipv6', version:'0.17'
implementation group: 'net.ripe.ipresource', name: 'ipresource', version:'1.47'
implementation group: 'commons-io', name: 'commons-io', version: '2.7'

testImplementation group: 'junit', name: 'junit', version:'4.8.2'
}

bootJar {
mainClassName = 'net.arin.rdap_bootstrap.service.RdapBootstrapApp'
}

bootRun {
systemProperties System.properties
}
232 changes: 232 additions & 0 deletions src/main/java/net/arin/rdap_bootstrap/AppProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
* Copyright (C) 2020 American Registry for Internet Numbers (ARIN)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package net.arin.rdap_bootstrap;

import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;

import java.io.File;
import java.math.BigDecimal;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class AppProperties
{
protected AppProperties()
{
}

public static final String PLACE_VALUE_HERE = "***PLACE_VALUE_HERE***";

private static final boolean allowForDefaultValues = true;

private static PropertyResolver resolver = new StandardEnvironment();

public static void updateResolver( PropertyResolver resolver )
{
if ( resolver == null )
{
throw new IllegalArgumentException( "Resolver cannot be null" );
}

AppProperties.resolver = resolver;
}

public static String getProperty( String name )
{
return resolver.containsProperty( name ) ? resolver.getProperty( name ) : null;
}

public static String getProperty( String name, String defaultValue )
{
return resolver.containsProperty( name ) ? resolver.getProperty( name ) : defaultValue;
}

public static void bind( String name, Object value )
{
System.setProperty( name, value.toString() );
}

public static void unbind( String name )
{
System.clearProperty( name );
}

public static Integer lookupInteger( String name )
{
return lookup( Integer.class, name );
}

public static Long lookupLong( String name )
{
return lookup( Long.class, name );
}

public static Long lookupLong( String name, long defaultValue )
{
return lookup( Long.class, name, defaultValue );
}

public static BigDecimal lookupBigDecimal( String name )
{
return lookup( BigDecimal.class, name );
}

public static String lookupString( String name )
{
return lookup( String.class, name );
}

public static Boolean lookupBoolean( String name )
{
return lookup( Boolean.class, name );
}

public static Integer lookupInteger( String name, int defaultValue )
{
return lookup( Integer.class, name, defaultValue );
}

public static String lookupString( String name, String defaultValue )
{
return lookup( String.class, name, defaultValue );
}

public static String lookupDirectory( String name )
{
String directory = lookupString( name );
return directory.endsWith( File.separator ) ? directory : directory + File.separator;
}

public static Boolean lookupBoolean( String name, Boolean defaultValue )
{
return lookup( Boolean.class, name, defaultValue );
}

public static <T> T lookup( Class<T> clazz, String name )
{
String value = getProperty( name, false, allowForDefaultValues );
return parseValue( value, clazz, name );
}

public static <T> T lookup( Class<T> clazz, String name, T defaultValue )
{
String value = getProperty( name, true, allowForDefaultValues );
if ( value == null )
{
return defaultValue;
}

return parseValue( value, clazz, name );
}

public static <T> T lookupForceAllowDefault( Class<T> clazz, String name, T defaultValue )
{
String value = getProperty( name, true, true );
if ( value == null )
{
return defaultValue;
}

return parseValue( value, clazz, name );
}

private static String getProperty( String name, boolean nullable, boolean allowForDefaultValues )
{
String value = getProperty( name );

if ( value == null )
{
if ( !nullable )
{
throw new RuntimeException( "System property '" + name + "' not found." );
}
if ( !allowForDefaultValues )
{
throw new RuntimeException( "System property '" + name + "' not found and default values are not allowed." );
}
}
else if ( value.equalsIgnoreCase( PLACE_VALUE_HERE ) )
{
if ( !nullable )
{
throw new RuntimeException( "System property '" + name + "' is '" + PLACE_VALUE_HERE + "' (i.e., not set)" );
}
if ( !allowForDefaultValues )
{
throw new RuntimeException( "System property '" + name + "' is '" + PLACE_VALUE_HERE + "' (i.e., not set) and default values are not allowed." );
}
}

return value;
}

@SuppressWarnings( "unchecked" )
private static <T> T parseValue( String value, Class<T> clazz, String name )
{
if ( clazz == String.class )
{
return ( T ) value;
}
else if ( clazz == Boolean.class )
{
if ( value.equalsIgnoreCase( Boolean.TRUE.toString() ) )
{
return ( T ) Boolean.TRUE;
}
else if ( value.equalsIgnoreCase( Boolean.FALSE.toString() ) )
{
return ( T ) Boolean.FALSE;
}
else
{
throw new RuntimeException( "System property '" + name + "' is not a boolean value" );
}
}
else if ( clazz == Integer.class )
{
return ( T ) Integer.decode( value );
}
else if ( clazz == Long.class )
{
return ( T ) Long.decode( removeEndingL( value ) );
}
else if ( clazz == BigDecimal.class )
{
return ( T ) new BigDecimal( removeEndingL( value ) );
}
else
{
throw new UnsupportedOperationException( "System property of " + clazz + " type is not supported" );
}
}

private static String removeEndingL( String value )
{
String newValue = value.trim();
return newValue.endsWith( "L" ) ? newValue.substring( 0, newValue.length() - 1 ) : newValue;
}

public static SortedMap<String, String> getSystemProperties( String prefix )
{
return System.getProperties().keySet().stream()
.map( Object::toString )
.filter( name -> name.startsWith( prefix ) )
.collect( Collectors.toMap( Function.identity(), AppProperties::getProperty, ( v1, v2 ) -> v1, TreeMap::new ) );
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/arin/rdap_bootstrap/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@
public class Constants
{
public final static String PROPERTY_PREFIX = "arin.rdapbootstrap.";

public final static String MATCH_SCHEME_ON_REDIRECT_PROPERTY = "arin.rdapbootstrap.match_scheme_on_redirect";
public final static String DOWNLOAD_BOOTSTRAP_FILES_PROPERTY = "arin.rdapbootstrap.download_bootstrap_files";
public final static String DOWNLOAD_ASN_FILE_URL_PROPERTY = "arin.rdapbootstrap.download_asn_file_url";
public final static String DOWNLOAD_DOMAIN_FILE_URL_PROPERTY = "arin.rdapbootstrap.download_domain_file_url";
public final static String DOWNLOAD_IPV4_FILE_URL_PROPERTY = "arin.rdapbootstrap.download_ipv4_file_url";
public final static String DOWNLOAD_IPV6_FILE_URL_PROPERTY = "arin.rdapbootstrap.download_ipv6_file_url";
public final static String DOWNLOAD_DIRECTORY_PROPERTY = "arin.rdapbootstrap.download_directory";
public final static String DOWNLOAD_INTERVAL_PROPERTY = "arin.rdapbootstrap.download_interval";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static ServletRegistrationBean<Servlet> rdapBootstrapRedirectServlet() th
ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>();
registrationBean.setServlet( ( Servlet ) Class.forName( "net.arin.rdap_bootstrap.service.RedirectServlet" ).getConstructor().newInstance() );
registrationBean.addUrlMappings( "/rdapbootstrap/*" );
registrationBean.setLoadOnStartup( 1 );
return registrationBean;
}
}
Loading