KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > actions > WeblogEntryFormAction


1
2 package org.roller.presentation.weblog.actions;
3
4 import java.io.BufferedReader JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.io.OutputStreamWriter JavaDoc;
8 import java.net.HttpURLConnection JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.net.URLConnection JavaDoc;
11 import java.net.URLEncoder JavaDoc;
12 import java.sql.Timestamp JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.ListIterator JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts.action.ActionError;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35 import org.apache.struts.actions.DispatchAction;
36 import org.roller.RollerException;
37 import org.roller.RollerPermissionsException;
38 import org.roller.model.IndexManager;
39 import org.roller.model.Roller;
40 import org.roller.model.RollerSpellCheck;
41 import org.roller.model.UserManager;
42 import org.roller.model.WeblogManager;
43 import org.roller.pojos.CommentData;
44 import org.roller.pojos.UserData;
45 import org.roller.pojos.WeblogEntryData;
46 import org.roller.pojos.WebsiteData;
47 import org.roller.presentation.MainPageAction;
48 import org.roller.presentation.RollerContext;
49 import org.roller.presentation.RollerRequest;
50 import org.roller.presentation.pagecache.PageCacheFilter;
51 import org.roller.presentation.velocity.PageHelper;
52 import org.roller.presentation.weblog.formbeans.WeblogEntryFormEx;
53 import org.roller.util.Utilities;
54
55 import com.swabunga.spell.event.SpellCheckEvent;
56
57
58
59 /////////////////////////////////////////////////////////////////////////////
60
/**
61  * Supports Weblog Entry form actions edit, remove, update, etc.
62  *
63  * @struts.action name="weblogEntryFormEx" path="/editor/weblog"
64  * scope="request" parameter="method"
65  *
66  * @struts.action-forward name="weblogEdit.page" path="/weblog/WeblogEdit.jsp"
67  * @struts.action-forward name="weblogEntryRemove.page" path="/weblog/WeblogEntryRemove.jsp"
68  */

69 public final class WeblogEntryFormAction extends DispatchAction
70 {
71     private static Log mLogger =
72         LogFactory.getFactory().getInstance(WeblogEntryFormAction.class);
73     
74     //-----------------------------------------------------------------------
75
/**
76      * Allow user to create a new weblog entry.
77      */

78     public ActionForward create(
79         ActionMapping mapping,
80         ActionForm actionForm,
81         HttpServletRequest JavaDoc request,
82         HttpServletResponse JavaDoc response)
83         throws IOException JavaDoc, ServletException JavaDoc
84     {
85         ActionForward forward = mapping.findForward("weblogEdit.page");
86         try
87         {
88             RollerRequest rreq = RollerRequest.getRollerRequest(request);
89             if ( rreq.isUserAuthorizedToEdit() )
90             {
91                 WeblogEntryFormEx form = (WeblogEntryFormEx)actionForm;
92                 form.initNew(request, response);
93                 
94                 request.setAttribute("model",
95                         new WeblogEntryPageModel(request, response, mapping,
96                                 (WeblogEntryFormEx)actionForm,
97                                 WeblogEntryPageModel.EDIT_MODE));
98             }
99             else
100             {
101                 forward = mapping.findForward("access-denied");
102             }
103         }
104         catch (Exception JavaDoc e)
105         {
106             request.getSession().getServletContext().log("ERROR",e);
107             throw new ServletException JavaDoc(e);
108         }
109         return forward;
110     }
111
112     //-----------------------------------------------------------------------
113
/**
114      * Allow user to edit a weblog entry.
115      */

116     public ActionForward edit(
117         ActionMapping mapping,
118         ActionForm actionForm,
119         HttpServletRequest JavaDoc request,
120         HttpServletResponse JavaDoc response)
121         throws IOException JavaDoc, ServletException JavaDoc
122     {
123         ActionForward forward = mapping.findForward("weblogEdit.page");
124         try
125         {
126             RollerRequest rreq = RollerRequest.getRollerRequest(request);
127             if ( rreq.isUserAuthorizedToEdit() )
128             {
129                 WeblogManager wmgr = rreq.getRoller().getWeblogManager();
130                 WeblogEntryData entry = rreq.getWeblogEntry();
131                 WeblogEntryFormEx form = (WeblogEntryFormEx)actionForm;
132                 if (entry == null && form.getId() != null)
133                 {
134                     entry= wmgr.retrieveWeblogEntry(form.getId());
135                 }
136                 form.copyFrom(entry, request.getLocale());
137
138                 request.setAttribute("model",
139                         new WeblogEntryPageModel(request, response, mapping,
140                                 form, WeblogEntryPageModel.EDIT_MODE));
141             }
142             else
143             {
144                 forward = mapping.findForward("access-denied");
145             }
146         }
147         catch (Exception JavaDoc e)
148         {
149             request.getSession().getServletContext().log("ERROR",e);
150             throw new ServletException JavaDoc(e);
151         }
152         return forward;
153     }
154
155     //-----------------------------------------------------------------------
156
public ActionForward preview(
157         ActionMapping mapping,
158         ActionForm actionForm,
159         HttpServletRequest JavaDoc request,
160         HttpServletResponse JavaDoc response)
161         throws IOException JavaDoc, ServletException JavaDoc
162     {
163         // we need to save any new entries before Previewing
164
WeblogEntryFormEx form = (WeblogEntryFormEx)actionForm;
165         if (form.getId() == null)
166         {
167             save(mapping, actionForm, request, response);
168         }
169         return display(WeblogEntryPageModel.PREVIEW_MODE,
170                     mapping, actionForm, request, response);
171     }
172
173     //-----------------------------------------------------------------------
174
public ActionForward returnToEditMode(
175         ActionMapping mapping,
176         ActionForm actionForm,
177         HttpServletRequest JavaDoc request,
178         HttpServletResponse JavaDoc response)
179         throws IOException JavaDoc, ServletException JavaDoc
180     {
181         return display(WeblogEntryPageModel.EDIT_MODE,
182                     mapping, actionForm, request, response);
183     }
184
185     //-----------------------------------------------------------------------
186
private ActionForward display(
187         WeblogEntryPageModel.PageMode mode,
188         ActionMapping mapping,
189         ActionForm actionForm,
190         HttpServletRequest JavaDoc request,
191         HttpServletResponse JavaDoc response)
192         throws IOException JavaDoc, ServletException JavaDoc
193     {
194         ActionForward forward = mapping.findForward("weblogEdit.page");
195         try
196         {
197             RollerRequest rreq = RollerRequest.getRollerRequest(request);
198             if ( rreq.isUserAuthorizedToEdit() )
199             {
200                 request.setAttribute("model",
201                    new WeblogEntryPageModel(request, response, mapping,
202                            (WeblogEntryFormEx)actionForm, mode));
203             }
204             else
205             {
206                 forward = mapping.findForward("access-denied");
207             }
208         }
209         catch (Exception JavaDoc e)
210         {
211             request.getSession().getServletContext().log("ERROR",e);
212             throw new ServletException JavaDoc(e);
213         }
214         return forward;
215     }
216
217     //-----------------------------------------------------------------------
218
/**
219      * Saves weblog entry and flushes page cache so that new entry will appear
220      * on users weblog page.
221      */

222     public ActionForward save(
223         ActionMapping mapping,
224         ActionForm actionForm,
225         HttpServletRequest JavaDoc request,
226         HttpServletResponse JavaDoc response)
227         throws IOException JavaDoc, ServletException JavaDoc
228     {
229         ActionForward forward = mapping.findForward("weblogEdit.page");
230         ActionMessages uiMessages = new ActionMessages();
231         try
232         {
233             RollerRequest rreq = RollerRequest.getRollerRequest(request);
234             if ( rreq.isUserAuthorizedToEdit() )
235             {
236                 UserManager userMgr = rreq.getRoller().getUserManager();
237                 WeblogManager weblogMgr = rreq.getRoller().getWeblogManager();
238
239                 UserData user = rreq.getUser();
240                 WebsiteData site = userMgr.getWebsite( user.getUserName() );
241                 WeblogEntryFormEx wf = (WeblogEntryFormEx)actionForm;
242                 
243                 // I was getting column 'x' cannot be null, so I fixed it here.
244
// A better solution might be to change the table so it allows
245
// nulls for these columns.
246
if (wf.getAllowComments() == null)
247                 {
248                     wf.setAllowComments(Boolean.FALSE);
249                 }
250                 if (wf.getRightToLeft() == null)
251                 {
252                     wf.setRightToLeft(Boolean.FALSE);
253                 }
254                 if (wf.getPinnedToMain() == null)
255                 {
256                     wf.setPinnedToMain(Boolean.FALSE);
257                 }
258                 if (wf.getPublishEntry() == null)
259                 {
260                     wf.setPublishEntry(Boolean.FALSE);
261                 }
262                
263                 WeblogEntryData entry = null;
264                 if (wf.getId() == null || wf.getId().trim().length()==0)
265                 {
266                     entry = new WeblogEntryData();
267                     entry.setWebsite( site );
268                 }
269                 else
270                 {
271                     entry = weblogMgr.retrieveWeblogEntry(wf.getId());
272                     entry.save(); // should throw if save not permitted
273
}
274                 wf.copyTo(entry, request.getLocale(), request.getParameterMap());
275
276                 // Fetch MediaCast content type and length
277
mLogger.debug("Checking MediaCast attributes");
278                 if (!checkMediaCast(entry))
279                 {
280                    mLogger.debug("Invalid MediaCast attributes");
281                    uiMessages.add(null,
282                      new ActionMessage("weblogEdit.message.mediaCastProblem"));
283                 }
284                 else if (mLogger.isDebugEnabled())
285                 {
286                    mLogger.debug("Invalid MediaCast attributes");
287                 }
288                 
289                 // Store value object (creates new or updates existing)
290
entry.setUpdateTime(new Timestamp JavaDoc(new Date JavaDoc().getTime()));
291                 mLogger.debug("Saving entry");
292                 entry.save();
293                 rreq.getRoller().commit();
294
295                 mLogger.debug("Populating form");
296                 wf.copyFrom(entry, request.getLocale());
297                 
298                 reindexEntry(rreq.getRoller(), entry);
299                 
300                 // open up a new session, because we will forward to the edit action
301
//rreq.getRoller().begin(); // begin already called by RequestFilter
302

303                 request.setAttribute(RollerRequest.WEBLOGENTRYID_KEY, entry.getId());
304                  
305                 // Flush the page cache
306
mLogger.debug("Removing from cache");
307                 PageCacheFilter.removeFromCache(request, user);
308                 
309                 // refresh the front page cache
310
MainPageAction.flushMainPageCache();
311
312                 // remove session objects that may be left over from spellchecking
313
HttpSession JavaDoc session = request.getSession(true);
314                 session.removeAttribute("spellCheckEvents");
315                 session.removeAttribute("entryText");
316
317                 request.setAttribute("model",
318                         new WeblogEntryPageModel(request, response, mapping,
319                                 (WeblogEntryFormEx)actionForm,
320                                 WeblogEntryPageModel.EDIT_MODE));
321                 
322                 uiMessages.add(null, new ActionMessage("weblogEdit.changesSaved"));
323                 saveMessages(request, uiMessages);
324                 
325                 mLogger.debug("operation complete");
326             }
327             else
328             {
329                 forward = mapping.findForward("access-denied");
330             }
331         }
332         catch (RollerPermissionsException e)
333         {
334             ActionErrors errors = new ActionErrors();
335             errors.add(null, new ActionError("error.permissions.deniedSave"));
336             saveErrors(request, errors);
337             forward = mapping.findForward("access-denied");
338         }
339         catch (Exception JavaDoc e)
340         {
341             throw new ServletException JavaDoc(e);
342         }
343         return forward;
344     }
345     
346     private boolean checkMediaCast(WeblogEntryData entry)
347     {
348         boolean valid = false;
349         String JavaDoc url = entry.findEntryAttribute("att_mediacast_url");
350         boolean empty = (url == null) || (url.trim().length() == 0);
351         if (!empty)
352         {
353             // fetch MediaCast content type and length
354
valid = false;
355             try
356             {
357                 mLogger.debug("Sending HTTP HEAD");
358                 HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)
359                     new URL JavaDoc(url).openConnection();
360                 mLogger.debug("Getting response code");
361                 con.setRequestMethod("HEAD");
362                 if (con.getResponseCode() != 200)
363                 {
364                     mLogger.debug("Response code indicates error");
365                     mLogger.error("ERROR "+con.getResponseCode()+" return from MediaCast URL");
366                     mLogger.debug(con.getContent().toString());
367                 }
368                 else if (con.getContentType()!=null && con.getContentLength()!=-1)
369                 {
370                     mLogger.debug("Got good reponse and content info");
371                     entry.putEntryAttribute(
372                         "att_mediacast_type", con.getContentType());
373                     entry.putEntryAttribute(
374                         "att_mediacast_length", ""+con.getContentLength());
375                     valid = true;
376                 }
377             }
378             catch (Exception JavaDoc e)
379             {
380                 mLogger.error("ERROR checking MediaCast URL");
381             }
382         }
383         else
384         {
385             mLogger.debug("No MediaCast specified, but that is OK");
386             valid = true;
387         }
388         if (!valid || empty)
389         {
390             mLogger.debug("Removing MediaCast attributes");
391             try
392             {
393                 entry.removeEntryAttribute("att_mediacast_url");
394                 entry.removeEntryAttribute("att_mediacast_type");
395                 entry.removeEntryAttribute("att_mediacast_length");
396             }
397             catch (RollerException e)
398             {
399                 mLogger.error("ERROR removing invalid MediaCast attributes");
400             }
401         }
402         mLogger.debug("operation complete");
403         return valid;
404     }
405     
406
407     //-----------------------------------------------------------------------
408
/**
409      * Responds to request to remove weblog entry. Forwards user to page
410      * that presents the 'are you sure?' question.
411      */

412     public ActionForward removeOk(
413         ActionMapping mapping,
414         ActionForm actionForm,
415         HttpServletRequest JavaDoc request,
416         HttpServletResponse JavaDoc response)
417         throws IOException JavaDoc, ServletException JavaDoc
418     {
419         ActionForward forward = mapping.findForward("weblogEntryRemove.page");
420         try
421         {
422             RollerRequest rreq = RollerRequest.getRollerRequest(request);
423             if ( rreq.isUserAuthorizedToEdit() )
424             {
425                 WeblogEntryFormEx wf = (WeblogEntryFormEx)actionForm;
426                 WeblogEntryData wd =
427                     rreq.getRoller().getWeblogManager().retrieveWeblogEntry(wf.getId());
428                 wf.copyFrom(wd, request.getLocale());
429                 if (wd == null || wd.getId() == null)
430                 {
431                     throw new NullPointerException JavaDoc(
432                         "Unable to find WeblogEntry for " +
433                         request.getParameter(RollerRequest.WEBLOGENTRYID_KEY));
434                 }
435             }
436             else
437             {
438                 forward = mapping.findForward("access-denied");
439             }
440         }
441         catch (Exception JavaDoc e)
442         {
443             throw new ServletException JavaDoc(e);
444         }
445         return forward;
446     }
447
448     //-----------------------------------------------------------------------
449
/**
450      * Responds to request from the 'are you sure you want to remove?' page.
451      * Removes the specified weblog entry and flushes the cache.
452      */

453     public ActionForward remove(
454         ActionMapping mapping,
455         ActionForm actionForm,
456         HttpServletRequest JavaDoc request,
457         HttpServletResponse JavaDoc response)
458         throws IOException JavaDoc, ServletException JavaDoc
459     {
460         try
461         {
462             RollerRequest rreq = RollerRequest.getRollerRequest(request);
463             if ( rreq.isUserAuthorizedToEdit() )
464             {
465                 WeblogManager mgr = rreq.getRoller().getWeblogManager();
466                 WeblogEntryData wd = mgr.retrieveWeblogEntry(request.getParameter("id"));
467                 UserData user = rreq.getUser();
468                 
469                 // remove the index for it
470
wd.setPublishEntry(Boolean.FALSE);
471                 reindexEntry(rreq.getRoller(), wd);
472
473                 // remove entry itself
474
wd.remove();
475                 rreq.getRoller().commit();
476
477                 // flush caches
478
PageCacheFilter.removeFromCache(request, user);
479                 MainPageAction.flushMainPageCache();
480                 
481                 ActionMessages uiMessages = new ActionMessages();
482                 uiMessages.add(null, new ActionMessage("weblogEdit.entryRemoved"));
483                 saveMessages(request, uiMessages);
484             }
485             else
486             {
487                 return mapping.findForward("access-denied");
488             }
489         }
490         catch (Exception JavaDoc e)
491         {
492             throw new ServletException JavaDoc(e);
493         }
494         
495         // need to reset all values to empty (including 'id')
496
actionForm = new WeblogEntryFormEx();
497         request.setAttribute(mapping.getName(), actionForm);
498         return create(mapping, actionForm, request, response);
499     }
500
501     //-----------------------------------------------------------------------
502
public ActionForward correctSpelling(
503         ActionMapping mapping,
504         ActionForm actionForm,
505         HttpServletRequest JavaDoc request,
506         HttpServletResponse JavaDoc response)
507         throws IOException JavaDoc, ServletException JavaDoc
508     {
509         try
510         {
511             RollerRequest rreq = RollerRequest.getRollerRequest(request);
512             if ( rreq.isUserAuthorizedToEdit() )
513             {
514                 HttpSession JavaDoc session = request.getSession(true);
515                 WeblogEntryFormEx wf = (WeblogEntryFormEx)actionForm;
516                 // misspelt words have been submitted
517
if (wf.getReplacementWords() != null &&
518                     wf.getReplacementWords().length > 0)
519                 {
520                     // replace misspelt words with chosen replacement
521
String JavaDoc[] replacementWords = wf.getReplacementWords();
522                     StringBuffer JavaDoc entryText = new StringBuffer JavaDoc(wf.getText());
523
524                     ArrayList JavaDoc events =
525                         (ArrayList JavaDoc) session.getAttribute("spellCheckEvents");
526                     SpellCheckEvent event = null;
527                     String JavaDoc oldWord = null;
528                     String JavaDoc newWord = null;
529                     int start = -1;
530                     int end = -1;
531                     int count = replacementWords.length;
532                     for(ListIterator JavaDoc it=events.listIterator(events.size());
533                         it.hasPrevious();)
534                     {
535                         event = (SpellCheckEvent)it.previous();
536                         oldWord = event.getInvalidWord();
537                         newWord = replacementWords[ --count ];
538                         if (!oldWord.equals(newWord))
539                         {
540                             start = event.getWordContextPosition();
541                             end = start + oldWord.length();
542                             entryText.replace( start, end, newWord );
543                         }
544                     }
545                     wf.setText( entryText.toString() );
546
547                     return save(mapping, wf, request, response);
548                 }
549             }
550         }
551         catch (Exception JavaDoc e)
552         {
553             throw new ServletException JavaDoc(e);
554         }
555         return mapping.findForward("access-denied");
556     }
557     
558     //-----------------------------------------------------------------------
559
public ActionForward spellCheck(
560         ActionMapping mapping,
561         ActionForm actionForm,
562         HttpServletRequest JavaDoc request,
563         HttpServletResponse JavaDoc response)
564         throws IOException JavaDoc, ServletException JavaDoc
565     {
566         ActionForward forward = mapping.findForward("weblogEdit.page");
567         try
568         {
569             RollerRequest rreq = RollerRequest.getRollerRequest(request);
570             if ( rreq.isUserAuthorizedToEdit() )
571             {
572                 HttpSession JavaDoc session = request.getSession(true);
573                 WeblogEntryFormEx wf = (WeblogEntryFormEx)actionForm;
574                 
575                 // we need to save any new entries before SpellChecking
576
if (wf.getId() == null)
577                 {
578                     save(mapping, actionForm, request, response);
579                 }
580
581                 // pass the submitted entry text through the spellchecker
582
ArrayList JavaDoc words =
583                     RollerSpellCheck.getSpellingErrors( wf.getText() );
584                 session.setAttribute("spellCheckEvents", words);
585
586                 request.setAttribute("model",
587                     new WeblogEntryPageModel(
588                        request, response, mapping,
589                        (WeblogEntryFormEx)actionForm,
590                        WeblogEntryPageModel.SPELL_MODE, words));
591             }
592             else
593             {
594                 forward = mapping.findForward("access-denied");
595             }
596         }
597         catch (Exception JavaDoc e)
598         {
599             throw new ServletException JavaDoc(e);
600         }
601         return forward;
602     }
603
604     //-----------------------------------------------------------------------
605
/**
606      * Update selected comments: delete and/or mark as spam.
607      */

608     public ActionForward updateComments(
609         ActionMapping mapping,
610         ActionForm actionForm,
611         HttpServletRequest JavaDoc request,
612         HttpServletResponse JavaDoc response)
613         throws IOException JavaDoc, ServletException JavaDoc
614     {
615         ActionForward forward = mapping.findForward("weblogEdit.page");
616         ActionErrors errors = new ActionErrors();
617         RollerRequest rreq = RollerRequest.getRollerRequest(request);
618         try
619         {
620             if ( rreq.isUserAuthorizedToEdit() )
621             {
622                 WeblogEntryData wd = rreq.getWeblogEntry();
623                 if (wd == null || wd.getId() == null)
624                 {
625                   throw new NullPointerException JavaDoc("Unable to find WeblogEntry for "+
626                         request.getParameter(RollerRequest.WEBLOGENTRYID_KEY));
627                 }
628                 WeblogEntryFormEx form = (WeblogEntryFormEx)actionForm;
629
630                 // If form indicates that comments should be deleted, then delete
631
WeblogManager mgr = rreq.getRoller().getWeblogManager();
632                 String JavaDoc[] deleteIds = form.getDeleteComments();
633                 if (deleteIds != null && deleteIds.length > 0)
634                 {
635                     mgr.removeComments( deleteIds );
636                 }
637
638                 List JavaDoc comments = mgr.getComments( wd.getId(), false ); // spam too
639
if (form.getSpamComments() != null)
640                 {
641                     // comments marked as spam
642
List JavaDoc spamIds = Arrays.asList(form.getSpamComments());
643
644                     // iterate over all comments, check each to see if
645
// is in the spamIds list. If so, mark it as spam.
646
Iterator JavaDoc it = comments.iterator();
647                     while (it.hasNext())
648                     {
649                         CommentData comment = (CommentData)it.next();
650                         if (spamIds.contains(comment.getId()))
651                         {
652                             comment.setSpam(Boolean.TRUE);
653                         }
654                         else
655                         {
656                             comment.setSpam(Boolean.FALSE);
657                         }
658                         comment.save();
659                     }
660                 }
661
662                 rreq.getRoller().commit();
663                 
664                 reindexEntry(rreq.getRoller(), wd);
665                 
666                 request.setAttribute("model",
667                         new WeblogEntryPageModel(request, response, mapping,
668                                 (WeblogEntryFormEx)actionForm,
669                                 WeblogEntryPageModel.EDIT_MODE));
670             }
671             else
672             {
673                 forward = mapping.findForward("access-denied");
674             }
675         }
676         catch (Exception JavaDoc e)
677         {
678             forward = mapping.findForward("error");
679
680             errors.add(ActionErrors.GLOBAL_ERROR,
681                 new ActionError("error.edit.comment", e.toString()));
682             saveErrors(request,errors);
683
684             mLogger.error(getResources(request).getMessage("error.edit.comment")
685                 + e.toString(),e);
686         }
687         return forward;
688     }
689
690     /**
691     *
692     */

693    public ActionForward sendTrackback(
694                    ActionMapping mapping,
695                    ActionForm actionForm,
696                    HttpServletRequest JavaDoc request,
697                    HttpServletResponse JavaDoc response) throws RollerException
698    {
699        ActionForward forward = mapping.findForward("weblogEdit.page");
700        ActionErrors errors = new ActionErrors();
701        WeblogEntryData entry = null;
702        try
703        {
704            RollerRequest rreq = RollerRequest.getRollerRequest(request);
705            if (rreq.isUserAuthorizedToEdit())
706            {
707                WeblogEntryFormEx form = (WeblogEntryFormEx)actionForm;
708                String JavaDoc entryid = form.getId();
709                if ( entryid == null )
710                {
711                    entryid= request.getParameter(RollerRequest.WEBLOGENTRYID_KEY);
712                }
713
714                RollerContext rctx= RollerContext.getRollerContext(request);
715                WeblogManager wmgr= rreq.getRoller().getWeblogManager();
716                entry = wmgr.retrieveWeblogEntry(entryid);
717
718                String JavaDoc title = entry.getTitle();
719
720                // Run entry through registered PagePlugins
721
PageHelper pageHelper = PageHelper.createPageHelper(request, response);
722                pageHelper.setSkipFlag(true); // don't process ReadMorePlugin
723
String JavaDoc excerpt = pageHelper.renderPlugins(entry);
724                excerpt = StringUtils.left( Utilities.removeHTML(excerpt),255 );
725
726                String JavaDoc url = rctx.createEntryPermalink(entry, request, true);
727                String JavaDoc blog_name = entry.getWebsite().getName();
728
729                if (form.getTrackbackUrl() != null)
730                {
731                    try
732                    {
733                        // Construct data
734

735                        String JavaDoc data = URLEncoder.encode("title", "UTF-8")
736                            +"="+URLEncoder.encode(title, "UTF-8");
737
738                        data += ("&" + URLEncoder.encode("excerpt", "UTF-8")
739                                  +"="+URLEncoder.encode(excerpt,"UTF-8"));
740
741                        data += ("&" + URLEncoder.encode("url", "UTF-8")
742                                  +"="+URLEncoder.encode(url,"UTF-8"));
743
744                        data += ("&" + URLEncoder.encode("blog_name", "UTF-8")
745                                  +"="+URLEncoder.encode(blog_name,"UTF-8"));
746
747                        // Send data
748
URL JavaDoc tburl = new URL JavaDoc(form.getTrackbackUrl());
749                        URLConnection JavaDoc conn = tburl.openConnection();
750                        conn.setDoOutput(true);
751
752                        OutputStreamWriter JavaDoc wr =
753                            new OutputStreamWriter JavaDoc(conn.getOutputStream());
754                        BufferedReader JavaDoc rd = null;
755                        try
756                        {
757                            wr.write(data);
758                            wr.flush();
759     
760                            // Get the response
761
rd = new BufferedReader JavaDoc(
762                                    new InputStreamReader JavaDoc(conn.getInputStream()));
763     
764                            String JavaDoc line;
765                            StringBuffer JavaDoc resultBuff = new StringBuffer JavaDoc();
766                            while ((line = rd.readLine()) != null)
767                            {
768                                resultBuff.append(Utilities.escapeHTML(line, true));
769                                resultBuff.append("<br />");
770                            }
771                            
772                            ActionMessages resultMsg = new ActionMessages();
773                            resultMsg.add(ActionMessages.GLOBAL_MESSAGE,
774                                new ActionMessage("weblogEdit.trackbackResults",
775                                resultBuff));
776                            saveMessages(request, resultMsg);
777                        }
778                        finally
779                        {
780                            wr.close();
781                            rd.close();
782                        }
783                    }
784                    catch (IOException JavaDoc e)
785                    {
786                        errors.add(ActionErrors.GLOBAL_ERROR,
787                            new ActionError("error.trackback",e));
788                    }
789                }
790                else
791                {
792                    errors.add(ActionErrors.GLOBAL_ERROR,
793                            new ActionError("error.noTrackbackUrlSpecified"));
794                }
795                
796                form.setTrackbackUrl(null);
797            }
798            else
799            {
800                forward = mapping.findForward("access-denied");
801            }
802        }
803        catch (Exception JavaDoc e) // unexpected
804
{
805            mLogger.error(e);
806            String JavaDoc msg = e.getMessage();
807            if ( msg == null )
808            {
809                msg = e.getClass().getName();
810            }
811            errors.add(ActionErrors.GLOBAL_ERROR,
812                new ActionError("error.general",msg));
813        }
814
815        if ( !errors.isEmpty() )
816        {
817            saveErrors(request, errors);
818        }
819        
820        request.setAttribute("model",
821                        new WeblogEntryPageModel(request, response, mapping,
822                                (WeblogEntryFormEx)actionForm,
823                                WeblogEntryPageModel.EDIT_MODE));
824
825        return forward;
826     }
827
828     //-----------------------------------------------------------------------
829
public ActionForward cancel(
830         ActionMapping mapping,
831         ActionForm actionForm,
832         HttpServletRequest JavaDoc request,
833         HttpServletResponse JavaDoc response)
834         throws IOException JavaDoc, ServletException JavaDoc
835     {
836         return (mapping.findForward("weblogEdit"));
837     }
838
839     //-----------------------------------------------------------------------
840
public ActionForward unspecified(
841         ActionMapping mapping,
842         ActionForm actionForm,
843         HttpServletRequest JavaDoc request,
844         HttpServletResponse JavaDoc response)
845         throws Exception JavaDoc
846     {
847         return create(mapping, actionForm, request, response);
848     }
849     
850     /**
851      * Attempts to remove the Entry from the Lucene index and
852      * then re-index the Entry if it is Published. If the Entry
853      * is being deleted then mark it published = false.
854      * @param entry
855      */

856     private void reindexEntry(Roller roller, WeblogEntryData entry) throws RollerException
857     {
858          IndexManager manager = roller.getIndexManager();
859         
860         // remove entry before (re)adding it, or in case it isn't Published
861
//manager.removeEntryIndexOperation(entry);
862

863         // if published, index the entry
864
if (entry.getPublishEntry() == Boolean.TRUE)
865         {
866             manager.addEntryReIndexOperation(entry);
867         }
868     }
869 }
870
871
Popular Tags