forked from I2P_Developers/i2p.i2p
* Addressbook, susidns: Rework addressbook into a
HttpServlet, so susidns can kick it when the subscription list changes
This commit is contained in:
@ -21,24 +21,43 @@
|
|||||||
|
|
||||||
package net.i2p.addressbook;
|
package net.i2p.addressbook;
|
||||||
|
|
||||||
import javax.servlet.GenericServlet;
|
import java.util.Random;
|
||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletConfig;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.ServletResponse;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper for addressbook to allow it to be started as a web application.
|
* A wrapper for addressbook to allow it to be started as a web application.
|
||||||
*
|
*
|
||||||
|
* This was a GenericServlet, we make it an HttpServlet solely to provide a hook
|
||||||
|
* for SusiDNS to wake us up when the subscription list changes.
|
||||||
|
*
|
||||||
* @author Ragnarok
|
* @author Ragnarok
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Servlet extends GenericServlet {
|
public class Servlet extends HttpServlet {
|
||||||
|
private Thread _thread;
|
||||||
|
private String _nonce;
|
||||||
|
private static final String PROP_NONCE = "addressbook.nonce";
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/**
|
||||||
* @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
|
* Hack to allow susidns to kick the daemon when the subscription list changes.
|
||||||
|
* URL must be /addressbook/ with wakeup param set, and nonce param set from system property.
|
||||||
|
*
|
||||||
|
* (non-Javadoc)
|
||||||
|
* see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
|
||||||
*/
|
*/
|
||||||
public void service(ServletRequest request, ServletResponse response) {
|
public void service(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
//System.err.println("Got request nonce = " + request.getParameter("nonce"));
|
||||||
|
if (_thread != null && request.getParameter("wakeup") != null &&
|
||||||
|
_nonce != null && _nonce.equals(request.getParameter("nonce"))) {
|
||||||
|
//System.err.println("Sending interrupt");
|
||||||
|
_thread.interrupt();
|
||||||
|
}
|
||||||
|
// no output
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@ -49,15 +68,19 @@ public class Servlet extends GenericServlet {
|
|||||||
try {
|
try {
|
||||||
super.init(config);
|
super.init(config);
|
||||||
} catch (ServletException exp) {
|
} catch (ServletException exp) {
|
||||||
|
System.err.println("Addressbook init exception: " + exp);
|
||||||
}
|
}
|
||||||
|
_nonce = "" + Math.abs((new Random()).nextLong());
|
||||||
|
// put the nonce where susidns can get it
|
||||||
|
System.setProperty(PROP_NONCE, _nonce);
|
||||||
String[] args = new String[1];
|
String[] args = new String[1];
|
||||||
args[0] = config.getInitParameter("home");
|
args[0] = config.getInitParameter("home");
|
||||||
DaemonThread thread = new DaemonThread(args);
|
_thread = new DaemonThread(args);
|
||||||
thread.setDaemon(true);
|
_thread.setDaemon(true);
|
||||||
thread.setName("Addressbook");
|
_thread.setName("Addressbook");
|
||||||
thread.start();
|
_thread.start();
|
||||||
System.out.println("INFO: Starting Addressbook " + Daemon.VERSION);
|
System.out.println("INFO: Starting Addressbook " + Daemon.VERSION);
|
||||||
System.out.println("INFO: config root under " + args[0]);
|
//System.out.println("INFO: config root under " + args[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,10 @@
|
|||||||
</init-param>
|
</init-param>
|
||||||
<load-on-startup>1</load-on-startup>
|
<load-on-startup>1</load-on-startup>
|
||||||
</servlet>
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>addressbook</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
</web-app>
|
</web-app>
|
||||||
|
@ -128,7 +128,19 @@ public class SubscriptionsBean
|
|||||||
if( lastSerial != null && serial != null && serial.compareTo( lastSerial ) == 0 ) {
|
if( lastSerial != null && serial != null && serial.compareTo( lastSerial ) == 0 ) {
|
||||||
if( action.compareToIgnoreCase( "save") == 0 ) {
|
if( action.compareToIgnoreCase( "save") == 0 ) {
|
||||||
save();
|
save();
|
||||||
message = "Subscriptions saved.";
|
String nonce = System.getProperty("addressbook.nonce");
|
||||||
|
if (nonce != null) {
|
||||||
|
// Yes this is a hack.
|
||||||
|
// No it doesn't work on a text-mode browser.
|
||||||
|
// Fetching from the addressbook servlet
|
||||||
|
// with the correct parameters will kick off a
|
||||||
|
// config reload and fetch.
|
||||||
|
message = "Subscriptions saved, updating addressbook from subscription sources now." +
|
||||||
|
"<img height=\"1\" width=\"1\" alt=\"\" " +
|
||||||
|
"src=\"/addressbook/?wakeup=1&nonce=" + nonce + "\">";
|
||||||
|
} else {
|
||||||
|
message = "Subscriptions saved.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if( action.compareToIgnoreCase( "reload") == 0 ) {
|
else if( action.compareToIgnoreCase( "reload") == 0 ) {
|
||||||
reload();
|
reload();
|
||||||
|
Reference in New Issue
Block a user