From 94a2d9eb867cb7b74c41e8eff9157c518e18408f Mon Sep 17 00:00:00 2001
From: ulrich <not disclosed>
Date: Thu, 28 Dec 2017 11:06:35 +0000
Subject: [PATCH] Erste Version des MarkdownServlets fertig zum Test

---
 src/de/uhilger/wbx/web/CustomResponseWrapper.java |   70 +++++++++++++++++
 src/de/uhilger/wbx/web/ViewServlet.java           |   31 -------
 src/de/uhilger/wbx/web/WbxServlet.java            |   48 +++++++++++
 src/de/uhilger/wbx/web/MarkdownServlet.java       |   38 ++-------
 4 files changed, 125 insertions(+), 62 deletions(-)

diff --git a/src/de/uhilger/wbx/web/CustomResponseWrapper.java b/src/de/uhilger/wbx/web/CustomResponseWrapper.java
new file mode 100644
index 0000000..bc765c3
--- /dev/null
+++ b/src/de/uhilger/wbx/web/CustomResponseWrapper.java
@@ -0,0 +1,70 @@
+/*
+    WebBox - Dein Server.
+    Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
+
+    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 <http://www.gnu.org/licenses/>.
+ */
+package de.uhilger.wbx.web;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+/**
+ * Die Klasse CustomResponseWrapper faengt den Inhalt einer HttpServletResponse
+ * in einem StringWriter auf. Dieser kann mit der Methode getResponseContent 
+ * gelesen werden.
+ *
+ * @author ulrich
+ */
+public class CustomResponseWrapper extends HttpServletResponseWrapper {
+
+  private StringWriter stringWriter;
+  private boolean isOutputStreamCalled;
+
+  public CustomResponseWrapper(HttpServletResponse response) {
+    super(response);
+  }
+
+  @Override
+  public ServletOutputStream getOutputStream() throws IOException {
+    if (this.stringWriter != null) {
+      throw new IllegalStateException("The getWriter() is already called.");
+    }
+    isOutputStreamCalled = true;
+    return super.getOutputStream();
+  }
+
+  @Override
+  public PrintWriter getWriter() throws IOException {
+    if (isOutputStreamCalled) {
+      throw new IllegalStateException("The getOutputStream() is already called.");
+    }
+
+    this.stringWriter = new StringWriter();
+
+    return new PrintWriter(this.stringWriter);
+  }
+
+  public String getResponseContent() {
+    if (this.stringWriter != null) {
+      return this.stringWriter.toString();
+    }
+    return "";
+  }
+
+}
diff --git a/src/de/uhilger/wbx/web/MarkdownServlet.java b/src/de/uhilger/wbx/web/MarkdownServlet.java
index 58bfbc3..46082d8 100644
--- a/src/de/uhilger/wbx/web/MarkdownServlet.java
+++ b/src/de/uhilger/wbx/web/MarkdownServlet.java
@@ -19,8 +19,10 @@
 
 import java.io.IOException;
 import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.markdownj.MarkdownProcessor;
 
 /**
  *
@@ -29,37 +31,15 @@
 public class MarkdownServlet extends WbxServlet {
   
   
-  /**
-   * Handles the HTTP <code>GET</code> method.
-   *
-   * @param request servlet request
-   * @param response servlet response
-   * @throws ServletException if a servlet-specific error occurs
-   * @throws IOException if an I/O error occurs
-   */
   @Override
-  protected void doGet(HttpServletRequest request, HttpServletResponse response)
-          throws ServletException, IOException {
-    String contextPath = request.getContextPath();
-    if(HOME_CTX.equals(contextPath)) {
-      String userName = getUserName(request);
-      if (userName != null) {
-        String urlUser = getUrlUser(request, userName);
-        if(userName.equals(urlUser)) {
-          seiteAusgeben(request, response);
-        } else {
-          logger.fine("Wrong user.");
-        }
-      } else {
-        logger.fine("Missing login.");
-      }
-    } else {
-      seiteAusgeben(request, response);
-    }
-  }
-  
-  private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) {
+  protected void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
     
+    CustomResponseWrapper wrapper = new CustomResponseWrapper(response);
+    super.doGet(request, wrapper);
+    String responseContent = wrapper.getResponseContent();
+    ServletOutputStream out = response.getOutputStream();
+    MarkdownProcessor p = new MarkdownProcessor();
+    out.print(p.markdown(responseContent));
   }
   
 }
diff --git a/src/de/uhilger/wbx/web/ViewServlet.java b/src/de/uhilger/wbx/web/ViewServlet.java
index 891a98f..4b3119c 100644
--- a/src/de/uhilger/wbx/web/ViewServlet.java
+++ b/src/de/uhilger/wbx/web/ViewServlet.java
@@ -38,36 +38,7 @@
   private static final Logger logger = Logger.getLogger(ViewServlet.class.getName());
   //private static final String HOME_CTX = "/home";
 
-  /**
-   * Handles the HTTP <code>GET</code> method.
-   *
-   * @param request servlet request
-   * @param response servlet response
-   * @throws ServletException if a servlet-specific error occurs
-   * @throws IOException if an I/O error occurs
-   */
-  @Override
-  protected void doGet(HttpServletRequest request, HttpServletResponse response)
-          throws ServletException, IOException {
-    String contextPath = request.getContextPath();
-    if(HOME_CTX.equals(contextPath)) {
-      String userName = getUserName(request);
-      if (userName != null) {
-        String urlUser = getUrlUser(request, userName);
-        if(userName.equals(urlUser)) {
-          seiteAusgeben(request, response);
-        } else {
-          logger.fine("Wrong user.");
-        }
-      } else {
-        logger.fine("Missing login.");
-      }
-    } else {
-      seiteAusgeben(request, response);
-    }
-  }
-  
-  private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+  protected void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
     ServletOutputStream out = response.getOutputStream();
     printHeader(out);
     super.doGet(request, response);
diff --git a/src/de/uhilger/wbx/web/WbxServlet.java b/src/de/uhilger/wbx/web/WbxServlet.java
index 5a111b4..fa7f661 100644
--- a/src/de/uhilger/wbx/web/WbxServlet.java
+++ b/src/de/uhilger/wbx/web/WbxServlet.java
@@ -9,20 +9,60 @@
 import java.security.Principal;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import org.apache.catalina.servlets.DefaultServlet;
 
 /**
- *
+ * Das WbxServlet prueft fuer alle HTTP GET Anfragen den Conetxt Path. Wenn 
+ * er /home enthaelt, muss der angemeldete Benutzer mit dem Benutzernamen 
+ * im Pfad uebereinstimmen. Stimmt er nicht ueberein oder ist kein Nutzer 
+ * angemeldet, wird ein Fehler ausgegeben.
+ * 
+ * Alle anderen Pfade als /home werden ohne Anmeldung ausgegeben.
+ * 
+ * Die Ausgabe des Inhalts wird auf die abstrakte Methode seiteAusgeben 
+ * umgeleitet. Serlvets, die von WbxServlet abgeleitet werden, koennen 
+ * die Methode seiteAusgeben so implementieren, dass sie z.B. den 
+ * Inhalt der Ausgabe nach Wunsch veraendern.
+ * 
  * @author ulrich
  */
-public class WbxServlet extends DefaultServlet {
+public abstract class WbxServlet extends DefaultServlet {
   
   protected static final Logger logger = Logger.getLogger(WbxServlet.class.getName());
   protected static final String HOME_CTX = "/home";
   
+  /**
+   * Handles the HTTP <code>GET</code> method.
+   *
+   * @param request servlet request
+   * @param response servlet response
+   * @throws ServletException if a servlet-specific error occurs
+   * @throws IOException if an I/O error occurs
+   */
+  @Override
+  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
+    String contextPath = request.getContextPath();
+    if(HOME_CTX.equals(contextPath)) {
+      String userName = getUserName(request);
+      if (userName != null) {
+        String urlUser = getUrlUser(request, userName);
+        if(userName.equals(urlUser)) {
+          seiteAusgeben(request, response);
+        } else {
+          logger.fine("Wrong user.");
+        }
+      } else {
+        logger.fine("Missing login.");
+      }
+    } else {
+      seiteAusgeben(request, response);
+    }
+  }
   
-  protected String getUrlUser(HttpServletRequest request, String userName) throws IOException {
+  protected String getUrlUser(HttpServletRequest request, String userName) {
     String result = "";
     String requestUrlStr = request.getRequestURL().toString();
     String contextPath = request.getContextPath();
@@ -48,5 +88,7 @@
     }
     return userName;
   }
+  
+  protected abstract void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
 
 }

--
Gitblit v1.9.3