alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ConfigurationWebHandler.java,v 1.34.2.1 2005/06/17 17:49:51 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.34.2.1 $
 * $Date: 2005/06/17 17:49:51 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 */

package com.mvnforum.admin;

import java.io.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.mvnforum.*;
import com.mvnforum.auth.*;
import freemarker.template.*;
import net.myvietnam.mvncore.MVNCoreConfig;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ConfigurationWebHandler
{
    private static Log log = LogFactory.getLog(ConfigurationWebHandler.class);

    private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();

    public ConfigurationWebHandler() {
    }

    public void updateConfigStepOne(HttpServletRequest request)
        throws BadInputException, DocumentException, IOException,
        AssertionException, DatabaseException, AuthenticationException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        // children of mvnforumconfig
        String mvnforum_home = ParamUtil.getParameterSafe(request, "mvnforum_home", true);
        String mvnforum_log = ParamUtil.getParameterSafe(request, "mvnforum_log", true);
        String webmaster_email = ParamUtil.getParameterEmail(request, "webmaster_email");
        String logo_url = ParamUtil.getParameterSafe(request, "logo_url", true);
        String supported_locales = ParamUtil.getParameterSafe(request, "supported_locales", false);
        String default_locale_name = ParamUtil.getParameterSafe(request, "default_locale_name", true);
        String default_guest_name = ParamUtil.getParameterSafe(request, "default_guest_name", true);
        int default_guest_timezone = ParamUtil.getParameterInt(request, "default_guest_timezone");
        String enable_passwordless_auth = ParamUtil.getParameterSafe(request, "enable_passwordless_auth", true);
        String enable_login_info_in_cookie = ParamUtil.getParameterSafe(request, "enable_login_info_in_cookie", true);
        String enable_login_info_in_session = ParamUtil.getParameterSafe(request, "enable_login_info_in_session", true);
        String enable_login_info_in_realm = ParamUtil.getParameterSafe(request, "enable_login_info_in_realm", true);
        String enable_login_info_in_customization = ParamUtil.getParameterSafe(request, "enable_login_info_in_customization", true);
        String require_activation = ParamUtil.getParameterSafe(request, "require_activation", true);
        String enable_login = ParamUtil.getParameterSafe(request, "enable_login", true);
        String enable_new_member = ParamUtil.getParameterSafe(request, "enable_new_member", true);
        String enable_new_post = ParamUtil.getParameterSafe(request, "enable_new_post", true);
        String enable_rss = ParamUtil.getParameterSafe(request, "enable_rss", true);
        String enable_watch = ParamUtil.getParameterSafe(request, "enable_watch", true);
        String enable_attachment = ParamUtil.getParameterSafe(request, "enable_attachment", true);
        String enable_avatar = ParamUtil.getParameterSafe(request, "enable_avatar", true);
        String enable_emoticon = ParamUtil.getParameterSafe(request, "enable_emoticon", true);
        String enable_captcha = ParamUtil.getParameterSafe(request, "enable_captcha", true);
        String enable_portal_like_index_page = ParamUtil.getParameterSafe(request, "enable_portal_like_index_page", true);

        String enable_search = ParamUtil.getParameterSafe(request, "enable_search", true);
        String enable_online_users = ParamUtil.getParameterSafe(request, "enable_online_users", true);
        String enable_duplicate_onlineusers = ParamUtil.getParameterSafe(request, "enable_duplicate_onlineusers", true);
        String enable_invisible_users = ParamUtil.getParameterSafe(request, "enable_invisible_users", true);
        String enable_listmembers = ParamUtil.getParameterSafe(request, "enable_listmembers", true);
        String enable_show_last_login = ParamUtil.getParameterSafe(request, "enable_show_last_login", true);
        String enable_private_message = ParamUtil.getParameterSafe(request, "enable_private_message", true);
        String enable_public_message = ParamUtil.getParameterSafe(request, "enable_public_message", true);
        String enable_message_attachment = ParamUtil.getParameterSafe(request, "enable_message_attachment", true);
        String enable_most_active_threads = ParamUtil.getParameterSafe(request, "enable_most_active_threads", true);
        String enable_most_active_members = ParamUtil.getParameterSafe(request, "enable_most_active_members", true);
        String enable_site_statistics_overview = ParamUtil.getParameterSafe(request, "enable_site_statistics_overview", true);
        String enable_admin_can_change_password = ParamUtil.getParameterSafe(request, "enable_admin_can_change_password", true);
        String enable_guest_view_image_attachment = ParamUtil.getParameterSafe(request, "enable_guest_view_image_attachment", true);

        int default_watch_option = ParamUtil.getParameterInt(request, "default_watch_option");
        int default_moderation_option = ParamUtil.getParameterInt(request, "default_moderation_option");

        // children of mvnforumfactoryconfig
        String member_implementation = ParamUtil.getParameterSafe(request, "member_implementation", true);
        String onlineuser_implementation = ParamUtil.getParameterSafe(request, "onlineuser_implementation", true);
        String authenticator_implementation = ParamUtil.getParameterSafe(request, "authenticator_implementation", false);
        String requestprocessor_implementation = ParamUtil.getParameterSafe(request, "requestprocessor_implementation", true);
        String lucene_analyzer_implementation = ParamUtil.getParameterSafe(request, "lucene_analyzer_implementation", true);

        // Now checking the validity of value
        Locale locale = onlineUser.getLocale();

        try {
            // always create a dir, if the dir already exitsted, nothing happens
            FileUtil.createDirs(mvnforum_home, true);

            String tempFilename = mvnforum_home + File.separatorChar + "mvnforum_tempfile.tmp";
            File tempFile = new File(tempFilename);
            if (log.isDebugEnabled()) {
                log.debug("Temp file = " + tempFilename);
                log.debug("Absolute filename of temp file = " + tempFile.getAbsolutePath());
            }

            FileOutputStream fos = new FileOutputStream(tempFilename);
            fos.write(tempFilename.getBytes());
            fos.close();

            tempFile.delete();
        } catch (IOException ex) {
            String errorMessage = "Check your mvnforum_home. Detail : " + ex.getMessage();
            throw new BadInputException(errorMessage);
        }

        try {
            int lastBackSlashIndex = mvnforum_log.lastIndexOf('\\');
            int lastForwardSlashIndex = mvnforum_log.lastIndexOf('/');
            int index = Math.max(lastBackSlashIndex, lastForwardSlashIndex);
            if (index < 0) {
                throw new BadInputException("Cannot find any '\\' or '/' in mvnforum_log : " + mvnforum_log);
            }
            String parentFolder = mvnforum_log.substring(0, index);
            log.debug("parentFolder = " + parentFolder);

            // always create a dir, if the dir already exitsted, nothing happens
            FileUtil.createDirs(parentFolder, true);

            String tempFilename = parentFolder + File.separatorChar + "mvnforum_tempfile.tmp";
            File tempFile = new File(tempFilename);
            if (log.isDebugEnabled()) {
                log.debug("Temp file in log folder = " + tempFilename);
                log.debug("Absolute filename of temp file in log folder = " + tempFile.getAbsolutePath());
            }

            FileOutputStream fos = new FileOutputStream(tempFilename);
            fos.write(tempFilename.getBytes());
            fos.close();

            tempFile.delete();
        } catch (IOException ex) {
            String errorMessage = "Check your mvnforum_log. Detail : " + ex.getMessage();
            throw new BadInputException(errorMessage);
        }

        MyUtil.checkClassName(locale, member_implementation, true);
        MyUtil.checkClassName(locale, onlineuser_implementation, true);
        MyUtil.checkClassName(locale, authenticator_implementation, false);
        MyUtil.checkClassName(locale, requestprocessor_implementation, true);
        MyUtil.checkClassName(locale, lucene_analyzer_implementation, true);

        // End checking, now save the value
        String strPathName = FileUtil.getServletClassesPath();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(strPathName + "mvnforum.xml"));

        Element root = document.getRootElement();
        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element element = (Element) i.next();
            for (int j = 0, size = element.nodeCount(); j < size; j++) {
                Node node = element.node(j);
                if (node.getNodeTypeName().equals("Element")) {
                    if (node.getName().equals("mvnforum_home")) {
                        node.setText(mvnforum_home);
                    } else if (node.getName().equals("mvnforum_log")) {
                        node.setText(mvnforum_log);
                    } else if (node.getName().equals("webmaster_email")) {
                        node.setText(webmaster_email);
                    } else if (node.getName().equals("logo_url")) {
                        node.setText(logo_url);
                    } else if (node.getName().equals("supported_locales")) {
                        node.setText(supported_locales);
                    } else if (node.getName().equals("default_locale_name")) {
                        node.setText(default_locale_name);
                    } else if (node.getName().equals("default_guest_name")) {
                        node.setText(default_guest_name);
                    } else if (node.getName().equals("default_guest_timezone")) {
                        node.setText(String.valueOf(default_guest_timezone));
                    } else if (node.getName().equals("enable_passwordless_auth")) {
                        node.setText(enable_passwordless_auth);
                    } else if (node.getName().equals("enable_login_info_in_cookie")) {
                        node.setText(enable_login_info_in_cookie);
                    } else if (node.getName().equals("enable_login_info_in_session")) {
                        node.setText(enable_login_info_in_session);
                    } else if (node.getName().equals("enable_login_info_in_realm")) {
                        node.setText(enable_login_info_in_realm);
                    } else if (node.getName().equals("enable_login_info_in_customization")) {
                        node.setText(enable_login_info_in_customization);
                    } else if (node.getName().equals("require_activation")) {
                        node.setText(require_activation);
                    } else if (node.getName().equals("enable_login")) {
                        node.setText(enable_login);
                    } else if (node.getName().equals("enable_new_member")) {
                        node.setText(enable_new_member);
                    } else if (node.getName().equals("enable_new_post")) {
                        node.setText(enable_new_post);
                    } else if (node.getName().equals("enable_rss")) {
                        node.setText(enable_rss);
                    } else if (node.getName().equals("enable_watch")) {
                        node.setText(enable_watch);
                    } else if (node.getName().equals("enable_attachment")) {
                        node.setText(enable_attachment);
                    } else if (node.getName().equals("enable_avatar")) {
                        node.setText(enable_avatar);
                    } else if (node.getName().equals("enable_emoticon")) {
                        node.setText(enable_emoticon);
                    } else if (node.getName().equals("enable_captcha")) {
                        node.setText(enable_captcha);
                    } else if (node.getName().equals("enable_portal_like_index_page")) {
                        node.setText(enable_portal_like_index_page);

                    } else if (node.getName().equals("enable_search")) {
                        node.setText(enable_search);
                    } else if (node.getName().equals("enable_online_users")) {
                        node.setText(enable_online_users);
                    } else if (node.getName().equals("enable_duplicate_onlineusers")) {
                        node.setText(enable_duplicate_onlineusers);
                    } else if (node.getName().equals("enable_invisible_users")) {
                        node.setText(enable_invisible_users);
                    } else if (node.getName().equals("enable_listmembers")) {
                        node.setText(enable_listmembers);
                    } else if (node.getName().equals("enable_show_last_login")) {
                        node.setText(enable_show_last_login);
                    } else if (node.getName().equals("enable_private_message")) {
                        node.setText(enable_private_message);
                    } else if (node.getName().equals("enable_public_message")) {
                        node.setText(enable_public_message);
                    } else if (node.getName().equals("enable_message_attachment")) {
                        node.setText(enable_message_attachment);
                    } else if (node.getName().equals("enable_most_active_threads")) {
                        node.setText(enable_most_active_threads);
                    } else if (node.getName().equals("enable_most_active_members")) {
                        node.setText(enable_most_active_members);
                    } else if (node.getName().equals("enable_site_statistics_overview")) {
                        node.setText(enable_site_statistics_overview);
                    } else if (node.getName().equals("enable_admin_can_change_password")) {
                        node.setText(enable_admin_can_change_password);
                    } else if (node.getName().equals("enable_guest_view_image_attachment")) {
                        node.setText(enable_guest_view_image_attachment);

                    } else if (node.getName().equals("default_watch_option")) {
                        node.setText(String.valueOf(default_watch_option));
                    } else if (node.getName().equals("default_moderation_option")) {
                        node.setText(String.valueOf(default_moderation_option));

                    // children of mvnforumfactoryconfig
                    } else if (node.getName().equals("member_implementation")) {
                        node.setText(member_implementation);
                    } else if (node.getName().equals("onlineuser_implementation")) {
                        node.setText(onlineuser_implementation);
                    } else if (node.getName().equals("authenticator_implementation")) {
                        node.setText(authenticator_implementation);
                    } else if (node.getName().equals("requestprocessor_implementation")) {
                        node.setText(requestprocessor_implementation);
                    } else if (node.getName().equals("lucene_analyzer_implementation")) {
                        node.setText(lucene_analyzer_implementation);
                    }
                }
            }
        }

        saveDocument(document, strPathName + "mvnforum.xml");
        request.setAttribute("step", "1");
    }

    public void updateConfigStepTwo(HttpServletRequest request)
        throws BadInputException, DocumentException, IOException,
        AssertionException, DatabaseException, AuthenticationException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        int max_private_message = ParamUtil.getParameterUnsignedInt(request, "max_private_message");
        String max_message_attachment_size = ParamUtil.getParameterSafe(request, "max_message_attachment_size", true);
        String max_attachment_size = ParamUtil.getParameterSafe(request, "max_attachment_size", true);
        int max_favorite_thread = ParamUtil.getParameterUnsignedInt(request, "max_favorite_thread");
        int max_edit_days = ParamUtil.getParameterUnsignedInt(request, "max_edit_days");
        int max_attach_days = ParamUtil.getParameterUnsignedInt(request, "max_attach_days");
        int max_delete_days = ParamUtil.getParameterUnsignedInt(request, "max_delete_days");
        int rows_per_page = ParamUtil.getParameterUnsignedInt(request, "rows_per_page");
        int rows_per_rss = ParamUtil.getParameterUnsignedInt(request, "rows_per_rss");
        int hot_topic_threshold = ParamUtil.getParameterUnsignedInt(request, "hot_topic_threshold");

        int max_posts_per_hour = ParamUtil.getParameterUnsignedInt(request, "max_posts_per_hour");
        int max_members_per_hour = ParamUtil.getParameterUnsignedInt(request, "max_members_per_hour");
        int max_logins_per_hour = ParamUtil.getParameterUnsignedInt(request, "max_logins_per_hour");
        int max_messages_per_hour = ParamUtil.getParameterUnsignedInt(request, "max_messages_per_hour");

        int max_chars_in_short_summary = ParamUtil.getParameterUnsignedInt(request, "max_chars_in_short_summary");
        int max_chars_in_long_summary = ParamUtil.getParameterUnsignedInt(request, "max_chars_in_long_summary");
        int max_chars_in_rss = ParamUtil.getParameterUnsignedInt(request, "max_chars_in_rss");

        String enable_backup_on_server = ParamUtil.getParameterSafe(request, "enable_backup_on_server", true);
        String max_import_size = ParamUtil.getParameterSafe(request, "max_import_size", true);

        String strPathName = FileUtil.getServletClassesPath();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(strPathName + "mvnforum.xml"));
        Element root = document.getRootElement();

        // iterate through child elements of root
        for (Iterator i = root.elementIterator("mvnforumconfig"); i.hasNext(); ) {
            Element element = (Element) i.next();
            for (int j = 0, size = element.nodeCount(); j < size; j++) {
                Node node = element.node(j);
                if (node.getNodeTypeName().equals("Element")) {
                    if (node.getName().equals("max_private_message")) {
                        node.setText(String.valueOf(max_private_message));
                    } else if (node.getName().equals("max_message_attachment_size")) {
                        node.setText(max_message_attachment_size);
                    } else if (node.getName().equals("max_attachment_size")) {
                        node.setText(max_attachment_size);
                    } else if (node.getName().equals("max_favorite_thread")) {
                        node.setText(String.valueOf(max_favorite_thread));
                    } else if (node.getName().equals("max_edit_days")) {
                        node.setText(String.valueOf(max_edit_days));
                    } else if (node.getName().equals("max_attach_days")) {
                        node.setText(String.valueOf(max_attach_days));
                    } else if (node.getName().equals("max_delete_days")) {
                        node.setText(String.valueOf(max_delete_days));
                    } else if (node.getName().equals("rows_per_page")) {
                        node.setText(String.valueOf(rows_per_page));
                    } else if (node.getName().equals("rows_per_rss")) {
                        node.setText(String.valueOf(rows_per_rss));
                    } else if (node.getName().equals("hot_topic_threshold")) {
                        node.setText(String.valueOf(hot_topic_threshold));

                    } else if (node.getName().equals("max_posts_per_hour")) {
                        node.setText(String.valueOf(max_posts_per_hour));
                    } else if (node.getName().equals("max_members_per_hour")) {
                        node.setText(String.valueOf(max_members_per_hour));
                    } else if (node.getName().equals("max_logins_per_hour")) {
                        node.setText(String.valueOf(max_logins_per_hour));
                    } else if (node.getName().equals("max_messages_per_hour")) {
                        node.setText(String.valueOf(max_messages_per_hour));

                    } else if (node.getName().equals("max_chars_in_short_summary")) {
                        node.setText(String.valueOf(max_chars_in_short_summary));
                    } else if (node.getName().equals("max_chars_in_long_summary")) {
                        node.setText(String.valueOf(max_chars_in_long_summary));
                    } else if (node.getName().equals("max_chars_in_rss")) {
                        node.setText(String.valueOf(max_chars_in_rss));

                    } else if (node.getName().equals("enable_backup_on_server")) {
                        node.setText(enable_backup_on_server);
                    } else if (node.getName().equals("max_import_size")) {
                        node.setText(max_import_size);
                    }
                }
            }
        }
        saveDocument(document, strPathName + "mvnforum.xml");
        request.setAttribute("step", "2");
    }

    public void updateConfigStepThree(HttpServletRequest request)
        throws BadInputException, DocumentException, IOException,
        AssertionException, DatabaseException, AuthenticationException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        String use_datasource = ParamUtil.getParameterSafe(request, "use_datasource", true);
        boolean isUseDatasource = use_datasource.equals("true") ? true : false;

        int database_type = 0;
        String driver_class_name = "";
        String database_url = "";
        String database_user = "";
        String database_password = "";
        int max_connection = 0;
        int max_time_to_wait = 0;
        int minutes_between_refresh = 0;
        String datasource_name = "";

        if (isUseDatasource == false) {
            database_type = ParamUtil.getParameterUnsignedInt(request, "database_type");
            driver_class_name = ParamUtil.getParameterSafe(request, "driver_class_name", true);
            database_url = ParamUtil.getParameterSafe(request, "database_url", true);
            database_user = ParamUtil.getParameterSafe(request, "database_user", true);
            database_password = ParamUtil.getParameterSafe(request, "database_password", false);
            max_connection = ParamUtil.getParameterUnsignedInt(request, "max_connection");
            max_time_to_wait = ParamUtil.getParameterUnsignedInt(request, "max_time_to_wait");
            minutes_between_refresh = ParamUtil.getParameterUnsignedInt(request, "minutes_between_refresh");
        } else {
            datasource_name = ParamUtil.getParameterSafe(request, "datasource_name", false);
        }

        String mail_server = ParamUtil.getParameterSafe(request, "mail_server", true);
        String default_mail_from = ParamUtil.getParameterEmail(request, "default_mail_from");
        String username = ParamUtil.getParameterSafe(request, "username", false);
        String password = ParamUtil.getParameterSafe(request, "password", false);
        int port = ParamUtil.getParameterUnsignedInt(request, "port");

        String context_path = ParamUtil.getParameterSafe(request, "context_path", false);
        String server_path = ParamUtil.getParameterSafe(request, "server_path", true);

        int server_hour_offset = ParamUtil.getParameterInt(request, "server_hour_offset");

        String blocked_user_agent = ParamUtil.getParameterSafe(request, "blocked_user_agent", false);
        String blocked_ip = ParamUtil.getParameterSafe(request, "blocked_ip", false);

        String enable_link_nofollow = ParamUtil.getParameterSafe(request, "enable_link_nofollow", true);

        String mailinterceptor_implementation = ParamUtil.getParameterSafe(request, "mailinterceptor_implementation", false);
        String contentinterceptor_implementation = ParamUtil.getParameterSafe(request, "contentinterceptor_implementation", false);
        String loginidinterceptor_implementation = ParamUtil.getParameterSafe(request, "loginidinterceptor_implementation", false);

        // Now checking the validity of value
        Locale locale = onlineUser.getLocale();

        MyUtil.checkClassName(locale, driver_class_name, false);
        MyUtil.checkClassName(locale, mailinterceptor_implementation, false);
        MyUtil.checkClassName(locale, contentinterceptor_implementation, false);
        MyUtil.checkClassName(locale, loginidinterceptor_implementation, false);

        // End checking, now save the value
        String strPathName = FileUtil.getServletClassesPath();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(strPathName + "mvncore.xml"));
        Element root = document.getRootElement();

        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element element = (Element) i.next();
            for (int j = 0, size = element.nodeCount(); j < size; j++) {
                Node node = element.node(j);
                if (node.getNodeTypeName().equals("Element")) {
                    if (node.getName().equals("use_datasource")) {
                        node.setText(use_datasource);
                    } else if (node.getName().equals("database_type")) {
                        if (isUseDatasource == false) {
                            node.setText(String.valueOf(database_type));
                        }
                    } else if (node.getName().equals("driver_class_name")) {
                        if (isUseDatasource == false) {
                            node.setText(driver_class_name);
                        }
                    } else if (node.getName().equals("database_url")) {
                        if (isUseDatasource == false) {
                            node.setText(database_url);
                        }
                    } else if (node.getName().equals("database_user")) {
                        if (isUseDatasource == false) {
                            node.setText(database_user);
                        }
                    } else if (node.getName().equals("database_password")) {
                        if (isUseDatasource == false) {
                            node.setText(database_password);
                        }
                    } else if (node.getName().equals("max_connection")) {
                        if (isUseDatasource == false) {
                            node.setText(String.valueOf(max_connection));
                        }
                    } else if (node.getName().equals("max_time_to_wait")) {
                        if (isUseDatasource == false) {
                            node.setText(String.valueOf(max_time_to_wait));
                        }
                    } else if (node.getName().equals("minutes_between_refresh")) {
                        if (isUseDatasource == false) {
                            node.setText(String.valueOf(minutes_between_refresh));
                        }
                    } else if (node.getName().equals("datasource_name")) {
                        if (isUseDatasource) {
                            node.setText(datasource_name);
                        }

                    } else if (node.getName().equals("mail_server")) {
                        node.setText(mail_server);
                    } else if (node.getName().equals("default_mail_from")) {
                        node.setText(default_mail_from);
                    } else if (node.getName().equals("username")) {
                        node.setText(username);
                    } else if (node.getName().equals("password")) {
                        node.setText(password);
                    } else if (node.getName().equals("port")) {
                        node.setText(String.valueOf(port));

                    } else if (node.getName().equals("context_path")) {
                        node.setText(context_path);
                    } else if (node.getName().equals("server_path")) {
                        node.setText(server_path);

                    } else if (node.getName().equals("server_hour_offset")) {
                        node.setText(String.valueOf(server_hour_offset));

                    } else if (node.getName().equals("blocked_user_agent")) {
                        node.setText(blocked_user_agent);
                    } else if (node.getName().equals("blocked_ip")) {
                        node.setText(blocked_ip);

                    } else if (node.getName().equals("enable_link_nofollow")) {
                        node.setText(enable_link_nofollow);

                    } else if (node.getName().equals("mailinterceptor_implementation")) {
                        node.setText(mailinterceptor_implementation);
                    } else if (node.getName().equals("contentinterceptor_implementation")) {
                        node.setText(contentinterceptor_implementation);
                    } else if (node.getName().equals("loginidinterceptor_implementation")) {
                        node.setText(loginidinterceptor_implementation);
                    }
                }
            }
        }
        saveDocument(document, strPathName + "mvncore.xml");
        request.setAttribute("step", "3");
    }

    public void updateUrlPattern(HttpServletRequest request)
        throws BadInputException, DocumentException, IOException,
        AssertionException, AuthenticationException, DatabaseException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        String admin_url = ParamUtil.getParameterSafe(request, "admin_url", true);
        String user_url = ParamUtil.getParameterSafe(request, "user_url", true);

        String strPathName = FileUtil.getServletClassesPath();
        SAXReader reader = new SAXReader();
        String xmlFilePath = strPathName + "mvnforum.xml";
        Document document = reader.read(new File(xmlFilePath));

        Element root = document.getRootElement();
        for (int i = 0; i < root.nodeCount(); i++) {
            Node node = root.node(i);
            //log.debug("Node " + i + " = " + node.getName());
            if (node.getNodeTypeName().equals("Element")) {
                if (node.getName().equals("adminmoduleconfig")) {
                    Node subElement = ((Element) node).element("url_pattern");
                    //log.debug("sub element " + subElement);
                    subElement.setText(admin_url);
                } else if (node.getName().equals("usermoduleconfig")) {
                    Node subElement = ((Element) node).element("url_pattern");
                    //log.debug("sub element " + subElement);
                    subElement.setText(user_url);
                }
            }
        }

        saveDocument(document, xmlFilePath);
        request.setAttribute("action", "updateurls");
    }

    public void configBackupProcess(HttpServletRequest request)
        throws IOException, AssertionException, DatabaseException, AuthenticationException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        String servletClassPath = FileUtil.getServletClassesPath();

        String mvncoreFileName = servletClassPath + File.separator + "mvncore.xml";
        String mvnforumFileName = servletClassPath + File.separator + "mvnforum.xml";

        String pattern = "yyyy-MM-dd_HH-mm-ss";
        String nowDesc = DateUtil.format(new Date(), pattern);
        String newMVNCoreFileName = servletClassPath + File.separator + "mvncore_" + nowDesc + ".xml";
        String newMVNForumFileName = servletClassPath + File.separator + "mvnforum_" + nowDesc + ".xml";

        // We don't have to check files here, we check it in FileUtil.copyFile
        FileUtil.copyFile(mvncoreFileName, newMVNCoreFileName, false);
        FileUtil.copyFile(mvnforumFileName, newMVNForumFileName, false);

        File newMVNCoreFile = new File(newMVNCoreFileName);
        File newMVNForumFile = new File(newMVNForumFileName);

        request.setAttribute("MVNCoreFileName", newMVNCoreFile.getAbsolutePath());
        request.setAttribute("MVNForumFileName", newMVNForumFile.getAbsolutePath());
        request.setAttribute("step", "Backup");
    }

    private void saveDocument(Document doc, String fileName) throws IOException {

        XMLWriter writer = new XMLWriter(new FileWriter(fileName));
        writer.write(doc);
        writer.close();
    }

    public void prepareEditTemplate(HttpServletRequest request)
        throws AuthenticationException, DatabaseException, AssertionException,
        IOException, FileNotFoundException, BadInputException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        String templateName = ParamUtil.getParameterSafe(request, "template", false);
        StringUtil.checkGoodName(templateName);
        String subject = "";
        String body = "";
        final String SUBJECT_SUFFIX = "-subject.ftl";
        final String BODY_SUFFIX = "-body.ftl";

        if (templateName.equals("")) {
            templateName = MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_PREFIX;
        }
        // checking valid input here
        if ((templateName.equals(MVNForumGlobal.TEMPLATE_FORGOTPASSWORD_PREFIX) == false) &&
            (templateName.equals(MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_PREFIX) == false) &&
            (templateName.equals(MVNForumGlobal.TEMPLATE_WATCHMAIL_PREFIX) == false) ) {
            throw new AssertionException("Does not accept invalid input: " + templateName);
        }

        String templateDir = MVNForumConfig.getTemplateDir();
        String subjectFileName = templateDir + File.separator + templateName + SUBJECT_SUFFIX;
        String bodyFileName    = templateDir + File.separator + templateName + BODY_SUFFIX;

//        subject = new String(FileUtil.getBytes(new FileInputStream(subjectFileName)), "UTF-8");
//        body    = new String(FileUtil.getBytes(new FileInputStream(bodyFileName)) , "UTF-8");
        subject = FileUtil.readFile(subjectFileName, "UTF-8");
        body    = FileUtil.readFile(bodyFileName, "UTF-8");

        request.setAttribute("TemplateSubject", subject);
        request.setAttribute("TemplateBody", body);
    }

    public void processEditTemplate(HttpServletRequest request)
        throws DatabaseException, AuthenticationException, IOException,
        FileNotFoundException, AssertionException, BadInputException, TemplateException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        final String SUBJECT_SUFFIX = "-subject.ftl";
        final String BODY_SUFFIX = "-body.ftl";

        String templateName = ParamUtil.getParameterSafe(request, "template", true);
        StringUtil.checkGoodName(templateName);
        // checking valid input here
        if ((templateName.equals(MVNForumGlobal.TEMPLATE_FORGOTPASSWORD_PREFIX) == false) &&
            (templateName.equals(MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_PREFIX) == false) &&
            (templateName.equals(MVNForumGlobal.TEMPLATE_WATCHMAIL_PREFIX) == false) ) {
            throw new AssertionException("Does not accept invalid input: " + templateName);
        }

        String subject = ParamUtil.getParameter(request, "subject", true);
        String body    = ParamUtil.getParameter(request, "body", true);

        String templateDir = MVNForumConfig.getTemplateDir();

        String subjectFileName = templateDir + File.separator + templateName + SUBJECT_SUFFIX;
        String bodyFileName    = templateDir + File.separator + templateName + BODY_SUFFIX;

        // just for test freemakers
        SimpleHash root = new SimpleHash();
        try {
            Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration();
            if (templateName.equals(MVNForumGlobal.TEMPLATE_SENDACTIVATECODE_PREFIX)) {
                root.put("serverName", "");
                root.put("MVNForumInfo", "");
                root.put("activationUrl", "");
                root.put("memberName", "");
                root.put("activateCode", "");
            } else if (templateName.equals(MVNForumGlobal.TEMPLATE_FORGOTPASSWORD_PREFIX)) {
                root.put("serverName", "");
                root.put("MVNForumInfo", "");
                root.put("passwordResetUrl", "");
                root.put("memberName", "");
                root.put("currentTempPassword", "");
            } else if (templateName.equals(MVNForumGlobal.TEMPLATE_WATCHMAIL_PREFIX)) {
                root.put("lastSent", "");
                root.put("now", "");
                root.put("forumBase", "");

                ArrayList list = new ArrayList();
                SimpleHash subroot = new SimpleHash();
                subroot.put("leader", true);
                subroot.put("categoryName", "");
                subroot.put("forumName", "");
                subroot.put("threadTopic", "");
                subroot.put("memberName", "");
                subroot.put("lastPostMemberName", "");
                subroot.put("threadLastPostDate", "");
                subroot.put("threadUrl", "");
                list.add(subroot);

                root.put("threadWatchList", list);
            } else {
                throw new AssertionException("Does not accept invalid input: " + templateName);
            }
            StringReader subjectReader = new StringReader(subject);
            StringReader bodyReader = new StringReader(body);

            Template subjectTemplate = new Template("", subjectReader, cfg, "");
            StringWriter subjectWriter = new StringWriter(256);
            subjectTemplate.process(root, subjectWriter);

            Template bodyTemplate = new Template("", bodyReader, cfg, "");
            StringWriter bodyWriter = new StringWriter(1024);
            bodyTemplate.process(root, bodyWriter);
        } catch(TemplateException ex) {
            throw ex;
        }
        FileUtil.writeFile(subject, subjectFileName, "UTF-8");
        FileUtil.writeFile(body, bodyFileName, "UTF-8");

        request.setAttribute("Successful", "true");
        request.setAttribute("TemplateSubject", subject);
        request.setAttribute("TemplateBody", body);
    }

    void commitConfig(HttpServletRequest request)
        throws DatabaseException, AuthenticationException, AssertionException {

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();
        MVNForumConfig.reload();
        MVNCoreConfig.reload();
        request.setAttribute("action", "commit");
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.