KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > tags > PostTag


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.tags;
108
109 import java.util.*;
110 import javax.servlet.*;
111 import javax.servlet.jsp.*;
112 import javax.servlet.jsp.tagext.*;
113 import javax.servlet.http.*;
114 import com.Yasna.forum.*;
115 import com.Yasna.forum.Exceptions.RapidPostingException;
116 import com.Yasna.forum.Exceptions.UserBlackListedException;
117 import com.Yasna.forum.util.ClientIP;
118 import com.Yasna.forum.tags.*;
119 import java.net.URLDecoder JavaDoc;
120
121 /**
122  * JSP Tag <b>post</b>, used to setup for posting a new message or replying
123  * to an existing message using <b>post_message</b> tag.
124  * <p>
125  * Requires that attribute <b>id</b> be set to the name of a
126  * script variable for use in tag body to get message
127  * properties using &lt;jsp:getProperty/&gt;, get extended message propertes
128  * using the <b>yazdGetProperty</b> tag, or set extended message properties
129  * using the <b>yazdSetProperty</b> tag.
130  * <p>
131  * Uses the the following HTTP input parameters
132  * <p><ul>
133  * <li><b>subject</b> - subject of message
134  * <li><b>body</b> - body of message
135  * <li><b>post</b> - this is a new message post
136  * <li><b>reply</b> - this is a reply to a message
137  * </ul>
138  * <p>
139  * The <b>change_forum</b> tag can be used nested within
140  * the <b>post</b> tag to set what forum the message is
141  * to be posted to.
142  * <p>
143  * JSP Tag Lib Descriptor
144  * <p><pre>
145  * &lt;name&gt;post&lt;/name&gt;
146  * &lt;tagclass&gt;com.Yasna.forum.tags.PostTag&lt;/tagclass&gt;
147  * &lt;teiclass&gt;com.Yasna.forum.tags.PostTEI&lt;/teiclass&gt;
148  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
149  * &lt;info&gt;Used to setup for posting a new message.&lt;/info&gt;
150  * &lt;attribute&gt;
151  * &lt;name&gt;id&lt;/name&gt;
152  * &lt;required&gt;true&lt;/required&gt;
153  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
154  * &lt;/attribute&gt;
155  * </pre>
156  *
157  * @see ChangeForumTag
158  * @see PostMessageTag
159  *
160  * @author Glenn Nielsen
161  */

162
163 public class PostTag extends TagSupport implements YazdProperty, ChangeForum
164 {
165     // HTTP input parameters
166
private String JavaDoc subject = null;
167     private String JavaDoc localIP = null;
168     private int type =0;
169     private StringBuffer JavaDoc body = new StringBuffer JavaDoc();
170     private boolean isreply = false;
171     private boolean submit = false;
172     private boolean isPrivate;
173     private boolean isPreview;
174     // Internal
175
private YazdRequest jr = null;
176     private YazdState js = null;
177     private Forum pf = null;
178     private ForumMessage message = null;
179     private Map properties = new HashMap();
180
181     /**
182      * Sets up to post a new message or reply to a message if this is an
183      * HTML form submission with values set for the parameters body, subject,
184      * and post.
185      *
186      * @throws JspException on system level error
187      *
188      * @return EVAL_BODY_INCLUDE
189      */

190     public final int doStartTag() throws JspException
191     {
192         // Initialize YazdState
193
js = (YazdState)pageContext.getAttribute("yazdUserState",
194                 PageContext.SESSION_SCOPE);
195         if( js == null )
196             throw new JspException("Yazd post tag, could not find yazdUserState");
197
198         // Get the user request information
199
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
200                 PageContext.REQUEST_SCOPE);
201         if( jr == null )
202             throw new JspException("Yazd post tag, could not find request");
203
204         // Save the script variable so JSP author can access post message data
205
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
206
207         // Get HTTP input parameters
208
ServletRequest req = pageContext.getRequest();
209         String JavaDoc tmp;
210         tmp = req.getParameter("post");
211         if( tmp != null && tmp.length() > 0 )
212             submit=true;
213         tmp = req.getParameter("reply");
214         if( tmp != null && tmp.length() > 0 ) {
215             submit=true;
216             isreply = true;
217         }
218         tmp = req.getParameter("preview");
219         if( tmp != null && tmp.length() > 0 ) {
220             isPreview = true;
221             submit = false;
222         } else {
223             isPreview = false;
224         }
225         tmp = req.getParameter("isPrivate");
226         if( tmp != null && "true".equals(tmp)) {
227             isPrivate = true;
228         } else {
229             isPrivate = false;
230         }
231
232         String JavaDoc b = req.getParameter("body");
233         String JavaDoc localIP = req.getParameter("localeip");
234         String JavaDoc s = req.getParameter("subject");
235         int type = Integer.parseInt(req.getParameter("type"));
236         if (req.getParameter("isPreview") != null && b != null && s != null) {
237             try {
238                 b = URLDecoder.decode(b, "UTF-8");
239                 s = URLDecoder.decode(s, "UTF-8");
240             } catch(Exception JavaDoc e) {
241                 e.printStackTrace();
242             }
243         }
244
245         subject = s;
246         if (!isPreview) body.append( b );
247
248         // Have the input to post a message
249
return EVAL_BODY_INCLUDE;
250     }
251
252     /**
253      * Message Subject entered in previous HTML form submisson which can be
254      * obtained by the JSP page using
255      * &lt;jsp:getProperty name=<i>"id"</i> property="subject"/&gt;
256      *
257      * @return String - message subject the user entered in the HTML form submitted
258      */

259     public final String JavaDoc getSubject()
260     {
261         if( subject == null )
262             return "";
263         return subject;
264     }
265
266     /**
267      * Method used by the getYazdProperty tag to get an extended Message
268      * property from the post or message tag script variable.
269      *
270      * @return String - value of the property
271      */

272     public final String JavaDoc getProperty(String JavaDoc name)
273     {
274         String JavaDoc tmp = (String JavaDoc)properties.get(name);
275         if( tmp != null )return tmp;
276         return "";
277     }
278
279     /**
280      * Method used by the setYazdProperty tag to set an extended Message
281      * property from the post or message tag script variable.
282      */

283     public final void setProperty(String JavaDoc name, String JavaDoc value)
284     {
285         properties.put(name,value);
286     }
287
288     /**
289      * Message Body entered in previous HTML form submisson which can be
290      * obtained by the JSP page using
291      * &lt;jsp:getProperty name=<i>"id"</i> property="body"/&gt;
292      *
293      * @return String - message body the user entered in the HTML form submitted
294      */

295     public final String JavaDoc getBody()
296     {
297         return body.toString();
298     }
299
300     /**
301      * Append a string to the body of the message,
302      * used by <b>post_append</b> tag. Only appends
303      * the string if the body of message already has
304      * content.
305      *
306      */

307     public final void appendBody(String JavaDoc str)
308     {
309         if( body.length() > 0 )
310             body.append(str);
311     }
312
313     /**
314      * Create a new message for a forum post or reply.
315      *
316      * @return true if message creation succeeded
317      */

318     public final boolean postMessage() throws JspException
319     {
320         ForumMessage parentMessage = null;
321         ForumThread thread = null;
322         Forum cf = jr.getForum();
323         User user = null;
324         String JavaDoc IP ="";
325
326         // Make sure post is triggered from a form submit
327
if( !submit && !isPreview)
328             return false;
329
330         if( body.length() == 0 ||
331                 subject == null || subject.length() == 0 ) {
332             jr.addError("A message must have both a Subject and a Message.");
333             pageContext.getRequest().removeAttribute("showPreview");
334             return false;
335         }
336         IP=pageContext.getRequest().getRemoteAddr();
337         if (isPreview) return false;
338         if( pf == null ) {
339             pf = cf;
340         }
341
342         try {
343             user = jr.getProfileManager().getUser( js.getAuthorization().getUserID() );
344         } catch( UserNotFoundException e ) {
345             throw new JspException("Post Message, user account not found");
346         }
347         try {
348             ClientIP clientIP = new ClientIP(localIP,IP);
349             message = pf.createMessage( user,clientIP );
350             message.setSubject( subject );
351             message.setBody( body.toString() );
352             message.setProperty("IP",IP);
353             if(localIP !=null){
354                 message.setProperty("localIP",localIP);
355             }
356             String JavaDoc tmp;
357             for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) {
358                 tmp = (String JavaDoc)it.next();
359                 message.setProperty(tmp,(String JavaDoc)properties.get(tmp));
360             }
361             if( isreply ) {
362                 try {
363                     thread = cf.getThread( js.getThreadID() );
364                     parentMessage = thread.getMessage( js.getMessageID() );
365                 } catch( ForumThreadNotFoundException e ) {
366                     throw new JspException("Post Message, thread not found with id: " +
367                             js.getThreadID() );
368                 } catch( ForumMessageNotFoundException e ) {
369                     throw new JspException("Post Message, parent not found with id: " +
370                             js.getMessageID() );
371                 }
372                 if (isPrivate) {
373                     message.setReplyPrivateUserId(parentMessage.getUser().getID());
374                 }
375                 thread.addMessage( parentMessage, message );
376             } else {
377                 thread = pf.createThread( message ,jr.getThreadType(type));
378                 pf.addThread( thread );
379             }
380         } catch( UnauthorizedException ue ) {
381             jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed"));
382             return false;
383         } catch( RapidPostingException ue ) {
384             jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.rapid.failed"));
385             return false;
386         } catch( UserBlackListedException ue ) {
387             jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.blacklist.failed"));
388             return false;
389         }
390         return true;
391     }
392
393     /**
394      * Set an alternate forum where message is to be posted.
395      */

396     public final void changeForum(Forum apf)
397     {
398         pf = apf;
399     }
400
401     public final boolean isPreview() {
402         return isPreview;
403     }
404
405 }
406
Popular Tags