| 1 |  |  | 
            
  
    | 2 |  |  | 
            
  
    | 3 |  |  | 
            
  
    | 4 |  |  | 
            
  
    | 5 |  |  | 
            
  
    | 6 |  |  | 
            
  
    | 7 |  |  | 
            
  
    | 8 |  |  | 
            
  
    | 9 |  |  | 
            
  
    | 10 |  |  | 
            
  
    | 11 |  |  | 
            
  
    | 12 |  |  | 
            
  
    | 13 |  |  | 
            
  
    | 14 |  |  | 
            
  
    | 15 |  |  | 
            
  
    | 16 |  |  | 
            
  
    | 17 |  |  | 
            
  
    | 18 |  |  | 
            
  
    | 19 |  |  | 
            
  
    | 20 |  |  | 
            
  
    | 21 |  |  | 
            
  
    | 22 |  |  | 
            
  
    | 23 |  |  | 
            
  
    | 24 |  |  | 
            
  
    | 25 |  |  | 
            
  
    | 26 |  |  | 
            
  
    | 27 |  |  | 
            
  
    | 28 |  |  | 
            
  
    | 29 |  |  | 
            
  
    | 30 |  |  | 
            
  
    | 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.controller.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 |  |  | 
            
  
    | 61 |  |  | 
            
  
    | 62 |  | @author | 
            
  
    | 63 |  |  | 
            
  
    | 64 |  | @RequireSecurityToken | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 156 (156) | Complexity: 36 | Complexity Density: 0,31 |  | 
  
  
    | 65 |  | public class SaveBlogEntryAction extends SecureAction { | 
            
  
    | 66 |  |  | 
            
  
    | 67 |  |  | 
            
  
    | 68 |  | private static Log log = LogFactory.getLog(SaveBlogEntryAction.class); | 
            
  
    | 69 |  |  | 
            
  
    | 70 |  |  | 
            
  
    | 71 |  | private static final String PREVIEW = "Preview"; | 
            
  
    | 72 |  |  | 
            
  
    | 73 |  |  | 
            
  
    | 74 |  |  | 
            
  
    | 75 |  |  | 
            
  
    | 76 |  | @param | 
            
  
    | 77 |  | @param | 
            
  
    | 78 |  | @return | 
            
  
    | 79 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 6 (6) | Complexity: 3 | Complexity Density: 0,75 |  | 
  
  
    | 80 | 0 |  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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 7 (7) | Complexity: 1 | Complexity Density: 0,14 |  | 
  
  
    | 90 | 0 |  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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 21 (21) | Complexity: 3 | Complexity Density: 0,16 |  | 
  
  
    | 103 | 0 |  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 | 0 | 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 |  | } 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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 13 (13) | Complexity: 4 | Complexity Density: 0,36 |  | 
  
  
    | 133 | 0 |  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 | 0 | try { | 
            
  
    | 141 | 0 | return service.getBlogEntry(blog, id); | 
            
  
    | 142 |  | } 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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 67 (67) | Complexity: 14 | Complexity Density: 0,27 |  | 
  
  
    | 152 | 0 |  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 |  |  | 
            
  
    | 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 | 0 | 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 |  | } catch (ParseException pe) { | 
            
  
    | 181 | 0 | log.warn(pe); | 
            
  
    | 182 | 0 | blogEntry.setDate(now); | 
            
  
    | 183 |  | } | 
            
  
    | 184 |  | } else { | 
            
  
    | 185 |  |  | 
            
  
    | 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 |  | } else { | 
            
  
    | 222 | 0 | blogEntry.setAttachment(null); | 
            
  
    | 223 |  | } | 
            
  
    | 224 |  | } | 
            
  
    | 225 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 34 (34) | Complexity: 10 | Complexity Density: 0,45 |  | 
  
  
    | 226 | 0 |  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 | 0 | 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 |  | } catch (IOException e) { | 
            
  
    | 248 | 0 | log.warn("Could not get details for attachment located at " + absoluteAttachmentUrl + " : " + e.getMessage()); | 
            
  
    | 249 |  | } | 
            
  
    | 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 |  |  | 
            
  
    | 264 |  |  | 
            
  
    | 265 |  | @return | 
            
  
    | 266 |  | @param | 
            
  
    | 267 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 268 | 0 |  public String[] getRoles(HttpServletRequest request) {... | 
            
  
    | 269 | 0 | return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE}; | 
            
  
    | 270 |  | } | 
            
  
    | 271 |  |  | 
            
  
    | 272 |  | } |