Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.uniregensburg.inett;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.io.IOException;
- import java.sql.*;
- import java.text.SimpleDateFormat;
- import java.util.Locale;
- import java.util.Scanner;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.ocpsoft.prettytime.PrettyTime;
- @WebServlet("/")
- public class BlogServlet extends HttpServlet
- {
- private static final long serialVersionUID = 1L;
- private static final int excerptLength = 20;
- private static final String siteTitle = "InetT Aufgabe 7";
- private static final String siteName = "InetT Aufgabe 7";
- // The DB-Connection variable (gets configured in init())
- private static Connection databaseConnection;
- // HTTP Parameter names and values
- private static final String postIDParam = "pst_id";
- private static final String postTitleParam = "pst_title";
- private static final String postContentParam = "pst_text";
- private static final String postTagParam = "pst_tags";
- private static final String postCreationDateParam = "pst_creationdate";
- private static final String postCommentAmountParam = "pst_cmtcount";
- private static final String commentIDParam = "cmt_id";
- private static final String commentAuthorParam = "cmt_author";
- private static final String commentTextParam = "cmt_text";
- private static final String commentCreationDateParam = "cmt_creationdate";
- private static final String actionParam = "action";
- private static final String actionCreateView = "createview";
- private static final String actionEditView = "editview";
- private static final String actionCreateEdit = "createedit";
- private static final String actionDelete = "delete";
- private static final String actionCreateComment = "createcmt";
- private static final String actionDeleteComment = "deletecmt";
- // HTML Replacement Tags
- private static final String tagTagList = "##TAGLIST##";
- private static final String tagPostID = "##POSTID##";
- private static final String tagCSS = "##CSS##";
- private static final String tagTitle = "##TITLE##";
- private static final String tagSiteName = "##SITENAME##";
- private static final String tagDynamicContent = "##DYNAMIC_CONTENT##";
- private static final String tagPostLink = "##POSTLINK##";
- private static final String tagEditButtonIMG = "##EDITBUTTONIMG##";
- private static final String tagReadOnIMG = "##READONIMG##";
- private static final String tagPostText = "##POSTTEXT##";
- private static final String tagCommentAmount = "##COMMENTAMOUNT##";
- private static final String tagPostAge = "##POSTAGE##";
- private static final String tagPostTitle = "##POSTTITLE##";
- private static final String tagPostLinkComment = "##POSTLINKCOMMENT##";
- private static final String tagPostEditLink = "##POSTEDITLINK##";
- private static final String tagPostDeleteLink = "##POSTDELETELINK##";
- private static final String tagPostList = "##POSTLIST##";
- private static final String tagPostExcerpt = "##POSTEXCERPT##";
- private static final String tagCommentLink = "##COMMENTLINK##";
- private static final String tagCreateEditLink = "##CREATEEDITLINK##";
- private static final String tagCommentList = "##COMMENTLIST##";
- private static final String tagCommentText = "##COMMENTTEXT##";
- private static final String tagCommentID = "##COMMENTID##";
- private static final String tagCommentAuthor = "##COMMENTAUTHOR##";
- private static final String tagCommentAge = "##COMMENTAGE##";
- private static final String tagCommentDeleteLink = "##COMMENTDELETELINK##";
- private static final String tagCommentCreateAction = "##COMMENTCREATEACTION##";
- private static final String site_static_template_path = "/WEB-INF/html_templates/site_static.txt";
- private static final String post_container_template_path = "/WEB-INF/html_templates/post_container.txt";
- private static final String post_view_template_path = "/WEB-INF/html_templates/post_view.txt";
- private static final String single_post_template_path = "/WEB-INF/html_templates/single_post.txt";
- private static final String create_edit_view_template_path = "/WEB-INF/html_templates/create_edit_view.txt";
- private static final String site_404_template_path = "/WEB-INF/html_templates/site_404.txt";
- private static final String single_comment_template_path = "/WEB-INF/html_templates/single_comment.txt";
- private String site_static_template;
- private String post_container_template;
- private String post_view_template;
- private String single_post_template;
- private String create_edit_view_template;
- private String site_404_template;
- private String single_comment_template;
- public BlogServlet()
- {
- super();
- }
- public void init(ServletConfig config) throws ServletException
- {
- super.init(config);
- // Database connection
- String databaseLocation = "localhost";
- String databasePort = "3306";
- String databaseName = "blog";
- String databaseUser = "root";
- String databasePassword = "dbpass";
- try
- {
- String connectionString = "jdbc:mysql://" + databaseLocation + ":" + databasePort + "/" + databaseName + "?user=" + databaseUser + "&password=" + databasePassword;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- databaseConnection = DriverManager.getConnection(connectionString);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- try
- {
- // Loading the HTML templates from file
- InputStream input;
- input = getServletContext().getResourceAsStream(site_static_template_path);
- site_static_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(site_404_template_path);
- site_404_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(post_container_template_path);
- post_container_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(post_view_template_path);
- post_view_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(single_post_template_path);
- single_post_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(single_comment_template_path);
- single_comment_template = readAll(input);
- input.close();
- input = getServletContext().getResourceAsStream(create_edit_view_template_path);
- create_edit_view_template = readAll(input);
- input.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void destroy()
- {
- super.destroy();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- handleRequest(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- handleRequest(request, response);
- }
- void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- String responseString = "";
- String self = request.getRequestURL().toString();
- PrettyTime timeFormatter = new PrettyTime(new Locale("de"));
- // Getting the HTTP Parameters
- String postID = request.getParameter(postIDParam);
- String commentID = request.getParameter(commentIDParam);
- String action = request.getParameter(actionParam);
- String postTitle = request.getParameter(postTitleParam);
- String postContent = request.getParameter(postContentParam);
- // Getting the basic site template into our response
- String site_static = site_static_template;
- site_static = site_static.replace(tagTitle, siteTitle);
- site_static = site_static.replace(tagSiteName, siteName);
- responseString += site_static;
- if (postTitle == null && postContent == null && action == null)
- {
- if (postID == null)
- {
- // String to write the post list into
- String postList = "";
- // Getting the list of all posts from the database
- ResultSet rs = executeStoredProcedure("getAllPosts");
- responseString = responseString.replace(tagDynamicContent, indentEverythingBy(post_container_template, 12));
- try
- {
- while (rs.next())
- {
- // Replacing the tags with its values
- String single_post_temp = single_post_template;
- single_post_temp = single_post_temp.replace(tagPostID, String.valueOf(rs.getInt(postIDParam)));
- single_post_temp = single_post_temp.replace(tagPostEditLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "&" + actionParam + "=" + actionEditView);
- single_post_temp = single_post_temp.replace(tagPostDeleteLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "&" + actionParam + "=" + actionDelete);
- single_post_temp = single_post_temp.replace(tagPostLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)));
- single_post_temp = single_post_temp.replace(tagPostAge, timeFormatter.format((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(rs.getString(postCreationDateParam))));
- single_post_temp = single_post_temp.replace(tagCommentAmount, rs.getInt(postCommentAmountParam) == 1 ? (String.valueOf(rs.getInt(postCommentAmountParam)) + " Kommentar") : (String.valueOf(rs.getInt(postCommentAmountParam)) + " Kommentare"));
- single_post_temp = single_post_temp.replace(tagPostLinkComment, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "#respond");
- single_post_temp = single_post_temp.replace(tagPostTitle, rs.getString(postTitleParam));
- single_post_temp = single_post_temp.replace(tagTagList, rs.getString(postTagParam));
- single_post_temp = single_post_temp.replace(tagPostExcerpt, rs.getString(postContentParam).substring(0, Math.min(rs.getString(postContentParam).length(), excerptLength)));
- postList += single_post_temp + "\n";
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- postList = postList.substring(0, postList.length() - 1);
- responseString = responseString.replace(tagPostList, indentEverythingBy(postList, 24));
- }
- else
- {
- ResultSet rs = executeStoredProcedure("getPost", postID);
- try
- {
- if (rs.next())
- {
- String post_view_temp = post_view_template;
- post_view_temp = post_view_temp.replace(tagPostID, String.valueOf(rs.getInt(postIDParam)));
- post_view_temp = post_view_temp.replace(tagPostEditLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "&" + actionParam + "=" + actionEditView);
- post_view_temp = post_view_temp.replace(tagPostDeleteLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "&" + actionParam + "=" + actionDelete);
- post_view_temp = post_view_temp.replace(tagPostLink, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)));
- post_view_temp = post_view_temp.replace(tagPostAge, timeFormatter.format((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(rs.getString(postCreationDateParam))));
- post_view_temp = post_view_temp.replace(tagCommentAmount, rs.getInt(postCommentAmountParam) == 1 ? (String.valueOf(rs.getInt(postCommentAmountParam)) + " Kommentar") : (String.valueOf(rs.getInt(postCommentAmountParam)) + " Kommentare"));
- post_view_temp = post_view_temp.replace(tagPostLinkComment, self + "?" + postIDParam + "=" + String.valueOf(rs.getInt(postIDParam)) + "#respond");
- post_view_temp = post_view_temp.replace(tagPostTitle, rs.getString(postTitleParam));
- post_view_temp = post_view_temp.replace(tagTagList, rs.getString(postTagParam));
- post_view_temp = post_view_temp.replace(tagPostText, rs.getString(postContentParam));
- post_view_temp = post_view_temp.replace(tagCommentLink, self);
- post_view_temp = post_view_temp.replace(tagCommentCreateAction, actionCreateComment);
- rs = executeStoredProcedure("getPostComments", postID);
- String commentList = "";
- while (rs.next())
- {
- String single_comment_temp = single_comment_template;
- single_comment_temp = single_comment_temp.replace(tagCommentID, String.valueOf(rs.getInt(commentIDParam)));
- single_comment_temp = single_comment_temp.replace(tagCommentAuthor, rs.getString(commentAuthorParam) == "" ? "Anonym" : rs.getString(commentAuthorParam));
- single_comment_temp = single_comment_temp.replace(tagCommentAge, timeFormatter.format((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(rs.getString(commentCreationDateParam))));
- single_comment_temp = single_comment_temp.replace(tagCommentDeleteLink, self + "?" + commentIDParam + "=" + String.valueOf(rs.getInt(commentIDParam)) + "&" + actionParam + "=" + actionDeleteComment);
- single_comment_temp = single_comment_temp.replace(tagCommentText, rs.getString(commentTextParam));
- commentList += single_comment_temp + "\n";
- }
- commentList = commentList.substring(0, commentList.length() - 1);
- post_view_temp = post_view_temp.replace(tagCommentList, indentEverythingBy(commentList, 20));
- responseString = responseString.replace(tagDynamicContent, indentEverythingBy(post_view_temp, 12));
- // ID valid
- // Post-View
- }
- else
- {
- // 404
- responseString = responseString.replace(tagDynamicContent, indentEverythingBy(site_404_template, 12));
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- else if (action == actionCreateView)
- {
- // Create / Edit Page in Create-Mode (Title and Content empty, postID = 0)
- }
- else if (action == actionEditView)
- {
- // Create / Edit Page in Edit-Mode (Title and Content prefilled, postID accordingly)
- }
- else if (action == actionCreateEdit)
- {
- if (postTitle != null && postContent != null && postTitle != "" && postContent != "")
- {
- if(postID == "0")
- {
- // Create new post
- }
- else
- {
- // Check for valid ID / 404
- // Edit existing post
- }
- // Post-View
- }
- else
- {
- // Title or content unfilled Page
- }
- }
- else if (action == actionDelete)
- {
- // Check for valid ID / 404
- // Delete Post from database
- // Overview Page
- }
- PrintWriter writer = response.getWriter();
- writer.write(responseString);
- writer.close();
- }
- public ResultSet executeStoredProcedure(String name, String parameters)
- {
- try
- {
- CallableStatement proc = databaseConnection.prepareCall("{call " + name + "(" + parameters + ")}");
- return proc.executeQuery();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public ResultSet executeStoredProcedure(String name)
- {
- return executeStoredProcedure(name, "");
- }
- public String indentEverythingBy(String text, int indent)
- {
- text = text.replace("\n", "\n" + repeatChar(' ', indent));
- return text;
- }
- public String repeatChar(char c, int length)
- {
- String temp = "";
- for (int i = 0; i < length; i++)
- {
- temp += c;
- }
- return temp;
- }
- public String readAll (InputStream input)
- {
- Scanner s = new Scanner(input);
- s.useDelimiter("\\A");
- String temp = s.hasNext() ? s.next() : "";
- s.close();
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement