From c3e8e1ba1ce93706ef8d8e9f97ce18fe8575c220 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 3 Sep 2010 12:48:57 -0400 Subject: Add quote submission Based on submit.jsp and submitted.jsp from the old application. --- html/quotes.jsp | 3 +-- src/WEB-INF/web.xml | 10 ++++++++ src/lq/AddQuote.java | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lq/Strings.java | 3 +++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/lq/AddQuote.java diff --git a/html/quotes.jsp b/html/quotes.jsp index 4d0c5d7..3e57979 100644 --- a/html/quotes.jsp +++ b/html/quotes.jsp @@ -24,8 +24,7 @@ hr { | | |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Quotes fra #linux.no på freenode - Klikk her for å legge til en quote - eller her for raskere stemming + Klikk her for å legge til en quote Sortér etter dato | score | id diff --git a/src/WEB-INF/web.xml b/src/WEB-INF/web.xml index bb255cb..3ed23ed 100644 --- a/src/WEB-INF/web.xml +++ b/src/WEB-INF/web.xml @@ -6,6 +6,16 @@ version="2.5"> Quotes fra #linux.no på freenode + + add_quote + lq.AddQuote + + + + add_quote + /post/add + + importquotes lq.ImportQuotes diff --git a/src/lq/AddQuote.java b/src/lq/AddQuote.java new file mode 100644 index 0000000..3174ac9 --- /dev/null +++ b/src/lq/AddQuote.java @@ -0,0 +1,69 @@ +package lq; + +import java.io.IOException; +import java.text.ParseException; +import java.util.Date; +import javax.jdo.PersistenceManager; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class AddQuote extends HttpServlet { + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + String nick = req.getParameter("nick"); + String text = req.getParameter("quote"); + String date = req.getParameter("date"); + String forward = req.getHeader("X-Forwarded-For"); + String ip = req.getRemoteAddr(); + + resp.setContentType("text/html"); + + boolean validInput = true; + Date quoteDate = new Date(); + + if (Strings.nullOrEmpty(nick)) { + nick = "Anonym"; + } + if (Strings.nullOrEmpty(text)) { + validInput = false; + } + if (!Strings.nullOrEmpty(date)) { + try { + quoteDate = DateUtil.dateFormat.parse(date); + } + catch (ParseException exception) { + resp.getWriter().println("Dato må være på formatet YYYY-MM-DD"); + validInput = false; + } + } + if (!Strings.nullOrEmpty(forward)) { + ip = ip + " (" + forward + ")"; + } + + if (!validInput) { + resp.getWriter().println("Eskje du lidt kniben nå"); + return; + } + + Quote newQuote = new Quote(quoteDate, nick, text, ip); + PersistenceManager pm = PMF.get().getPersistenceManager(); + try { + pm.makePersistent(newQuote); + } + finally { + pm.close(); + } + + resp.getWriter().println(successString); + } + + public final String successString = + "
\n" + + "

\n" + + "

 Takk, quoten venter nå på godkjenning.
\n" + + "
 Tilbake til quotes 
\n" + + "
\n"; +} diff --git a/src/lq/Strings.java b/src/lq/Strings.java index f3604f0..42d23e5 100644 --- a/src/lq/Strings.java +++ b/src/lq/Strings.java @@ -7,6 +7,9 @@ public class Strings { .replaceAll("<","<") .replaceAll(">",">") .replaceAll(" ","  "); + } + public static final boolean nullOrEmpty(String str) { + return str == null || str.isEmpty(); } } -- cgit v1.2.3