kwashc.blog.database.Database.java Source code

Java tutorial

Introduction

Here is the source code for kwashc.blog.database.Database.java

Source

/*
 * Copyright 2012 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kwashc.blog.database;

import kwashc.blog.model.Comment;
import kwashc.blog.model.User;
//import org.apache.commons.lang.StringEscapeUtils;

import javax.servlet.ServletException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * This blog system uses a very basic in-memory database, consisting of a map og users, and a set of comments.
 */
public class Database {

    private static final Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());

    private static final Set<Comment> comments = Collections.synchronizedSet(new TreeSet<Comment>());

    static {
        // initial content:
        //        User user = new User("username", "password");
        User user = new User("username", createHash("password"));
        users.put("username", user);
        //        users.put("anotheruser", new User("anotheruser", "anotherpassword"));
        users.put("anotheruser", new User("anotheruser", createHash("anotherpassword")));
        // system user
        users.put("system", new User("system", createHash("xik74659bs7zw6t59sw6508w")));

        comments.add(new Comment(user, "http://www.google.com/", "Test message",
                "This is a test message, already residing in the database."));
    }

    public static Comment getComment(int ID) throws ServletException {
        for (Comment comment : comments) {
            if (ID == comment.getID()) {
                return comment;
            }
        }
        throw new ServletException("Comment with ID=" + ID + " not found. Program error or failed attack?");
    }

    public static void deleteComment(int ID) throws ServletException {
        Comment comment = getComment(ID);
        comments.remove(comment);
    }

    public static void addComment(Comment comment) {
        comments.add(comment);
    }

    public static User getUser(String username) {
        return users.get(username);
    }

    public static Set<Comment> getComments() {
        return comments;
    }

    //    public static List getComment() {
    //        List cs = new ArrayList();
    //        for (Comment c : comments) {
    //            Comment com = new Comment(new User(c.getAuthor().getUsername(), ""), c.getHomepage(), c.getTitle(),
    //                    c.getComment());
    //            cs.add(com);
    //        }
    //        return cs;
    //    }

    public static String escapeChar(String input) {
        //        StringEscapeUtils.escapeHtml(input);
        input.replaceAll("&", "&amp;");
        input.replaceAll("<", "&lt;");
        input.replaceAll(">", "&gt;");
        input.replaceAll(String.valueOf('"'), "&quot;");
        input.replaceAll("'", "&#x27; ");
        input.replaceAll("/", "&#x2F;");
        return input;
    }

    public static String createHash(String text) {
        try {
            return new String(MessageDigest.getInstance("SHA-512").digest(text.getBytes()));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}