KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > ajax > AjaxUtils


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on May 29, 2005 1:45:36 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.util.ajax;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Date JavaDoc;
47
48 import net.jforum.entities.Post;
49 import net.jforum.util.SafeHtml;
50 import net.jforum.util.mail.Spammer;
51 import net.jforum.util.preferences.ConfigKeys;
52 import net.jforum.util.preferences.SystemGlobals;
53 import net.jforum.view.forum.common.PostCommon;
54 import freemarker.template.SimpleHash;
55
56 /**
57  * General AJAX utility methods.
58  *
59  * @author Rafael Steil
60  * @version $Id: AjaxUtils.java,v 1.6 2005/11/16 20:39:59 rafaelsteil Exp $
61  */

62 public class AjaxUtils
63 {
64     /**
65      * Sends a test message
66      * @param sender The sender's email address
67      * @param host the smtp host
68      * @param auth if need authorization or not
69      * @param username the smtp server username, if auth is needed
70      * @param password the smtp server password, if auth is needed
71      * @param to the recipient
72      * @return The status message
73      */

74     public static String JavaDoc sendTestMail(String JavaDoc sender, String JavaDoc host, String JavaDoc auth,
75             String JavaDoc username, String JavaDoc password, String JavaDoc to)
76     {
77         // Save the current values
78
String JavaDoc originalHost = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
79         String JavaDoc originalAuth = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_AUTH);
80         String JavaDoc originalUsername = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_USERNAME);
81         String JavaDoc originalPassword = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PASSWORD);
82         String JavaDoc originalSender = SystemGlobals.getValue(ConfigKeys.MAIL_SENDER);
83         
84         // Now put the new ones
85
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, host);
86         SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, auth);
87         SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, username);
88         SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, password);
89         SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, sender);
90         
91         // Send the test mail
92
class TestSpammer extends Spammer {
93             public TestSpammer(final String JavaDoc to) {
94                 super.prepareMessage(new ArrayList JavaDoc() {{ add(to); }}, null, "JForum Test Mail", null);
95             }
96             
97             protected String JavaDoc getMessageText(SimpleHash params, String JavaDoc messageFile) throws Exception JavaDoc {
98                 return ("Test mail from JForum Admin Panel. Sent at " + new Date JavaDoc());
99             }
100         }
101         
102         Spammer s = new TestSpammer(to);
103         
104         try {
105             s.dispatchMessages();
106         }
107         catch (Exception JavaDoc e) {
108             return e.toString();
109         }
110         finally {
111             // Restore the original values
112
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, originalHost);
113             SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, originalAuth);
114             SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, originalUsername);
115             SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, originalPassword);
116             SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, originalSender);
117         }
118         
119         return "OK";
120     }
121     
122     /**
123      * Prepares a message for previwing
124      * @param p the post to preview
125      * @return the formatted post
126      */

127     public static Post previewPost(Post p)
128     {
129         p = PostCommon.preparePostForDisplay(p);
130         
131         if (p.isHtmlEnabled()) {
132             p.setText(SafeHtml.makeSafe(p.getText()));
133         }
134         
135         return p;
136     }
137 }
138
Popular Tags