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