Coverage Report - net.sourceforge.pebble.web.action.SaveBlogEntryAction
 
Classes in this File Line Coverage Branch Coverage Complexity
SaveBlogEntryAction
0%
0/121
0%
0/50
5.857
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.web.action;
 33  
 
 34  
 import net.sourceforge.pebble.Constants;
 35  
 import net.sourceforge.pebble.domain.*;
 36  
 import net.sourceforge.pebble.util.SecurityUtils;
 37  
 import net.sourceforge.pebble.util.StringUtils;
 38  
 import net.sourceforge.pebble.web.security.RequireSecurityToken;
 39  
 import net.sourceforge.pebble.web.validation.ValidationContext;
 40  
 import net.sourceforge.pebble.web.view.RedirectView;
 41  
 import net.sourceforge.pebble.web.view.View;
 42  
 import net.sourceforge.pebble.web.view.impl.BlogEntryFormView;
 43  
 import org.apache.commons.httpclient.Header;
 44  
 import org.apache.commons.httpclient.HttpClient;
 45  
 import org.apache.commons.httpclient.methods.HeadMethod;
 46  
 import org.apache.commons.logging.Log;
 47  
 import org.apache.commons.logging.LogFactory;
 48  
 
 49  
 import javax.servlet.ServletException;
 50  
 import javax.servlet.http.HttpServletRequest;
 51  
 import javax.servlet.http.HttpServletResponse;
 52  
 import java.io.IOException;
 53  
 import java.text.DateFormat;
 54  
 import java.text.ParseException;
 55  
 import java.util.Date;
 56  
 import java.util.HashSet;
 57  
 import java.util.Set;
 58  
 
 59  
 /**
 60  
  * Saves a blog entry.
 61  
  *
 62  
  * @author    Simon Brown
 63  
  */
 64  
 @RequireSecurityToken
 65  0
 public class SaveBlogEntryAction extends SecureAction {
 66  
 
 67  
   /** the log used by this class */
 68  0
   private static Log log = LogFactory.getLog(SaveBlogEntryAction.class);
 69  
 
 70  
   /** the value used if the blog entry is being previewed rather than added */
 71  
   private static final String PREVIEW = "Preview";
 72  
 
 73  
   /**
 74  
    * Peforms the processing associated with this action.
 75  
    *
 76  
    * @param request  the HttpServletRequest instance
 77  
    * @param response the HttpServletResponse instance
 78  
    * @return the name of the next view
 79  
    */
 80  
   public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 81  0
     String submitType = request.getParameter("submit");
 82  
 
 83  0
     if (submitType != null && submitType.equalsIgnoreCase(PREVIEW)) {
 84  0
       return previewBlogEntry(request);
 85  
     } else {
 86  0
       return saveBlogEntry(request);
 87  
     }
 88  
   }
 89  
 
 90  
   private View previewBlogEntry(HttpServletRequest request) throws ServletException {
 91  0
     BlogEntry blogEntry = getBlogEntry(request);
 92  
 
 93  0
     populateBlogEntry(blogEntry, request);
 94  
 
 95  0
     ValidationContext validationContext = new ValidationContext();
 96  0
     blogEntry.validate(validationContext);
 97  0
     getModel().put("validationContext", validationContext);
 98  0
     getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
 99  
 
 100  0
     return new BlogEntryFormView();
 101  
   }
 102  
 
 103  
   private View saveBlogEntry(HttpServletRequest request) throws ServletException {
 104  0
     BlogEntry blogEntry = getBlogEntry(request);
 105  0
     Blog blog = blogEntry.getBlog();
 106  
 
 107  0
     populateBlogEntry(blogEntry, request);
 108  
 
 109  0
     ValidationContext context = new ValidationContext();
 110  0
     blogEntry.validate(context);
 111  
 
 112  0
     getModel().put("validationContext", context);
 113  0
     getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
 114  
 
 115  0
     if (context.hasErrors())  {
 116  0
       return new BlogEntryFormView();
 117  
     } else {
 118  0
       BlogService service = new BlogService();
 119  
       try {
 120  0
         service.putBlogEntry(blogEntry);
 121  0
         blog.info("Blog entry <a href=\"" + blogEntry.getLocalPermalink() + "\">" + blogEntry.getTitle() + "</a> saved.");
 122  0
         getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
 123  0
         return new RedirectView(blogEntry.getLocalPermalink());
 124  0
       } catch (BlogServiceException be) {
 125  0
         log.error(be.getMessage(), be);
 126  0
         context.addError(be.getMessage());
 127  0
         be.printStackTrace();
 128  0
         return new BlogEntryFormView();
 129  
       }
 130  
     }
 131  
   }
 132  
 
 133  
   private BlogEntry getBlogEntry(HttpServletRequest request) throws ServletException {
 134  0
     Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
 135  0
     String id = request.getParameter("entry");
 136  0
     String persistent = request.getParameter("persistent");
 137  
 
 138  0
     if (persistent != null && persistent.equalsIgnoreCase("true")) {
 139  0
       BlogService service = new BlogService();
 140  
       try {
 141  0
         return service.getBlogEntry(blog, id);
 142  0
       } catch (BlogServiceException e) {
 143  0
         throw new ServletException(e);
 144  
       }
 145  
     } else {
 146  0
       BlogEntry blogEntry = new BlogEntry(blog);
 147  0
       blogEntry.setAuthor(SecurityUtils.getUsername());
 148  0
       return blogEntry;
 149  
     }
 150  
   }
 151  
 
 152  
   private void populateBlogEntry(BlogEntry blogEntry, HttpServletRequest request) {
 153  0
     Blog blog = (Blog)request.getAttribute(Constants.BLOG_KEY);
 154  0
     String title = StringUtils.stripScriptTags(request.getParameter("title"));
 155  0
     String subtitle = StringUtils.stripScriptTags(request.getParameter("subtitle"));
 156  0
     String body = StringUtils.filterNewlines(request.getParameter("body"));
 157  0
     String excerpt = StringUtils.filterNewlines(request.getParameter("excerpt"));
 158  0
     String originalPermalink = request.getParameter("originalPermalink");
 159  0
     String tags = request.getParameter("tags");
 160  0
     String commentsEnabled = request.getParameter("commentsEnabled");
 161  0
     String trackBacksEnabled = request.getParameter("trackBacksEnabled");
 162  0
     String category[] = request.getParameterValues("category");
 163  0
     String timeZone = request.getParameter("timeZone");
 164  
 
 165  
     // the date can only set on those entries that have not yet been persisted
 166  0
     if (!blogEntry.isPersistent()) {
 167  0
       DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, blog.getLocale());
 168  0
       dateFormat.setTimeZone(blog.getTimeZone());
 169  0
       dateFormat.setLenient(false);
 170  
 
 171  0
       Date now = new Date();
 172  0
       String dateAsString = request.getParameter("date");
 173  0
       if (dateAsString != null && dateAsString.length() > 0) {
 174  
         try {
 175  0
           Date date = dateFormat.parse(dateAsString);
 176  0
           if (date.after(now)) {
 177  0
             date = now;
 178  
           }
 179  0
           blogEntry.setDate(date);
 180  0
         } catch (ParseException pe) {
 181  0
           log.warn(pe);
 182  0
           blogEntry.setDate(now);
 183  0
         }
 184  
       } else {
 185  
         // the date has been blanked out, so reset to "now"
 186  0
         blogEntry.setDate(now);
 187  
       }
 188  
     }
 189  
 
 190  0
     blogEntry.setTimeZoneId(timeZone);
 191  0
     blogEntry.setTitle(title);
 192  0
     blogEntry.setSubtitle(subtitle);
 193  0
     blogEntry.setBody(body);
 194  0
     blogEntry.setExcerpt(excerpt);
 195  0
     Set categories = new HashSet();
 196  0
     if (category != null) {
 197  0
       for (int i = 0; i < category.length; i++) {
 198  0
         categories.add(blog.getCategory(category[i]));
 199  
       }
 200  
     }
 201  0
     blogEntry.setCategories(categories);
 202  0
     blogEntry.setTags(tags);
 203  0
     blogEntry.setOriginalPermalink(originalPermalink);
 204  0
     if (commentsEnabled != null && commentsEnabled.equalsIgnoreCase("true")) {
 205  0
       blogEntry.setCommentsEnabled(true);
 206  
     } else {
 207  0
       blogEntry.setCommentsEnabled(false);
 208  
     }
 209  0
     if (trackBacksEnabled != null && trackBacksEnabled.equalsIgnoreCase("true")) {
 210  0
       blogEntry.setTrackBacksEnabled(true);
 211  
     } else {
 212  0
       blogEntry.setTrackBacksEnabled(false);
 213  
     }
 214  
 
 215  0
     String attachmentUrl = request.getParameter("attachmentUrl");
 216  0
     String attachmentSize = request.getParameter("attachmentSize");
 217  0
     String attachmentType = request.getParameter("attachmentType");
 218  0
     if (attachmentUrl != null && attachmentUrl.length() > 0) {
 219  0
       Attachment attachment = populateAttachment(blogEntry, attachmentUrl, attachmentSize, attachmentType);
 220  0
       blogEntry.setAttachment(attachment);
 221  0
     } else {
 222  0
       blogEntry.setAttachment(null);
 223  
     }
 224  0
   }
 225  
 
 226  
   private Attachment populateAttachment(BlogEntry blogEntry, String attachmentUrl, String attachmentSize, String attachmentType) {
 227  0
     if (attachmentSize == null || attachmentSize.length() == 0) {
 228  0
       String absoluteAttachmentUrl =  attachmentUrl;
 229  
       try {
 230  0
         HttpClient httpClient = new HttpClient();
 231  0
         if (absoluteAttachmentUrl.startsWith("./")) {
 232  0
           absoluteAttachmentUrl = blogEntry.getBlog().getUrl() + absoluteAttachmentUrl.substring(2);
 233  
         }
 234  
 
 235  0
         HeadMethod headMethod = new HeadMethod(absoluteAttachmentUrl);
 236  0
         int status = httpClient.executeMethod(headMethod);
 237  0
         if (status == 200) {
 238  0
           Header attachmentSizeHeader = headMethod.getResponseHeader("Content-Length");
 239  0
           if (attachmentSizeHeader != null) {
 240  0
             attachmentSize = attachmentSizeHeader.getValue();
 241  
           }
 242  0
           Header attachmentTypeHeader = headMethod.getResponseHeader("Content-Type");
 243  0
           if (attachmentTypeHeader != null) {
 244  0
             attachmentType = attachmentTypeHeader.getValue();
 245  
           }
 246  
         }
 247  0
       } catch (IOException e) {
 248  0
         log.warn("Could not get details for attachment located at " + absoluteAttachmentUrl + " : " + e.getMessage());
 249  0
       }
 250  
     }
 251  
 
 252  0
     Attachment attachment = new Attachment();
 253  0
     attachment.setUrl(attachmentUrl);
 254  0
     if (attachmentSize != null && attachmentSize.length() > 0) {
 255  0
       attachment.setSize(Long.parseLong(attachmentSize));
 256  
     }
 257  0
     attachment.setType(attachmentType);
 258  
 
 259  0
     return attachment;
 260  
   }
 261  
 
 262  
   /**
 263  
    * Gets a list of all roles that are allowed to access this action.
 264  
    *
 265  
    * @return  an array of Strings representing role names
 266  
    * @param request
 267  
    */
 268  
   public String[] getRoles(HttpServletRequest request) {
 269  0
     return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE};
 270  
   }
 271  
 
 272  
 }