From 6bc5a9075bec3c0a2a364a55db066ebe93daa175 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 15 Dec 2019 16:16:11 +0000 Subject: [PATCH] rewrite welcome jsp to a servlet, add sanity check of inputs --- .../muwire/webui/ConfigurationServlet.java | 6 +- .../java/com/muwire/webui/InitServlet.java | 55 +++++++++++++++++++ .../src/main/java/com/muwire/webui/Util.java | 1 + webui/src/main/webapp/MuWire.jsp | 7 +++ webui/src/main/webapp/init.jsp | 24 -------- webui/templates/web.xml.template | 15 +++-- 6 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 webui/src/main/java/com/muwire/webui/InitServlet.java delete mode 100644 webui/src/main/webapp/init.jsp diff --git a/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java b/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java index 421c44de..9ae7c608 100644 --- a/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java +++ b/webui/src/main/java/com/muwire/webui/ConfigurationServlet.java @@ -93,11 +93,11 @@ public class ConfigurationServlet extends HttpServlet { private static File getDirectory(String s) throws Exception { File f = new File(s); if (!f.exists()) - throw new Exception(Util._t("Bad input")+ Util._t("{0} does not exist",s)); + throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} does not exist",s)); if (!f.isDirectory()) - throw new Exception(Util._t("Bad input")+ Util._t("{0} is not a directory",s)); + throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} is not a directory",s)); if (!f.canWrite()) - throw new Exception(Util._t("Bad input")+ Util._t("{0} not writeable",s)); + throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} not writeable",s)); return f; } diff --git a/webui/src/main/java/com/muwire/webui/InitServlet.java b/webui/src/main/java/com/muwire/webui/InitServlet.java new file mode 100644 index 00000000..6b079981 --- /dev/null +++ b/webui/src/main/java/com/muwire/webui/InitServlet.java @@ -0,0 +1,55 @@ +package com.muwire.webui; + +import java.io.File; +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class InitServlet extends HttpServlet { + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String nickname = req.getParameter("nickname"); + if (nickname == null || nickname.trim().length() == 0) + throw new Exception("Nickname cannot be blank"); + + String downloadLocation = req.getParameter("download_location"); + if (downloadLocation == null) + throw new Exception("Download location cannot be blank"); + File downloadLocationFile = new File(downloadLocation); + if (!downloadLocationFile.exists()) { + if (!downloadLocationFile.mkdirs()) + throw new Exception("Couldn't create download location"); + } else if (downloadLocationFile.isFile()) + throw new Exception("Download location must point to a directory"); + else if (!downloadLocationFile.canWrite()) + throw new Exception("Download location not writeable"); + + String incompleteLocation = req.getParameter("incomplete_location"); + if (incompleteLocation == null) + throw new Exception("Incomplete files location cannot be blank"); + File incompleteLocationFile = new File(incompleteLocation); + if (!incompleteLocationFile.exists()) { + if (!incompleteLocationFile.mkdirs()) + throw new Exception("Couldn't create incomplete files location"); + } else if (incompleteLocationFile.isFile()) + throw new Exception("Incomplete files location must point to a directory"); + else if (!incompleteLocationFile.canWrite()) + throw new Exception("Incomplete files location not writeable"); + + MuWireClient client = (MuWireClient) req.getServletContext().getAttribute("mwClient"); + client.initMWProps(nickname, downloadLocationFile, incompleteLocationFile); + client.start(); + resp.sendRedirect("/MuWire/index"); + } catch (Throwable e) { + req.getServletContext().setAttribute("MWInitError", e); + resp.sendRedirect("/MuWire/MuWire"); + } + + } + +} diff --git a/webui/src/main/java/com/muwire/webui/Util.java b/webui/src/main/java/com/muwire/webui/Util.java index d041d82b..114c6adc 100644 --- a/webui/src/main/java/com/muwire/webui/Util.java +++ b/webui/src/main/java/com/muwire/webui/Util.java @@ -44,6 +44,7 @@ public class Util { _x("Download"), _x("Download Location"), _x("Download retry frequency (seconds)"), + _x("Download speed smoothing interval (second)"), _x("Downloader"), _x("Downloading"), _x("Enter Reason (Optional)"), diff --git a/webui/src/main/webapp/MuWire.jsp b/webui/src/main/webapp/MuWire.jsp index 7d85f026..766d0823 100644 --- a/webui/src/main/webapp/MuWire.jsp +++ b/webui/src/main/webapp/MuWire.jsp @@ -15,6 +15,8 @@ String defaultIncompletesLocation = System.getProperty("user.home") + File.separator+"MuWire Incompletes"; session.setAttribute("defaultDownloadLocation",defaultDownloadLocation); session.setAttribute("defaultIncompletesLocation",defaultIncompletesLocation); + + Throwable error = (Throwable) application.getAttribute("MWInitError"); %> + +<% if (error != null) { %> +
<%=error.getMessage()%>
+<% } %> +

<%=Util._t("Welcome to MuWire! Please select a nickname and download locations")%>

<%=Util._t("Nickname")%>: diff --git a/webui/src/main/webapp/init.jsp b/webui/src/main/webapp/init.jsp deleted file mode 100644 index 03124bce..00000000 --- a/webui/src/main/webapp/init.jsp +++ /dev/null @@ -1,24 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%> - -<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> -<%@ page import="java.io.File" %> -<%@ page import="com.muwire.webui.MuWireClient" %> - - - -<%@include file="css.jsi"%> - - - <% - String nickname = request.getParameter("nickname"); - String downloadLocation = request.getParameter("download_location"); - String incompleteLocation = request.getParameter("incomplete_location"); - - MuWireClient client = (MuWireClient) application.getAttribute("mwClient"); - client.initMWProps(nickname, new File(downloadLocation), new File(incompleteLocation)); - client.start(); - %> - - - diff --git a/webui/templates/web.xml.template b/webui/templates/web.xml.template index ea4f7ff3..22f4a804 100644 --- a/webui/templates/web.xml.template +++ b/webui/templates/web.xml.template @@ -16,6 +16,11 @@ + + com.muwire.webui.InitServlet + com.muwire.webui.InitServlet + + com.muwire.webui.SearchServlet com.muwire.webui.SearchServlet @@ -66,6 +71,11 @@ /index.jsp + + com.muwire.webui.InitServlet + /init + + com.muwire.webui.SearchServlet /Search @@ -154,11 +164,6 @@ Mappings without the .jsp suffix /SharedFiles - - com.muwire.webui.init_jsp - /init - - com.muwire.webui.TrustUsers_jsp /TrustUsers