switch to using a servlet instead of bean

This commit is contained in:
Zlatin Balevsky
2019-11-30 13:07:47 +00:00
parent ac17618f0c
commit 9edea17fb7
9 changed files with 89 additions and 117 deletions

View File

@ -133,6 +133,6 @@ task sign {
webappConfig.dependsOn pluginDir
clientsConfig.dependsOn webappConfig
pack.dependsOn webappConfig
pluginZip.dependsOn(webappConfig,pluginConfig,clientsConfig,pack)
pluginZip.dependsOn(webappConfig,pluginConfig,pack)
sign.dependsOn pluginZip
assemble.dependsOn sign

View File

@ -50,6 +50,7 @@ task generateWebXML {
def templateText = template.text
def jasper = new File("$buildDir/tmp_jsp/web.xml.jasper")
templateText = templateText.replaceAll("__JASPER__", jasper.text)
templateText = templateText.replaceAll("__VERSION__", project.version)
def webXml = new File("$buildDir/tmp_jsp/web.xml")
webXml.text = templateText
}

View File

@ -16,79 +16,23 @@ import net.i2p.app.ClientAppState;
import net.i2p.router.RouterContext;
import net.i2p.router.app.RouterApp;
public class MuWireClient implements RouterApp {
public class MuWireClient {
private final RouterContext ctx;
private final ClientAppManager mgr;
private final String version;
private final String home;
private final File mwProps;
private ClientAppState state;
private volatile Core core;
public MuWireClient(RouterContext ctx, ClientAppManager mgr, String[]args) {
public MuWireClient(RouterContext ctx, String home, String version) {
this.ctx = ctx;
this.mgr = mgr;
String version = null;
String home = null;
for (String arg : args) {
String [] split = arg.split("=");
if (split[0].equals("version"))
version = split[1];
else if (split[0].equals("home"))
home = split[1];
}
this.version = version;
this.home = home;
this.mwProps = new File(home, "MuWire.properties");
this.state = ClientAppState.INITIALIZED;
mgr.register(this);
}
@Override
public void startup() throws Throwable {
changeState(ClientAppState.STARTING, null, null);
try {
start();
changeState(ClientAppState.RUNNING, null, null);
} catch (Exception bad) {
changeState(ClientAppState.START_FAILED, "Failed to start", bad);
stop();
}
}
@Override
public void shutdown(String[] args) throws Throwable {
if (state == ClientAppState.STOPPED)
return;
changeState(ClientAppState.STOPPING,null,null);
stop();
changeState(ClientAppState.STOPPED, null, null);
}
private synchronized void changeState(ClientAppState state, String msg, Exception e) {
this.state = state;
mgr.notify(this, state, msg, e);
}
@Override
public synchronized ClientAppState getState() {
return state;
}
@Override
public String getName() {
return "MuWire";
}
@Override
public String getDisplayName() {
return "MuWire";
}
private void start() throws Throwable {
public void start() throws Throwable {
if (needsMWInit())
return;
@ -102,7 +46,7 @@ public class MuWireClient implements RouterApp {
core.startServices();
}
private void stop() throws Throwable {
public void stop() throws Throwable {
Core core = this.core;
if (core == null)
return;

View File

@ -1,14 +1,56 @@
package com.muwire.webui;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.i2p.I2PAppContext;
import net.i2p.router.RouterContext;
public class MuWireServlet extends HttpServlet {
private volatile MuWireClient client;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
RouterContext ctx = (RouterContext) I2PAppContext.getGlobalContext();
String home = ctx.getConfigDir()+File.separator+"plugins"+File.separator+"MuWire";
String version = config.getInitParameter("version");
client = new MuWireClient(ctx, home, version);
try {
client.start();
} catch (Throwable bad) {
throw new ServletException(bad);
}
config.getServletContext().setAttribute("mwClient", client);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("mwClient", client);
if (client.needsMWInit()) {
resp.sendRedirect("/MuWire/MuWire.jsp");
} else {
resp.setContentType("text/html");
resp.getWriter().println("<html>MW is initialized</html");
}
}
@Override
public void destroy() {
if (client != null) {
try {client.stop();} catch (Throwable ignore) {}
client = null;
}
}
}

View File

@ -0,0 +1,30 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>MuWire</title>
</head>
<body>
<%
String defaultDownloadLocation = System.getProperty("user.home")+File.separator+"Downloads";
String defaultIncompletesLocation = System.getProperty("user.home") + File.separator+"MuWire Incompletes";
session.setAttribute("defaultDownloadLocation",defaultDownloadLocation);
session.setAttribute("defaultIncompletesLocation",defaultIncompletesLocation);
%>
<p>Welcome to MuWire! Please select a nickname and download locations</p>
<form action="/MuWire/init.jsp" method="post">
Nickname:
<input type="text" name="nickname"><br>
Directory for saving downloaded files:
<input type='text' name='download_location' value="${defaultDownloadLocation}"><br/>
Directory for storing incomplete files:
<input type='text' name='incomplete_location' value="${defaultIncompletesLocation}"><br/>
<input type="submit" value="Submit">
</body>
</html>

View File

@ -1,8 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0;url=index.jsp" />
</head>
<body>
</body>
</html>

View File

@ -1,38 +0,0 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>MuWire</title>
</head>
<body>
<jsp:useBean id="mwBean" class="com.muwire.webui.MuWireBean"/>
<c:set var="mwClient" scope="application" value="${mwBean.getMwClient()}"/>
<%
String defaultDownloadLocation = System.getProperty("user.home")+File.separator+"Downloads";
String defaultIncompletesLocation = System.getProperty("user.home") + File.separator+"MuWire Incompletes";
session.setAttribute("defaultDownloadLocation",defaultDownloadLocation);
session.setAttribute("defaultIncompletesLocation",defaultIncompletesLocation);
%>
<c:if test = "${mwClient.needsMWInit()}">
<p>Welcome to MuWire! Please select a nickname and download locations</p>
<form action="/MuWire/init.jsp" method="post">
Nickname:
<input type="text" name="nickname"><br>
Directory for saving downloaded files:
<input type='text' name='download_location' value="${defaultDownloadLocation}"><br/>
Directory for storing incomplete files:
<input type='text' name='incomplete_location' value="${defaultIncompletesLocation}"><br/>
<input type="submit" value="Submit">
</c:if>
<c:if test = "${!mwClient.needsMWInit()}">
<p>MW doesn't need initing</p>
</c:if>
</body>
</html>

View File

@ -1,23 +1,19 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.muwire.webui.MuWireClient" %>
<html
<body>
<jsp:useBean id="mwBean" class="com.muwire.webui.MuWireBean"/>
<c:set var="mwClient" scope="application" value="${mwBean.getMwClient()}"/>
<%
String nickname = request.getParameter("nickname");
String downloadLocation = request.getParameter("download_location");
String incompleteLocation = request.getParameter("incomplete_location");
session.setAttribute("downloadLocation", new File(downloadLocation));
session.setAttribute("incompleteLocation", new File(incompleteLocation));
session.setAttribute("nickname",nickname);
MuWireClient client = (MuWireClient) session.getAttribute("mwClient");
client.initMWProps(nickname, new File(downloadLocation), new File(incompleteLocation));
client.start();
%>
<c:set var="initResult" scope="session" value="${mwClient.initMWProps(nickname,downloadLocation,incompleteLocation)}"/>
<c:redirect url="/index.jsp"/>
<c:redirect url="/"/>
</body>
</html>

View File

@ -9,11 +9,16 @@
<servlet>
<servlet-name>com.muwire.webui.MuWireServlet</servlet-name>
<servlet-class>com.muwire.webui.MuWireServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>version</param-name>
<param-value>__VERSION__</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>com.muwire.webui.MuWireServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>