savestaticpageaction.java

### File Source: savestaticpageaction.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.action.SaveStaticPageAction__static_init
              *
              *  Postconditions:
              *    init'ed(log)
              */
         1  /*
         2   * Copyright (c) 2003-2006, 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
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.action.SaveStaticPageAction()
              */
        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  public class SaveStaticPageAction extends SecureAction {
        59
        60    /** the log used by this class */
        61    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 {
                 /*
        P/P       *  Method: View process(HttpServletRequest, HttpServletResponse)
                  *
                  *  Preconditions:
                  *    request != null
                  *    (soft) log != null
                  *    (soft) this.model != null
                  *    (soft) this.model.data != null
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{&new StaticPageFormView(previewPage#2),&new RedirectView(unlockPage#2),&new RedirectView(unlockPage#3),&new StaticPageFormView(savePage#3),&new RedirectView(savePage#5),&new StaticPageFormView(savePage#6)}
                  *    new RedirectView(savePage#5) num objects <= 1
                  *    new RedirectView(unlockPage#2) num objects <= 1
                  *    new RedirectView(unlockPage#3) num objects <= 1
                  *    new StaticPageFormView(previewPage#2) num objects <= 1
                  *    new StaticPageFormView(savePage#3) num objects <= 1
                  *    new StaticPageFormView(savePage#6) num objects <= 1
                  *
                  *  Test Vectors:
                  *    java.lang.String:equalsIgnoreCase(...)@77: {0}, {1}
                  *    java.lang.String:equalsIgnoreCase(...)@79: {0}, {1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@75: Addr_Set{null}, Inverse{null}
                  */
        75      String submitType = request.getParameter("submit");
        76
        77      if (submitType != null && submitType.equalsIgnoreCase(PREVIEW)) {
        78        return previewPage(request);
        79      } else if (submitType != null && submitType.equalsIgnoreCase(CANCEL)) {
        80        return unlockPage(request);
        81      } else {
        82        return savePage(request);
        83      }
        84    }
        85
        86    private View previewPage(HttpServletRequest request) throws ServletException {
                 /*
        P/P       *  Method: View previewPage(HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Postconditions:
                  *    return_value == &new StaticPageFormView(previewPage#2)
                  *    new StaticPageFormView(previewPage#2) num objects == 1
                  */
        87      StaticPage staticPage = getStaticPage(request);
        88
        89      // we don't want to actually edit the original whilst previewing
    +   90      staticPage = (StaticPage)staticPage.clone();
        91      populateStaticPage(staticPage, request);
        92
        93      ValidationContext validationContext = new ValidationContext();
        94      staticPage.validate(validationContext);
        95      getModel().put("validationContext", validationContext);
        96      getModel().put(Constants.STATIC_PAGE_KEY, staticPage);
        97
        98      return new StaticPageFormView();
        99    }
       100
       101    private View unlockPage(HttpServletRequest request) throws ServletException {
                 /*
        P/P       *  Method: View unlockPage(HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.domain.StaticPage:getBlog(...)@109 != null
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{&new RedirectView(unlockPage#3),&new RedirectView(unlockPage#2)}
                  *    new RedirectView(unlockPage#2) num objects <= 1
                  *    new RedirectView(unlockPage#3) num objects <= 1
                  *
                  *  Test Vectors:
                  *    net.sourceforge.pebble.domain.StaticPage:isPersistent(...)@106: {0}, {1}
                  */
       102      StaticPage staticPage = getStaticPage(request);
       103      StaticPageService service = new StaticPageService();
    +  104      service.unlock(staticPage);
       105
       106      if (staticPage.isPersistent()) {
       107        return new RedirectView(staticPage.getLocalPermalink());
       108      } else {
       109        return new RedirectView(staticPage.getBlog().getUrl() + "viewStaticPages.secureaction");
       110      }
       111    }
       112
       113    private View savePage(HttpServletRequest request) throws ServletException {
                 /*
        P/P       *  Method: View savePage(HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *    (soft) log != null
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.domain.StaticPage:getBlog(...)@128 != null
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{&new RedirectView(savePage#5),&new StaticPageFormView(savePage#6),&new StaticPageFormView(savePage#3)}
                  *    new RedirectView(savePage#5) num objects <= 1
                  *    new StaticPageFormView(savePage#3) num objects <= 1
                  *    new StaticPageFormView(savePage#6) num objects <= 1
                  */
       114      StaticPageService service = new StaticPageService();
       115      StaticPage staticPage = getStaticPage(request);
    +  116      populateStaticPage(staticPage, request);
       117      getModel().put(Constants.STATIC_PAGE_KEY, staticPage);
       118
       119      ValidationContext validationContext = new ValidationContext();
       120      staticPage.validate(validationContext);
       121
       122      if (validationContext.hasErrors())  {
       123        getModel().put("validationContext", validationContext);
       124        return new StaticPageFormView();
       125      } else {
       126        try {
       127          service.putStaticPage(staticPage);
       128          staticPage.getBlog().info("Static page <a href=\"" + staticPage.getLocalPermalink() + "\">" + staticPage.getTitle() + "</a> saved.");
       129          service.unlock(staticPage);
       130          return new RedirectView(staticPage.getLocalPermalink());
       131        } catch (StaticPageServiceException e) {
       132          log.error(e.getMessage(), e);
       133
       134          return new StaticPageFormView();
       135        }
       136      }
       137    }
       138
       139    private StaticPage getStaticPage(HttpServletRequest request) throws ServletException {
                 /*
        P/P       *  Method: StaticPage getStaticPage(HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *    new StaticPage(getStaticPage#3) num objects <= 1
                  *
                  *  Test Vectors:
                  *    java.lang.String:equalsIgnoreCase(...)@144: {0}, {1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@142: Addr_Set{null}, Inverse{null}
                  */
       140      Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
       141      String id = request.getParameter("page");
       142      String persistent = request.getParameter("persistent");
       143
       144      if (persistent != null && persistent.equalsIgnoreCase("true")) {
       145        try {
       146          StaticPageService service = new StaticPageService();
       147          return service.getStaticPageById(blog, id);
       148        } catch (StaticPageServiceException e) {
       149          throw new ServletException(e);
       150        }
       151      } else {
       152        return new StaticPage(blog);
       153      }
       154    }
       155
       156    private void populateStaticPage(StaticPage staticPage, HttpServletRequest request) {
                 /*
        P/P       *  Method: void populateStaticPage(StaticPage, HttpServletRequest)
                  *
                  *  Preconditions:
                  *    request != null
                  *    staticPage != null
                  */
       157      String title = request.getParameter("title");
       158      String subtitle = request.getParameter("subtitle");
       159      String body = StringUtils.filterNewlines(request.getParameter("body"));
       160      String tags = request.getParameter("tags");
       161      String originalPermalink = request.getParameter("originalPermalink");
       162      String name = request.getParameter("name");
       163      String author = SecurityUtils.getUsername();
       164      String template = request.getParameter("template");
       165
       166      staticPage.setTitle(title);
       167      staticPage.setSubtitle(subtitle);
       168      staticPage.setBody(body);
       169      staticPage.setTags(tags);
       170      staticPage.setAuthor(author);
       171      staticPage.setOriginalPermalink(originalPermalink);
       172      staticPage.setName(name);
       173      staticPage.setTemplate(template);
       174    }
       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) {
                 /*
        P/P       *  Method: String[] getRoles(HttpServletRequest)
                  *
                  *  Presumptions:
                  *    init'ed(net.sourceforge.pebble.Constants.BLOG_CONTRIBUTOR_ROLE)
                  *
                  *  Postconditions:
                  *    return_value == &new String[](getRoles#1)
                  *    new String[](getRoles#1) num objects == 1
                  *    return_value.length == 1
                  *    return_value[0] == net.sourceforge.pebble.Constants.BLOG_CONTRIBUTOR_ROLE
                  *    (soft) init'ed(return_value[0])
                  */
       183      return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE};
       184    }
       185
       186  }

SofCheck Inspector Build Version : 2.22510

savestaticpageaction.java | 2010-Jun-25 19:40:34
savestaticpageaction.class | 2010-Jul-19 20:23:38