@ -19,6 +19,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package net.yacy.htroot.yacy ;
import java.util.ArrayList ;
import java.util.Map ;
import java.util.Set ;
@ -45,38 +47,38 @@ import net.yacy.server.serverSwitch;
public final class seedlist {
private static final int LISTMAX = 1000 ;
public static serverObjects respond ( final RequestHeader header , final serverObjects post , final serverSwitch env ) {
// return variable that accumulates replacements
final Switchboard sb = ( Switchboard ) env ;
int maxcount = Math . min ( LISTMAX , post = = null ? Integer . MAX_VALUE : post . getInt ( "maxcount" , Integer . MAX_VALUE ) ) ;
float minversion = Math . min ( LISTMAX , post = = null ? 0.0f : post . getFloat ( "minversion" , 0.0f ) ) ;
boolean nodeonly = post = = null | | ! post . containsKey ( "node" ) ? false : post . getBoolean ( "node" ) ;
boolean includeme = post = = null | | ! post . containsKey ( "me" ) ? true : post . getBoolean ( "me" ) ;
boolean addressonly = post = = null | | ! post . containsKey ( "address" ) ? false : post . getBoolean ( "address" ) ;
String peername = post = = null ? null : post . containsKey ( "my" ) ? sb . peers . myName ( ) : post . get ( "peername" ) ;
final int maxcount = Math . min ( LISTMAX , post = = null ? Integer . MAX_VALUE : post . getInt ( "maxcount" , Integer . MAX_VALUE ) ) ;
final float minversion = Math . min ( LISTMAX , post = = null ? 0.0f : post . getFloat ( "minversion" , 0.0f ) ) ;
final boolean nodeonly = post = = null | | ! post . containsKey ( "node" ) ? false : post . getBoolean ( "node" ) ;
final boolean includeme = post = = null | | ! post . containsKey ( "me" ) ? true : post . getBoolean ( "me" ) ;
final boolean addressonly = post = = null | | ! post . containsKey ( "address" ) ? false : post . getBoolean ( "address" ) ;
final String peername = post = = null ? null : post . containsKey ( "my" ) ? sb . peers . myName ( ) : post . get ( "peername" ) ;
final ArrayList < Seed > v ;
if ( post ! = null & & post . containsKey ( "my" ) ) {
v = new ArrayList < Seed > ( 1 ) ;
v . add ( sb . peers . mySeed ( ) ) ;
} else if ( post ! = null & & post . containsKey ( "id" ) ) {
v = new ArrayList < Seed > ( 1 ) ;
Seed s = sb . peers . get ( post . get ( "id" ) ) ;
final Seed s = sb . peers . get ( post . get ( "id" ) ) ;
if ( s ! = null ) v . add ( s ) ;
} else if ( post ! = null & & post . containsKey ( "name" ) ) {
v = new ArrayList < Seed > ( 1 ) ;
Seed s = sb . peers . lookupByName ( post . get ( "name" ) ) ;
final Seed s = sb . peers . lookupByName ( post . get ( "name" ) ) ;
if ( s ! = null ) v . add ( s ) ;
} else {
v = sb . peers . getSeedlist ( maxcount , includeme , nodeonly , minversion ) ;
}
final serverObjects prop = new serverObjects ( ) ;
// write simple-encoded seed lines or json
String EXT = header . get ( HeaderFramework . CONNECTION_PROP_EXT ) ;
boolean json = EXT ! = null & & EXT . equals ( "json" ) ;
boolean xml = EXT ! = null & & EXT . equals ( "xml" ) ;
final String EXT = header . get ( HeaderFramework . CONNECTION_PROP_EXT ) ;
final boolean json = EXT ! = null & & EXT . equals ( "json" ) ;
final boolean xml = EXT ! = null & & EXT . equals ( "xml" ) ;
if ( json ) {
// check for JSONP
if ( post ! = null & & post . containsKey ( "callback" ) ) {
@ -89,17 +91,17 @@ public final class seedlist {
// construct json property lists
int count = 0 ;
for ( int i = 0 ; i < v . size ( ) ; i + + ) {
Seed seed = v . get ( i ) ;
final Seed seed = v . get ( i ) ;
if ( peername ! = null & & ! peername . equals ( seed . getName ( ) ) ) continue ;
Set < String > ips = seed . getIPs ( ) ;
final Set < String > ips = seed . getIPs ( ) ;
if ( ips = = null | | ips . size ( ) = = 0 ) continue ;
prop . putJSON ( "peers_" + count + "_map_0_k" , Seed . HASH ) ;
prop . put ( "peers_" + count + "_map_0_v" , JSONObject . quote ( seed . hash ) ) ;
prop . put ( "peers_" + count + "_map_0_c" , 1 ) ;
Map < String , String > map = seed . getMap ( ) ;
final Map < String , String > map = seed . getMap ( ) ;
int c = 1 ;
if ( ! addressonly ) {
for ( Map . Entry < String , String > m : map . entrySet ( ) ) {
for ( final Map . Entry < String , String > m : map . entrySet ( ) ) {
prop . putJSON ( "peers_" + count + "_map_" + c + "_k" , m . getKey ( ) ) ;
prop . put ( "peers_" + count + "_map_" + c + "_v" , JSONObject . quote ( m . getValue ( ) ) ) ;
prop . put ( "peers_" + count + "_map_" + c + "_c" , 1 ) ;
@ -107,9 +109,9 @@ public final class seedlist {
}
}
// construct a list of ips
StringBuilder a = new StringBuilder ( ) ;
final StringBuilder a = new StringBuilder ( ) ;
a . append ( '[' ) ;
for ( String ip : ips ) a . append ( JSONObject . quote ( seed . getPublicAddress ( ip ) ) ) . append ( ',' ) ;
for ( final String ip : ips ) a . append ( JSONObject . quote ( seed . getPublicAddress ( ip ) ) ) . append ( ',' ) ;
a . setCharAt ( a . length ( ) - 1 , ']' ) ;
prop . putJSON ( "peers_" + count + "_map_" + c + "_k" , "Address" ) ;
prop . put ( "peers_" + count + "_map_" + c + "_v" , a . toString ( ) ) ;
@ -124,22 +126,22 @@ public final class seedlist {
} else if ( xml ) {
int count = 0 ;
for ( int i = 0 ; i < v . size ( ) ; i + + ) {
Seed seed = v . get ( i ) ;
final Seed seed = v . get ( i ) ;
if ( peername ! = null & & ! peername . equals ( seed . getName ( ) ) ) continue ;
Set < String > ips = seed . getIPs ( ) ;
final Set < String > ips = seed . getIPs ( ) ;
if ( ips = = null | | ips . size ( ) = = 0 ) continue ;
prop . putXML ( "peers_" + count + "_map_0_k" , Seed . HASH ) ;
prop . putXML ( "peers_" + count + "_map_0_v" , seed . hash ) ;
Map < String , String > map = seed . getMap ( ) ;
final Map < String , String > map = seed . getMap ( ) ;
int c = 1 ;
if ( ! addressonly ) {
for ( Map . Entry < String , String > m : map . entrySet ( ) ) {
for ( final Map . Entry < String , String > m : map . entrySet ( ) ) {
prop . putXML ( "peers_" + count + "_map_" + c + "_k" , m . getKey ( ) ) ;
prop . putXML ( "peers_" + count + "_map_" + c + "_v" , m . getValue ( ) ) ;
c + + ;
}
}
for ( String ip : ips ) {
for ( final String ip : ips ) {
prop . putXML ( "peers_" + count + "_map_" + c + "_k" , "Address" ) ;
prop . putXML ( "peers_" + count + "_map_" + c + "_v" , seed . getPublicAddress ( ip ) ) ;
c + + ;
@ -147,17 +149,17 @@ public final class seedlist {
prop . put ( "peers_" + count + "_map" , c ) ;
count + + ;
}
prop . put ( "peers_" + ( count - 1 ) + "_c" , 0 ) ;
prop . put ( "peers" , count ) ;
} else {
final StringBuilder encoded = new StringBuilder ( 1024 ) ;
for ( Seed seed : v ) {
for ( final Seed seed : v ) {
encoded . append ( seed . genSeedStr ( null ) ) . append ( serverCore . CRLF_STRING ) ;
}
}
prop . put ( "encoded" , encoded . toString ( ) ) ;
}
// return rewrite properties
return prop ;
}