| 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.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 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
@RequireSecurityToken |
| 58 | 0 | public class SaveStaticPageAction extends SecureAction { |
| 59 | |
|
| 60 | |
|
| 61 | 0 | private static Log log = LogFactory.getLog(SaveStaticPageAction.class); |
| 62 | |
|
| 63 | |
|
| 64 | |
private static final String PREVIEW = "Preview"; |
| 65 | |
private static final String CANCEL = "Cancel"; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 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 | |
|
| 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 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public String[] getRoles(HttpServletRequest request) { |
| 183 | 0 | return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE}; |
| 184 | |
} |
| 185 | |
|
| 186 | |
} |