File Source: saveblogentryaction.java
/*
P/P * Method: net.sourceforge.pebble.web.action.SaveBlogEntryAction__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.domain.*;
36 import net.sourceforge.pebble.util.SecurityUtils;
37 import net.sourceforge.pebble.util.StringUtils;
38 import net.sourceforge.pebble.web.security.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;
/*
P/P * Method: void net.sourceforge.pebble.web.action.SaveBlogEntryAction()
*/
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 * Saves a blog entry.
61 *
62 * @author Simon Brown
63 */
64 @RequireSecurityToken
65 public class SaveBlogEntryAction extends SecureAction {
66
67 /** the log used by this class */
68 private static Log log = LogFactory.getLog(SaveBlogEntryAction.class);
69
70 /** the value used if the blog entry is being previewed rather than added */
71 private static final String PREVIEW = "Preview";
72
73 /**
74 * Peforms the processing associated with this action.
75 *
76 * @param request the HttpServletRequest instance
77 * @param response the HttpServletResponse instance
78 * @return the name of the next view
79 */
80 public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
/*
P/P * Method: View process(HttpServletRequest, HttpServletResponse)
*
* Preconditions:
* request != null
* this.model != null
* this.model.data != null
* (soft) log != null
*
* Postconditions:
* return_value in Addr_Set{&new BlogEntryFormView(previewBlogEntry#2),&new BlogEntryFormView(saveBlogEntry#2),&new RedirectView(saveBlogEntry#5),&new BlogEntryFormView(saveBlogEntry#6)}
* new BlogEntryFormView(previewBlogEntry#2) num objects <= 1
* new BlogEntryFormView(saveBlogEntry#2) num objects <= 1
* new BlogEntryFormView(saveBlogEntry#6) num objects <= 1
* new RedirectView(saveBlogEntry#5) num objects <= 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@83: {0}, {1}
* javax.servlet.http.HttpServletRequest:getParameter(...)@81: Addr_Set{null}, Inverse{null}
*/
81 String submitType = request.getParameter("submit");
82
83 if (submitType != null && submitType.equalsIgnoreCase(PREVIEW)) {
84 return previewBlogEntry(request);
85 } else {
86 return saveBlogEntry(request);
87 }
88 }
89
90 private View previewBlogEntry(HttpServletRequest request) throws ServletException {
/*
P/P * Method: View previewBlogEntry(HttpServletRequest)
*
* Preconditions:
* request != null
* this.model != null
* this.model.data != null
* (soft) log != null
*
* Postconditions:
* return_value == &new BlogEntryFormView(previewBlogEntry#2)
* new BlogEntryFormView(previewBlogEntry#2) num objects == 1
*/
91 BlogEntry blogEntry = getBlogEntry(request);
92
+ 93 populateBlogEntry(blogEntry, request);
94
95 ValidationContext validationContext = new ValidationContext();
96 blogEntry.validate(validationContext);
97 getModel().put("validationContext", validationContext);
98 getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
99
100 return new BlogEntryFormView();
101 }
102
103 private View saveBlogEntry(HttpServletRequest request) throws ServletException {
/*
P/P * Method: View saveBlogEntry(HttpServletRequest)
*
* Preconditions:
* request != null
* this.model != null
* this.model.data != null
* (soft) log != null
*
* Presumptions:
* net.sourceforge.pebble.domain.BlogEntry:getBlog(...)@105 != null
*
* Postconditions:
* return_value in Addr_Set{&new RedirectView(saveBlogEntry#5),&new BlogEntryFormView(saveBlogEntry#6),&new BlogEntryFormView(saveBlogEntry#2)}
* new BlogEntryFormView(saveBlogEntry#2) num objects <= 1
* new BlogEntryFormView(saveBlogEntry#6) num objects <= 1
* new RedirectView(saveBlogEntry#5) num objects <= 1
*/
104 BlogEntry blogEntry = getBlogEntry(request);
+ 105 Blog blog = blogEntry.getBlog();
106
107 populateBlogEntry(blogEntry, request);
108
109 ValidationContext context = new ValidationContext();
110 blogEntry.validate(context);
111
112 getModel().put("validationContext", context);
113 getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
114
115 if (context.hasErrors()) {
116 return new BlogEntryFormView();
117 } else {
118 BlogService service = new BlogService();
119 try {
120 service.putBlogEntry(blogEntry);
121 blog.info("Blog entry <a href=\"" + blogEntry.getLocalPermalink() + "\">" + blogEntry.getTitle() + "</a> saved.");
122 getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
123 return new RedirectView(blogEntry.getLocalPermalink());
124 } catch (BlogServiceException be) {
125 log.error(be.getMessage(), be);
126 context.addError(be.getMessage());
127 be.printStackTrace();
128 return new BlogEntryFormView();
129 }
130 }
131 }
132
133 private BlogEntry getBlogEntry(HttpServletRequest request) throws ServletException {
/*
P/P * Method: BlogEntry getBlogEntry(HttpServletRequest)
*
* Preconditions:
* request != null
* this.model != null
* this.model.data != null
*
* Postconditions:
* init'ed(return_value)
* new BlogEntry(getBlogEntry#3) num objects <= 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@138: {0}, {1}
* javax.servlet.http.HttpServletRequest:getParameter(...)@136: Addr_Set{null}, Inverse{null}
*/
134 Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
135 String id = request.getParameter("entry");
136 String persistent = request.getParameter("persistent");
137
138 if (persistent != null && persistent.equalsIgnoreCase("true")) {
139 BlogService service = new BlogService();
140 try {
141 return service.getBlogEntry(blog, id);
142 } catch (BlogServiceException e) {
143 throw new ServletException(e);
144 }
145 } else {
146 BlogEntry blogEntry = new BlogEntry(blog);
147 blogEntry.setAuthor(SecurityUtils.getUsername());
148 return blogEntry;
149 }
150 }
151
152 private void populateBlogEntry(BlogEntry blogEntry, HttpServletRequest request) {
/*
P/P * Method: void populateBlogEntry(BlogEntry, HttpServletRequest)
*
* Preconditions:
* blogEntry != null
* request != null
* (soft) log != null
*
* Presumptions:
* category.length@162 <= 232-1
* java.text.DateFormat:getDateTimeInstance(...)@167 != null
* java.text.DateFormat:parse(...)@175 != null
* javax.servlet.http.HttpServletRequest:getAttribute(...)@153 != null
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@204: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@209: {0}, {1}
* java.lang.String:length(...)@173: {0}, {1..232-1}
* java.lang.String:length(...)@218: {0}, {1..232-1}
* java.util.Date:after(...)@176: {0}, {1}
* javax.servlet.http.HttpServletRequest:getParameter(...)@160: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@161: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@172: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@215: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameterValues(...)@162: Addr_Set{null}, Inverse{null}
* ...
*/
153 Blog blog = (Blog)request.getAttribute(Constants.BLOG_KEY);
154 String title = StringUtils.stripScriptTags(request.getParameter("title"));
155 String subtitle = StringUtils.stripScriptTags(request.getParameter("subtitle"));
156 String body = StringUtils.filterNewlines(request.getParameter("body"));
157 String excerpt = StringUtils.filterNewlines(request.getParameter("excerpt"));
158 String originalPermalink = request.getParameter("originalPermalink");
159 String tags = request.getParameter("tags");
160 String commentsEnabled = request.getParameter("commentsEnabled");
161 String trackBacksEnabled = request.getParameter("trackBacksEnabled");
162 String category[] = request.getParameterValues("category");
163 String timeZone = request.getParameter("timeZone");
164
165 // the date can only set on those entries that have not yet been persisted
166 if (!blogEntry.isPersistent()) {
167 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, blog.getLocale());
168 dateFormat.setTimeZone(blog.getTimeZone());
169 dateFormat.setLenient(false);
170
171 Date now = new Date();
172 String dateAsString = request.getParameter("date");
173 if (dateAsString != null && dateAsString.length() > 0) {
174 try {
175 Date date = dateFormat.parse(dateAsString);
176 if (date.after(now)) {
177 date = now;
178 }
179 blogEntry.setDate(date);
180 } catch (ParseException pe) {
181 log.warn(pe);
182 blogEntry.setDate(now);
183 }
184 } else {
185 // the date has been blanked out, so reset to "now"
186 blogEntry.setDate(now);
187 }
188 }
189
190 blogEntry.setTimeZoneId(timeZone);
191 blogEntry.setTitle(title);
192 blogEntry.setSubtitle(subtitle);
193 blogEntry.setBody(body);
194 blogEntry.setExcerpt(excerpt);
195 Set categories = new HashSet();
196 if (category != null) {
197 for (int i = 0; i < category.length; i++) {
198 categories.add(blog.getCategory(category[i]));
199 }
200 }
201 blogEntry.setCategories(categories);
202 blogEntry.setTags(tags);
203 blogEntry.setOriginalPermalink(originalPermalink);
204 if (commentsEnabled != null && commentsEnabled.equalsIgnoreCase("true")) {
205 blogEntry.setCommentsEnabled(true);
206 } else {
207 blogEntry.setCommentsEnabled(false);
208 }
209 if (trackBacksEnabled != null && trackBacksEnabled.equalsIgnoreCase("true")) {
210 blogEntry.setTrackBacksEnabled(true);
211 } else {
212 blogEntry.setTrackBacksEnabled(false);
213 }
214
215 String attachmentUrl = request.getParameter("attachmentUrl");
216 String attachmentSize = request.getParameter("attachmentSize");
217 String attachmentType = request.getParameter("attachmentType");
218 if (attachmentUrl != null && attachmentUrl.length() > 0) {
219 Attachment attachment = populateAttachment(blogEntry, attachmentUrl, attachmentSize, attachmentType);
220 blogEntry.setAttachment(attachment);
221 } else {
222 blogEntry.setAttachment(null);
223 }
224 }
225
226 private Attachment populateAttachment(BlogEntry blogEntry, String attachmentUrl, String attachmentSize, String attachmentType) {
227 if (attachmentSize == null || attachmentSize.length() == 0) {
228 String absoluteAttachmentUrl = attachmentUrl;
229 try {
/*
P/P * Method: Attachment populateAttachment(BlogEntry, String, String, String)
*
* Preconditions:
* (soft) attachmentUrl != null
* (soft) blogEntry != null
* (soft) log != null
*
* Presumptions:
* net.sourceforge.pebble.domain.BlogEntry:getBlog(...)@232 != null
*
* Postconditions:
* return_value == &new Attachment(populateAttachment#5)
* new Attachment(populateAttachment#5) num objects == 1
*
* Test Vectors:
* attachmentSize: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@227: {1..232-1}, {0}
* java.lang.String:length(...)@254: {0}, {1..232-1}
* java.lang.String:startsWith(...)@231: {0}, {1}
* org.apache.commons.httpclient.HttpClient:executeMethod(...)@236: {-231..199, 201..232-1}, {200}
* org.apache.commons.httpclient.methods.HeadMethod:getResponseHeader(...)@238: Addr_Set{null}, Inverse{null}
* org.apache.commons.httpclient.methods.HeadMethod:getResponseHeader(...)@242: Addr_Set{null}, Inverse{null}
*/
230 HttpClient httpClient = new HttpClient();
231 if (absoluteAttachmentUrl.startsWith("./")) {
232 absoluteAttachmentUrl = blogEntry.getBlog().getUrl() + absoluteAttachmentUrl.substring(2);
233 }
234
235 HeadMethod headMethod = new HeadMethod(absoluteAttachmentUrl);
236 int status = httpClient.executeMethod(headMethod);
237 if (status == 200) {
238 Header attachmentSizeHeader = headMethod.getResponseHeader("Content-Length");
239 if (attachmentSizeHeader != null) {
240 attachmentSize = attachmentSizeHeader.getValue();
241 }
242 Header attachmentTypeHeader = headMethod.getResponseHeader("Content-Type");
243 if (attachmentTypeHeader != null) {
244 attachmentType = attachmentTypeHeader.getValue();
245 }
246 }
247 } catch (IOException e) {
248 log.warn("Could not get details for attachment located at " + absoluteAttachmentUrl + " : " + e.getMessage());
249 }
250 }
251
252 Attachment attachment = new Attachment();
253 attachment.setUrl(attachmentUrl);
254 if (attachmentSize != null && attachmentSize.length() > 0) {
255 attachment.setSize(Long.parseLong(attachmentSize));
256 }
257 attachment.setType(attachmentType);
258
259 return attachment;
260 }
261
262 /**
263 * Gets a list of all roles that are allowed to access this action.
264 *
265 * @return an array of Strings representing role names
266 * @param request
267 */
268 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])
*/
269 return new String[]{Constants.BLOG_CONTRIBUTOR_ROLE};
270 }
271
272 }
SofCheck Inspector Build Version : 2.22510
| saveblogentryaction.java |
2010-Jun-25 19:40:34 |
| saveblogentryaction.class |
2010-Jul-19 20:23:38 |