2005-10-05 15:16:53 +02:00
// query.java
2005-04-07 21:19:42 +02:00
// -----------------------
// part of the AnomicHTTPD caching proxy
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2005-04-07 21:19:42 +02:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
2005-10-05 15:16:53 +02:00
//
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
2005-04-07 21:19:42 +02:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2005-05-12 19:50:45 +02:00
// You must compile this file with
2005-10-05 15:16:53 +02:00
// javac -classpath .:../../classes query.java
2005-04-07 21:19:42 +02:00
// if the shell's current path is HTROOT
2009-03-16 01:18:37 +01:00
import java.io.IOException ;
2011-10-04 11:06:24 +02:00
2011-01-03 21:52:54 +01:00
import net.yacy.cora.date.GenericFormatter ;
2010-08-23 14:32:02 +02:00
import net.yacy.cora.protocol.HeaderFramework ;
import net.yacy.cora.protocol.RequestHeader ;
2013-07-09 14:28:25 +02:00
import net.yacy.cora.util.ConcurrentLog ;
2011-10-04 11:06:24 +02:00
import net.yacy.peers.Network ;
2012-06-28 14:27:29 +02:00
import net.yacy.peers.Protocol ;
2011-09-25 18:59:06 +02:00
import net.yacy.search.Switchboard ;
2012-09-21 15:48:16 +02:00
import net.yacy.server.serverObjects ;
import net.yacy.server.serverSwitch ;
2005-04-07 21:19:42 +02:00
2005-10-05 12:45:33 +02:00
public final class query {
2005-04-07 21:19:42 +02:00
2008-06-05 14:52:27 +02:00
// example:
2011-01-28 11:54:13 +01:00
// http://localhost:8090/yacy/query.html?youare=sCJ6Tq8T0N9x&object=lurlcount
2011-10-04 11:06:24 +02:00
2009-07-19 22:37:44 +02:00
public static serverObjects respond ( final RequestHeader header , final serverObjects post , final serverSwitch ss ) {
2008-03-12 01:05:18 +01:00
if ( post = = null | | ss = = null ) { return null ; }
2005-10-05 15:16:53 +02:00
// return variable that accumulates replacements
2009-07-19 22:37:44 +02:00
final Switchboard sb = ( Switchboard ) ss ;
2011-04-29 03:52:20 +02:00
2012-08-16 16:28:57 +02:00
// remember the peer contact for peer statistics
final String clientip = header . get ( HeaderFramework . CONNECTION_PROP_CLIENTIP , " <unknown> " ) ; // read an artificial header addendum
final String userAgent = header . get ( HeaderFramework . USER_AGENT , " <unknown> " ) ;
sb . peers . peerActions . setUserAgent ( clientip , userAgent ) ;
final serverObjects prop = new serverObjects ( ) ;
2011-10-04 11:06:24 +02:00
prop . put ( " magic " , Network . magic ) ;
if ( ( post = = null ) | | ( ss = = null ) | | ! Protocol . authentifyRequest ( post , ss ) ) {
2008-06-05 14:52:27 +02:00
prop . put ( " response " , " -1 " ) ; // request rejected
return prop ;
}
2011-10-04 11:06:24 +02:00
2008-03-12 01:05:18 +01:00
if ( ( sb . isRobinsonMode ( ) ) & &
( ! sb . isPublicRobinson ( ) ) & &
2009-07-19 22:37:44 +02:00
( ! sb . isInMyCluster ( header . get ( HeaderFramework . CONNECTION_PROP_CLIENTIP ) ) ) ) {
2008-03-12 01:05:18 +01:00
// if we are a robinson cluster, answer only if we are public robinson peers,
// or we are a private cluster and the requester is in our cluster.
// if we don't answer, the remote peer will recognize us as junior peer,
// what would mean that our peer ping does not work
prop . put ( " response " , " -1 " ) ; // request rejected
2008-02-08 15:06:34 +01:00
return prop ;
}
2011-10-04 11:06:24 +02:00
2005-10-05 15:16:53 +02:00
// System.out.println("YACYQUERY: RECEIVED POST = " + ((post == null) ? "NULL" : post.toString()));
2005-04-07 21:19:42 +02:00
2008-03-12 01:05:18 +01:00
// final String iam = post.get("iam", ""); // complete seed of the requesting peer
2005-12-07 00:51:29 +01:00
final String youare = post . get ( " youare " , " " ) ; // seed hash of the target peer, used for testing network stability
// final String key = post.get("key", ""); // transmission key for response
final String obj = post . get ( " object " , " " ) ; // keyword for query subject
2008-03-12 01:05:18 +01:00
final String env = post . get ( " env " , " " ) ; // argument to query
2008-02-05 17:32:10 +01:00
2011-02-25 22:11:53 +01:00
prop . put ( " mytime " , GenericFormatter . SHORT_SECOND_FORMATTER . format ( ) ) ;
2005-10-05 15:16:53 +02:00
// check if we are the right target and requester has correct information about this peer
2009-05-28 16:26:05 +02:00
if ( sb . peers . mySeed ( ) = = null | | ! sb . peers . mySeed ( ) . hash . equals ( youare ) ) {
2005-10-05 15:16:53 +02:00
// this request has a wrong target
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " -1 " ) ; // request rejected
2005-10-05 15:16:53 +02:00
return prop ;
}
// requests about environment
2009-03-16 01:18:37 +01:00
if ( obj . equals ( " rwiurlcount " ) ) try {
2005-10-05 15:16:53 +02:00
// the total number of different urls in the rwi is returned
2005-08-14 02:57:30 +02:00
// <env> shall contain a word hash, the number of assigned lurls to this hash is returned
2013-05-28 16:26:38 +02:00
prop . put ( " response " , sb . index . termIndex ( ) = = null ? 0 : sb . index . termIndex ( ) . get ( env . getBytes ( ) , null ) . size ( ) ) ;
2005-08-14 02:57:30 +02:00
return prop ;
2011-10-04 11:06:24 +02:00
} catch ( final IOException e ) {
2013-07-09 14:28:25 +02:00
ConcurrentLog . logException ( e ) ;
2005-10-05 15:16:53 +02:00
}
if ( obj . equals ( " rwicount " ) ) {
// return the total number of available word indexes
2012-08-31 13:03:00 +02:00
prop . put ( " response " , sb . index . RWICount ( ) ) ;
2005-10-05 15:16:53 +02:00
return prop ;
}
if ( obj . equals ( " lurlcount " ) ) {
// return the number of all available l-url's
2013-05-08 11:50:46 +02:00
prop . put ( " response " , 1 /*sb.index.fulltext().collectionSize()*/ ) ; // patched to not call collectionSize() any more because the acutal size is not needed. Instead, rwicount should be called
2005-10-05 15:16:53 +02:00
return prop ;
}
// requests about requirements
if ( obj . equals ( " wantedlurls " ) ) {
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " 0 " ) ; // dummy response
2005-10-05 15:16:53 +02:00
return prop ;
}
if ( obj . equals ( " wantedpurls " ) ) {
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " 0 " ) ; // dummy response
2005-10-05 15:16:53 +02:00
return prop ;
}
if ( obj . equals ( " wantedword " ) ) {
// response returns a list of wanted word hashes
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " 0 " ) ; // dummy response
2005-10-05 15:16:53 +02:00
return prop ;
}
if ( obj . equals ( " wantedrwi " ) ) {
// <env> shall contain a word hash, the number of wanted lurls for this hash is returned
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " 0 " ) ; // dummy response
2005-10-05 15:16:53 +02:00
return prop ;
}
if ( obj . equals ( " wantedseeds " ) ) {
// return a number of wanted seed
2007-10-24 23:38:19 +02:00
prop . put ( " response " , " 0 " ) ; // dummy response
2005-10-05 15:16:53 +02:00
return prop ;
}
// return rewrite properties
return prop ;
2005-04-07 21:19:42 +02:00
}
2007-01-16 15:07:54 +01:00
}