Advertisement
Guest User

Untitled

a guest
May 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. package main;
  2.  
  3. import html.HTMLDocument;
  4. import html.HTMLDocumentHandlerFactory;
  5. import html.HTMLElement;
  6. import richtext.RichTextHandler;
  7. import richtext.NestedMarkupHelper;
  8. import richtext.InlineMarkup;
  9. import richtext.SmileyParserFactory;
  10. import richtext.HyperlinkParser;
  11. import bbcode.BBCodeParser;
  12. import server.AbstractServer;
  13. import server.Query;
  14. import server.Request;
  15. import server.RequestMethod;
  16. import server.Response;
  17. import server.ExchangedRequest;
  18. import forum.Forum;
  19. import reply.ReplyContainer;
  20.  
  21. import java.io.IOException;
  22. import java.sql.Connection;
  23. import java.sql.DriverManager;
  24. import java.util.Locale;
  25. import java.util.Stack;
  26. import java.util.ResourceBundle;
  27. import server.ResponseStatus;
  28.  
  29. /**
  30.  * A main class used to test Liva.
  31.  * @author tobyhinloopen
  32.  */
  33. public class Main {
  34.     private static Stack<String> msgs = new Stack<String>();
  35.     final static int MAX_MESSAGE_LENGTH = 0x0FFFFF;
  36.  
  37.     /**
  38.      * The entrypoint of the test class of Liva.
  39.      * @param args the command line arguments
  40.      */
  41.     public static void main(String[] args) {
  42.         try {
  43.  
  44.             Connection conn = null;
  45.             try {
  46.                 ResourceBundle mysqlprops = ResourceBundle.getBundle("main.mysql");
  47.                 String host = mysqlprops.getString("host");
  48.                 int port = Integer.parseInt(mysqlprops.getString("port"));
  49.                 String database = mysqlprops.getString("db");
  50.                 String user = mysqlprops.getString("user");
  51.                 String password = mysqlprops.getString("pass");
  52.                 conn = DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database+"?user="+user+"&password="+password);
  53.             } catch(Exception e) {
  54.                 e.printStackTrace();
  55.             }
  56.  
  57.  
  58.             final HTMLDocumentHandlerFactory documentHandlerFactory = new HTMLDocumentHandlerFactory();
  59.             HTMLElement element;
  60.             for(InlineMarkup markup : InlineMarkup.values()) {
  61.                 element = new HTMLElement("span");
  62.                 element.setAttribute("class", markup.name().toLowerCase());
  63.                 documentHandlerFactory.addTag(markup, element);
  64.             }
  65.  
  66.             final SmileyParserFactory smileyHandlerFactory = new SmileyParserFactory();
  67.             smileyHandlerFactory.set(":P", "joke");
  68.             smileyHandlerFactory.set(":D", "happy");
  69.             smileyHandlerFactory.set("xD", "wtf");
  70.             smileyHandlerFactory.set(":xD", "wtf");
  71.             smileyHandlerFactory.set(":(", "sad");
  72.             smileyHandlerFactory.set(":)", "smile");
  73.             smileyHandlerFactory.set(":'(", "cry");
  74.             smileyHandlerFactory.set(":@", "mad");
  75.             smileyHandlerFactory.set(":|", "shocked");
  76.             smileyHandlerFactory.set(":o", "ooo");
  77.  
  78.             //Creates a new BBCode parser and register some BBCode tagnames with
  79.             //their RichText InlineMarkup counterpart.
  80.             final BBCodeParser bbcode = new BBCodeParser();
  81.             bbcode.addTag("b", InlineMarkup.BOLD);
  82.             bbcode.addTag("i", InlineMarkup.ITALIC);
  83.             bbcode.addTag("u", InlineMarkup.UNDERLINE);
  84.             bbcode.addTag("s", InlineMarkup.STRIKE);
  85.             bbcode.addTag("strike", InlineMarkup.STRIKE);
  86.            
  87.             final Forum forum = new Forum(conn, bbcode);
  88.             final ReplyContainer guestbook = forum.getTopic(1);
  89.  
  90.             ResourceBundle serverprops = ResourceBundle.getBundle("main.server");
  91.             ExchangedRequest.setMaxPostSize(Long.parseLong(serverprops.getString("http.maxPostSize")));
  92.  
  93.             AbstractServer server = new AbstractServer(
  94.                     Integer.parseInt(serverprops.getString("http.port")),
  95.                     Integer.parseInt(serverprops.getString("http.maxConnections"))) {
  96.                 public void handle(Query query, Request request, Response response) throws IOException {
  97.                     try {
  98.                         //System.out.println(query);
  99.                         //System.out.println(request);
  100.                         //System.out.println(response);
  101.  
  102.                         if(request.getRequestMethod() == RequestMethod.POST) {
  103.                             String msg = request.getArgument("msg");
  104.                             if(msg != null) {
  105.                                 System.out.println("MSG-LENGTH: "+msg.length());
  106.                                 if(msg.length()>MAX_MESSAGE_LENGTH) {
  107.                                     msg = msg.substring(0,MAX_MESSAGE_LENGTH);
  108.                                 }
  109.                                 msgs.add(0, msg);
  110.                                 if(msgs.size()>0xFF) {
  111.                                     msgs.pop();
  112.                                 }
  113.                             }
  114.                             response.setHeader("Location", "/");
  115.                             response.sendHeaders(ResponseStatus._303_SEE_OTHER);
  116.                             response.finish();
  117.                             return;
  118.                         }
  119.                         HTMLDocument document = new HTMLDocument(response.getWriter());
  120.  
  121.                         document.openDocument(new Locale("nl"));
  122.  
  123.                         document.add("title", "Gast & Boek");
  124.  
  125.                         document.startBody();
  126.  
  127.                         document.add("h1", "Gast & Boek");
  128.  
  129.                         document.open("form");
  130.                             document.setAttribute("method", "post");
  131.  
  132.                             document.open("textarea");
  133.                             document.setAttribute("name", "msg");
  134.                             document.setAttribute("cols", "70");
  135.                             document.setAttribute("rows", "5");
  136.                             document.close(true);
  137.  
  138.                             document.println();
  139.                             document.println("Note: Max "+MAX_MESSAGE_LENGTH+" tekens!");
  140.  
  141.                             document.add("button", "Post!");
  142.                         document.close();
  143.                        
  144.                         RichTextHandler handler = new HyperlinkParser(
  145.                                 smileyHandlerFactory.create(
  146.                                 new NestedMarkupHelper(
  147.                                 documentHandlerFactory.create(document))));
  148.  
  149.                         for(String msg : msgs) {
  150.                             double millitime = (double)System.nanoTime()/1000000d;
  151.                             bbcode.parse(msg, handler);
  152.                             millitime = (double)System.nanoTime()/1000000d - millitime;
  153.                             document.open("code");
  154.                             document.setAttribute("style", "float:right;");
  155.                             document.print(millitime+"ms");
  156.                             document.close();
  157.                             document.add("hr");
  158.                         }
  159.  
  160.                         document.closeDocument();
  161.                         response.finish();
  162.                     } catch(Exception e) {
  163.                         e.printStackTrace();
  164.                     }
  165.                 }
  166.             };
  167.         } catch(Exception e) {
  168.             e.printStackTrace();
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement