skeleton of search manager

This commit is contained in:
Zlatin Balevsky
2019-11-30 18:16:25 +00:00
parent ad8983e889
commit 7bf520ac8c
6 changed files with 95 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import com.muwire.core.Core;
import com.muwire.core.MuWireSettings;
@@ -26,14 +28,16 @@ import net.i2p.util.Log;
public class MuWireClient {
private final RouterContext ctx;
private final ServletContext servletContext;
private final String version;
private final String home;
private final File mwProps;
private volatile Core core;
public MuWireClient(RouterContext ctx, String home, String version) {
public MuWireClient(RouterContext ctx, String home, String version, ServletContext servletContext) {
this.ctx = ctx;
this.servletContext = servletContext;
this.version = version;
this.home = home;
this.mwProps = new File(home, "MuWire.properties");
@@ -96,8 +100,14 @@ public class MuWireClient {
return core;
}
public ServletContext getServletContext() {
return servletContext;
}
void setCore(Core core) {
this.core = core;
servletContext.setAttribute("core", core);
servletContext.setAttribute("searchManager", new SearchManager(core));
}
public String getHome() {

View File

@@ -34,7 +34,7 @@ public class MuWireServlet extends HttpServlet {
String home = ctx.getConfigDir()+File.separator+"plugins"+File.separator+"MuWire";
String version = config.getInitParameter("version");
client = new MuWireClient(ctx, home, version);
client = new MuWireClient(ctx, home, version, config.getServletContext());
try {
client.start();
} catch (Throwable bad) {

View File

@@ -0,0 +1,27 @@
package com.muwire.webui;
import java.util.HashSet;
import java.util.Set;
import com.muwire.core.Core;
class SearchManager {
private final Core core;
private final Set<String> searches = new HashSet<>();
SearchManager(Core core) {
this.core = core;
}
void newSearch(String search) {
searches.add(search);
}
Iterable<String> getSearches() {
return searches;
}
}

View File

@@ -0,0 +1,27 @@
package com.muwire.webui;
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;
public class SearchServlet extends HttpServlet {
private SearchManager searchManager;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String search = req.getParameter("search");
searchManager.newSearch(search);
resp.sendRedirect("/MuWire/Home.jsp");
}
@Override
public void init(ServletConfig config) throws ServletException {
searchManager = (SearchManager) config.getServletContext().getAttribute("searchManager");
}
}

View File

@@ -1,7 +1,8 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="com.muwire.webui.MuWireClient" %>
<%@ page import="java.util.*" %>
<%@ page import="com.muwire.webui.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
@@ -21,5 +22,21 @@
<body>
<p>Welcome to MuWire ${persona}</p>
<form action="/MuWire/Search" method="post">
<input type="text", name="search" />
<input type="submit", value="Search" />
</form>
<hr/>
Active Searches:<br/>
<%
SearchManager searchManager = (SearchManager) client.getServletContext().getAttribute("searchManager");
for (String search : searchManager.getSearches()) {
out.print(search);
out.print("<br/>");
}
%>
</body>
</html>

View File

@@ -16,11 +16,22 @@
</init-param>
</servlet>
<servlet>
<servlet-name>com.muwire.webui.SearchServlet</servlet-name>
<servlet-class>com.muwire.webui.SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.muwire.webui.MuWireServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.SearchServlet</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
__JASPER__