From 14ad66efa9f692023ae6982e3fa1d67c3c0aad11 Mon Sep 17 00:00:00 2001
From: Ugnich Anton
Date: Mon, 24 Dec 2012 13:18:30 +0700
Subject: Help
---
src/java/com/juick/http/www/Help.java | 108 +++++++++++++++++++++++++++++++++
src/java/com/juick/http/www/Main.java | 5 ++
src/java/com/juick/http/www/Utils.java | 5 ++
3 files changed, 118 insertions(+)
create mode 100644 src/java/com/juick/http/www/Help.java
(limited to 'src/java/com/juick/http/www')
diff --git a/src/java/com/juick/http/www/Help.java b/src/java/com/juick/http/www/Help.java
new file mode 100644
index 00000000..9954194c
--- /dev/null
+++ b/src/java/com/juick/http/www/Help.java
@@ -0,0 +1,108 @@
+/*
+ * Juick
+ * Copyright (C) 2008-2011, Ugnich Anton
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+package com.juick.http.www;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author Ugnich Anton
+ */
+public class Help {
+
+ protected void doRedirectToHelpIndex(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ String lang = request.getLocale().getLanguage();
+ if (!(lang.equals("ru") || lang.equals("fr"))) {
+ lang = "en";
+ }
+ Utils.sendTemporaryRedirect(response, "/help/" + lang + "/");
+ }
+
+ protected void doGetHelp(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ com.juick.User visitor = Utils.getVisitorUser(sql, request);
+ Locale locale = request.getLocale();
+ ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
+
+ String path[] = request.getRequestURI().split("/");
+ String page;
+ if (path.length < 3 || path.length > 4 || path[2].length() != 2 || !path[2].matches("^[a-z]+$")) {
+ response.sendError(404);
+ return;
+ }
+
+ if (path.length == 4) {
+ page = path[3];
+ if (!page.matches("^[a-zA-Z0-9\\-]*$") || page.equals("navigation") || page.equals("index")) {
+ response.sendError(404);
+ return;
+ }
+ } else {
+ page = "index";
+ }
+
+ File f = new File("/var/www/juick.com/help3/" + path[2] + "/" + page);
+ if (!f.isFile()) {
+ response.sendError(404);
+ return;
+ }
+
+ response.setContentType("text/html; charset=UTF-8");
+ PrintWriter out = response.getWriter();
+ try {
+ PageTemplates.pageHead(out, rb.getString("Help"), "");
+ PageTemplates.pageNavigation(out, locale, visitor);
+
+ out.println("
");
+ out.println("
");
+ out.println("
");
+ printFile(out, f);
+ out.println("
");
+ out.println("
");
+
+ out.println("
");
+ printFile(out, new File("/var/www/juick.com/help3/" + path[2] + "/navigation"));
+ out.println("
");
+ printFile(out, new File("/var/www/juick.com/help3/langs"));
+ out.println("");
+
+ out.println("
");
+
+ PageTemplates.pageFooter(request, out, locale, visitor);
+ } finally {
+ out.close();
+ }
+ }
+
+ private void printFile(PrintWriter out, File f) throws IOException {
+ BufferedReader br = new BufferedReader(new FileReader(f));
+ String str;
+ while ((str = br.readLine()) != null) {
+ out.println(str);
+ }
+ }
+}
diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java
index 055327dd..20885906 100644
--- a/src/java/com/juick/http/www/Main.java
+++ b/src/java/com/juick/http/www/Main.java
@@ -45,6 +45,7 @@ public class Main extends HttpServlet implements XmppListener {
Tags tags = new Tags();
RootRedirects rootRedirects = new RootRedirects();
Login login = new Login();
+ Help help = new Help();
User pagesUser = new User();
UserThread pagesUserThread = new UserThread();
NewMessage pagesNewMessage = new NewMessage();
@@ -142,6 +143,10 @@ public class Main extends HttpServlet implements XmppListener {
login.doGetLogout(sql, request, response);
} else if (uri.equals("/settings")) {
//TODO settings
+ } else if (uri.equals("/help") || uri.equals("/help/")) {
+ help.doRedirectToHelpIndex(sql, request, response);
+ } else if (uri.startsWith("/help/")) {
+ help.doGetHelp(sql, request, response);
} else if (uri.matches("^/\\d+$")) {
rootRedirects.doGetPostID(sql, request, response);
} else if (uri.matches("^/[^/]+$")) {
diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java
index 80577d04..15782bf0 100644
--- a/src/java/com/juick/http/www/Utils.java
+++ b/src/java/com/juick/http/www/Utils.java
@@ -66,6 +66,11 @@ public class Utils {
return 0;
}
+ public static void sendTemporaryRedirect(HttpServletResponse response, String location) {
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", location);
+ }
+
public static void sendPermanentRedirect(HttpServletResponse response, String location) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", location);
--
cgit v1.2.3