Coverage Report - net.sourceforge.pebble.web.action.SaveStaticPageAction
 
Classes in this File Line Coverage Branch Coverage Complexity
SaveStaticPageAction
0%
0/65
0%
0/16
3.429
 
 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.service.StaticPageService;
 36  
 import net.sourceforge.pebble.service.StaticPageServiceException;
 37  
 import net.sourceforge.pebble.domain.*;
 38  
 import net.sourceforge.pebble.util.SecurityUtils;
 39  
 import net.sourceforge.pebble.util.StringUtils;
 40  
 import net.sourceforge.pebble.web.security.RequireSecurityToken;
 41  
 import net.sourceforge.pebble.web.validation.ValidationContext;
 42  
 import net.sourceforge.pebble.web.view.RedirectView;
 43  
 import net.sourceforge.pebble.web.view.View;
 44  
 import net.sourceforge.pebble.web.view.impl.StaticPageFormView;
 45  
 import org.apache.commons.logging.Log;
 46  
 import org.apache.commons.logging.LogFactory;
 47  
 
 48  
 import javax.servlet.ServletException;
 49  
 import javax.servlet.http.HttpServletRequest;
 50  
 import javax.servlet.http.HttpServletResponse;
 51  
 
 52  
 /**
 53  
  * Saves a static page.
 54  
  *
 55  
  * @author    Simon Brown
 56  
  */
 57  
 @RequireSecurityToken
 58  0
 public class SaveStaticPageAction extends SecureAction {
 59  
 
 60  
   /** the log used by this class */
 61  0
   private static Log log = LogFactory.getLog(SaveStaticPageAction.class);
 62  
 
 63  
   /** the value used if the page is being previewed rather than saved */
 64  
   private static final String PREVIEW = "Preview";
 65  
   private static final String CANCEL = "Cancel";
 66  
 
 67  
   /**
 68  
    * Peforms the processing associated with this action.
 69  
    *
 70  
    * @param request  the HttpServletRequest instance
 71  
    * @param response the HttpServletResponse instance
 72  
    * @return the name of the next view
 73  
    */
 74  
   public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 75  0
     String submitType = request.getParameter("submit");
 76  
 
 77  0
     if (submitType != null && submitType.equalsIgnoreCase(PREVIEW)) {
 78  0
       return previewPage(request);
 79  0
     } else if (submitType != null && submitType.equalsIgnoreCase(CANCEL)) {
 80  0
       return unlockPage(request);
 81  
     } else {
 82  0
       return savePage(request);
 83  
     }
 84  
   }
 85  
 
 86  
   private View previewPage(HttpServletRequest request) throws ServletException {
 87  0
     StaticPage staticPage = getStaticPage(request);
 88  
 
 89  
     // we don't want to actually edit the original whilst previewing
 90  0
     staticPage = (StaticPage)staticPage.clone();
 91  0
     populateStaticPage(staticPage, request);
 92  
 
 93  0
     ValidationContext validationContext = new ValidationContext();
 94  0
     staticPage.validate(validationContext);
 95  0
     getModel().put("validationContext", validationContext);
 96  0
     getModel().put(Constants.STATIC_PAGE_KEY, staticPage);
 97  
 
 98  0
     return new StaticPageFormView();
 99  
   }
 100  
 
 101  
   private View unlockPage(HttpServletRequest request) throws ServletException {
 102  0
     StaticPage staticPage = getStaticPage(request);
 103  0
     StaticPageService service = new StaticPageService();
 104  0
     service.unlock(staticPage);
 105  
 
 106  0
     if (staticPage.isPersistent()) {
 107  0
       return new RedirectView(staticPage.getLocalPermalink());
 108  
     } else {
 109  0
       return new RedirectView(staticPage.getBlog().getUrl() + "viewStaticPages.secureaction");
 110  
     }
 111  
   }
 112  
 
 113  
   private View savePage(HttpServletRequest request) throws ServletException {
 114  0
     StaticPageService service = new StaticPageService();
 115  0
     StaticPage staticPage = getStaticPage(request);
 116  0
     populateStaticPage(staticPage, request);
 117  0
     getModel().put(Constants.STATIC_PAGE_KEY, staticPage);
 118  
 
 119  0
     ValidationContext validationContext = new ValidationContext();
 120  0
     staticPage.validate(validationContext);
 121  
 
 122  0
     if (validationContext.hasErrors())  {
 123  0
       getModel().put("validationContext", validationContext);
 124  0
       return new StaticPageFormView();
 125  
     } else {
 126  
       try {
 127  0
         service.putStaticPage(staticPage);
 128  0
         staticPage.getBlog().info("Static page <a href=\"" + staticPage.getLocalPermalink() + "\">" + staticPage.getTitle() + "</a> saved.");
 129  0
         service.unlock(staticPage);
 130  0
         return new RedirectView(staticPage.getLocalPermalink());
 131  0
       } catch (StaticPageServiceException e) {
 132  0
         log.error(e.getMessage(), e);
 133  
 
 134  0
         return new StaticPageFormView();
 135  
       }
 136  
     }
 137  
   }
 138  
 
 139  
   private StaticPage getStaticPage(HttpServletRequest request) throws ServletException {
 140  0
     Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
 141  0
     String id = request.getParameter("page");
 142  0
     String persistent = request.getParameter("persistent");
 143  
 
 144  0
     if (persistent != null && persistent.equalsIgnoreCase("true")) {
 145  
       try {
 146  0
         StaticPageService service = new StaticPageService();
 147  0
         return service.getStaticPageById(blog, id);
 148  0
       } catch (StaticPageServiceException e) {
 149  0
         throw new ServletException(e);
 150  
       }
 151  
     } else {
 152  0
       return new StaticPage(blog);
 153  
     }
 154  
   }
 155  
 
 156  
   private void populateStaticPage(StaticPage staticPage, HttpServletRequest request) {
 157  0
     String title = request.getParameter("title");
 158  0
     String subtitle = request.getParameter("subtitle");
 159  0
     String body = StringUtils.filterNewlines(request.getParameter("body"));
 160  0
     String tags = request.getParameter("tags");
 161  0
     String originalPermalink = request.getParameter("originalPermalink");
 162  0
     String name = request.getParameter("name");
 163  0
     String author = SecurityUtils.getUsername();
 164  0
     String template = request.getParameter("template");
 165  
 
 166  0
     staticPage.setTitle(title);
 167  0
     staticPage.setSubtitle(subtitle);
 168  0
     staticPage.setBody(body);
 169  0
     staticPage.setTags(tags);
 170  0
     staticPage.setAuthor(author);
 171  0
     staticPage.setOriginalPermalink(originalPermalink);
 172  0
     staticPage.setName(name);
 173  0
     staticPage.setTemplate(template);
 174  0
   }
 175  
 
 176  
   /**
 177  
    * Gets a list of all roles that are allowed to access this action.
 178  
    *
 179  
    * @return  an array of Strings representing role names
 180  
    * @param request
 181  
    */
 182  
   public String[] getRoles(HttpServletRequest request) {
 183  0
     return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE};
 184  
   }
 185  
 
 186  
 }