File Source: utilities.java
/*
P/P * Method: net.sourceforge.pebble.util.Utilities$1__static_init
*/
1 /*
2 * Copyright (c) 2003-2005, 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.util;
33
34 import net.sourceforge.pebble.Configuration;
35 import net.sourceforge.pebble.PebbleContext;
36 import net.sourceforge.pebble.dao.CategoryDAO;
37 import net.sourceforge.pebble.dao.DAOFactory;
38 import net.sourceforge.pebble.dao.file.*;
39 import net.sourceforge.pebble.domain.*;
40 import net.sourceforge.pebble.api.event.comment.CommentEvent;
41 import net.sourceforge.pebble.api.event.trackback.TrackBackEvent;
42 import net.sourceforge.pebble.event.response.IpAddressListener;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import java.io.File;
47 import java.io.FileInputStream;
48 import java.io.FilenameFilter;
49 import java.util.*;
50 import java.text.SimpleDateFormat;
51
52 /**
53 * Utilities for the current blog, such as those useful for moving
54 * between versions of Pebble.
55 *
56 * @author Simon Brown
57 */
/*
P/P * Method: void net.sourceforge.pebble.util.Utilities()
*/
58 public class Utilities {
59
60 /** the logger used by this action */
/*
P/P * Method: net.sourceforge.pebble.util.Utilities__static_init
*
* Postconditions:
* init'ed(log)
*/
61 private static final Log log = LogFactory.getLog(Utilities.class);
62
63 /**
64 * Builds the indexes for the given blog.
65 *
66 * @param blog a Blog instance
67 */
68 public static void buildIndexes(Blog blog) {
/*
P/P * Method: void buildIndexes(Blog)
*
* Preconditions:
* blog != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*/
69 log.info("Reindexing blog");
70 blog.reindex();
71 }
72
73 /**
74 * Builds the blacklist and whitelist of IP addresses from all responses
75 * for the given blog.
76 *
77 * @param blog a Blog instance
78 */
79 public static void buildIpAddressLists(Blog blog) {
/*
P/P * Method: void buildIpAddressLists(Blog)
*
* Preconditions:
* blog != null
*
* Presumptions:
* java.util.Iterator:next(...)@100 != null
* java.util.Iterator:next(...)@84 != null
* java.util.Iterator:next(...)@88 != null
* net.sourceforge.pebble.domain.Blog:getBlogEntries(...)@80 != null
* net.sourceforge.pebble.domain.BlogEntry:getComments(...)@86 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@83: {1}, {0}
* java.util.Iterator:hasNext(...)@87: {1}, {0}
* java.util.Iterator:hasNext(...)@99: {1}, {0}
* net.sourceforge.pebble.domain.Comment:isApproved(...)@89: {0}, {1}
* net.sourceforge.pebble.domain.Comment:isRejected(...)@92: {0}, {1}
* net.sourceforge.pebble.domain.TrackBack:isApproved(...)@101: {0}, {1}
* net.sourceforge.pebble.domain.TrackBack:isRejected(...)@104: {0}, {1}
*/
80 Iterator blogEntries = blog.getBlogEntries().iterator();
81 IpAddressListener ipAddressListener = new IpAddressListener();
82
83 while (blogEntries.hasNext()) {
84 BlogEntry blogEntry = (BlogEntry)blogEntries.next();
85 log.info("Processing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
86 Iterator comments = blogEntry.getComments().iterator();
87 while (comments.hasNext()) {
88 Comment comment = (Comment)comments.next();
89 if (comment.isApproved()) {
90 CommentEvent event = new CommentEvent(comment, CommentEvent.COMMENT_APPROVED);
91 ipAddressListener.commentApproved(event);
92 } else if (comment.isRejected()) {
93 CommentEvent event = new CommentEvent(comment, CommentEvent.COMMENT_REJECTED);
94 ipAddressListener.commentRejected(event);
95 }
96 }
97
98 Iterator trackbacks = blogEntry.getTrackBacks().iterator();
99 while (trackbacks.hasNext()) {
100 TrackBack trackback = (TrackBack)trackbacks.next();
101 if (trackback.isApproved()) {
102 TrackBackEvent event = new TrackBackEvent(trackback, TrackBackEvent.TRACKBACK_APPROVED);
103 ipAddressListener.trackBackApproved(event);
104 } else if (trackback.isRejected()) {
105 TrackBackEvent event = new TrackBackEvent(trackback, TrackBackEvent.TRACKBACK_REJECTED);
106 ipAddressListener.trackBackRejected(event);
107 }
108 }
109 //
110 // try {
111 // blogEntry.store();
112 // } catch (BlogServiceException e) {
113 // log.error("Error storing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
114 // }
115 }
116 }
117
118 /**
119 * Fixes HTML escaping of comment and TrackBack content for the given blog.
120 *
121 * @param blog a Blog instance
122 */
123 public static void fixHtmlInResponses(Blog blog) {
/*
P/P * Method: void fixHtmlInResponses(Blog)
*
* Preconditions:
* blog != null
*
* Presumptions:
* java.util.Iterator:next(...)@126 != null
* java.util.Iterator:next(...)@130 != null
* java.util.Iterator:next(...)@139 != null
* net.sourceforge.pebble.domain.Blog:getBlogEntries(...)@124 != null
* net.sourceforge.pebble.domain.BlogEntry:getComments(...)@128 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@125: {1}, {0}
* java.util.Iterator:hasNext(...)@129: {1}, {0}
* java.util.Iterator:hasNext(...)@138: {1}, {0}
* net.sourceforge.pebble.domain.Comment:getBody(...)@131: Addr_Set{null}, Inverse{null}
* net.sourceforge.pebble.domain.TrackBack:getExcerpt(...)@140: Addr_Set{null}, Inverse{null}
*/
124 Iterator blogEntries = blog.getBlogEntries().iterator();
125 while (blogEntries.hasNext()) {
126 BlogEntry blogEntry = (BlogEntry)blogEntries.next();
127 log.info("Processing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
128 Iterator comments = blogEntry.getComments().iterator();
129 while (comments.hasNext()) {
130 Comment comment = (Comment)comments.next();
131 if (comment.getBody() != null) {
132 comment.setBody(comment.getBody().replaceAll("&", "&"));
133 comment.setBody(comment.getBody().replaceAll("<", "<"));
134 comment.setBody(comment.getBody().replaceAll(">", ">"));
135 }
136 }
137 Iterator trackbacks = blogEntry.getTrackBacks().iterator();
138 while (trackbacks.hasNext()) {
139 TrackBack trackback = (TrackBack)trackbacks.next();
140 if (trackback.getExcerpt() != null) {
141 trackback.setExcerpt(trackback.getExcerpt().replaceAll("&", "&"));
142 trackback.setExcerpt(trackback.getExcerpt().replaceAll("<", "<"));
143 trackback.setExcerpt(trackback.getExcerpt().replaceAll(">", ">"));
144 }
145 }
146 try {
147 BlogService service = new BlogService();
148 service.putBlogEntry(blogEntry);
149 } catch (BlogServiceException e) {
150 log.error("Error storing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
151 }
152 }
153 }
154
155 /**
156 * Converts flat categories to hierarchical categories.
157 *
158 * @param blog a Blog instance
159 */
160 public static void convertCategories(Blog blog) {
/*
P/P * Method: void convertCategories(Blog)
*
* Preconditions:
* (soft) blog != null
*
* Presumptions:
* java.util.Iterator:next(...)@169 != null
* java.util.Properties:keySet(...)@167 != null
* net.sourceforge.pebble.dao.DAOFactory:getCategoryDAO(...)@181 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@180 != null
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*
* Test Vectors:
* java.lang.String:startsWith(...)@173: {1}, {0}
*/
161 Properties categories = new Properties();
162 try {
163 FileInputStream in = new FileInputStream(new File(blog.getRoot(), "blog.categories"));
164 categories.load(in);
165 in.close();
166
167 Iterator it = categories.keySet().iterator();
168 while (it.hasNext()) {
169 String id = (String)it.next();
170 String name = categories.getProperty(id);
171 Category category;
172
173 if (!id.startsWith("/")) {
174 category = new Category("/" + id, name);
175 } else {
176 category = new Category(id, name);
177 }
178
179 blog.addCategory(category);
180 DAOFactory factory = DAOFactory.getConfiguredFactory();
181 CategoryDAO dao = factory.getCategoryDAO();
182 dao.addCategory(category, blog);
183 }
184 } catch (Exception e) {
185 log.error("Exception encountered", e);
186 }
187 }
188
189 /**
190 * Moves blog entries from one category to another.
191 *
192 * @param blog a Blog instance
193 */
194 public static void moveBlogEntriesFromCategory(Blog blog, Category from, Category to) {
/*
P/P * Method: void moveBlogEntriesFromCategory(Blog, Category, Category)
*
* Preconditions:
* blog != null
*
* Presumptions:
* java.util.Iterator:next(...)@197 != null
* net.sourceforge.pebble.domain.Blog:getBlogEntries(...)@195 != null
* net.sourceforge.pebble.domain.BlogEntry:getCategories(...)@200 != null
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*
* Test Vectors:
* java.util.Collection:contains(...)@201: {0}, {1}
* java.util.Iterator:hasNext(...)@196: {1}, {0}
*/
195 Iterator blogEntries = blog.getBlogEntries().iterator();
196 while (blogEntries.hasNext()) {
197 BlogEntry blogEntry = (BlogEntry)blogEntries.next();
198 log.info("Processing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
199
200 Collection categories = blogEntry.getCategories();
201 if (categories.contains(from)) {
202 categories.remove(from);
203 categories.add(to);
204 blogEntry.setCategories(categories);
205
206 try {
207 BlogService service = new BlogService();
208 service.putBlogEntry(blogEntry);
209 } catch (BlogServiceException e) {
210 log.info("Error storing " + blogEntry.getTitle() + " (" + blogEntry.getDate() + ")");
211 }
212 }
213 }
214 }
215
216 /**
217 * Resets the theme of a blog to "default".
218 */
219 public static void resetTheme(Blog blog) {
/*
P/P * Method: void resetTheme(Blog)
*
* Preconditions:
* (soft) blog != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*/
220 log.info("Resetting theme to default");
221 try {
222 blog.removeProperty(Blog.THEME_KEY);
223 blog.storeProperties();
224 } catch (BlogServiceException e) {
225 e.printStackTrace();
226 }
227 }
228
229 /**
230 * Blasts the blog specific theme and overwrites it with the default theme.
231 */
232 public static void restoreTheme(Blog blog, String themeName) {
/*
P/P * Method: void restoreTheme(Blog, String)
*
* Preconditions:
* blog != null
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getEditableTheme(...)@234 != null
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*/
233 log.info("Restoring theme to " + themeName);
234 blog.getEditableTheme().restoreToSpecifiedTheme(themeName);
235 }
236
237 /**
238 * Resets the plugins back to their defaults.
239 */
240 public static void resetPlugins(Blog blog) {
/*
P/P * Method: void resetPlugins(Blog)
*
* Preconditions:
* (soft) blog != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@61 != null
*/
241 log.info("Resetting plugins to the default configuration");
242 try {
243 blog.removeProperty(Blog.PERMALINK_PROVIDER_KEY);
244 blog.removeProperty(Blog.CONTENT_DECORATORS_KEY);
245 blog.removeProperty(Blog.BLOG_LISTENERS_KEY);
246 blog.removeProperty(Blog.BLOG_ENTRY_LISTENERS_KEY);
247 blog.removeProperty(Blog.COMMENT_LISTENERS_KEY);
248 blog.removeProperty(Blog.COMMENT_CONFIRMATION_STRATEGY_KEY);
249 blog.removeProperty(Blog.TRACKBACK_LISTENERS_KEY);
250 blog.removeProperty(Blog.TRACKBACK_CONFIRMATION_STRATEGY_KEY);
251 blog.removeProperty(Blog.LUCENE_ANALYZER_KEY);
252 blog.removeProperty(Blog.LOGGER_KEY);
253 blog.storeProperties();
254 } catch (BlogServiceException e) {
255 e.printStackTrace();
256 }
257 }
258
259 /**
260 * Moves blog entries from one category to another.
261 *
262 * @param blog a Blog instance
263 */
264 public static void restructureBlogToGMT(Blog blog) {
/*
P/P * Method: void restructureBlogToGMT(Blog)
*
* Preconditions:
* blog != null
*
* Presumptions:
* Local_13[Local_11]@271 != null
* Local_18[Local_16]@273 != null
* Local_23[Local_21]@275 != null
* Local_8[Local_6]@269 != null
* blogEntryFiles.length@275 <= 232-1
* ...
*
* Test Vectors:
* java.io.File:equals(...)@283: {1}, {0}
*/
265 log.info("Restructuring blog entries into GMT directory hierarchy");
266 TimeZone gmt = TimeZone.getTimeZone("GMT");
267 FileBlogEntryDAO dao = new FileBlogEntryDAO();
268 File root = new File(blog.getRoot());
269 File years[] = root.listFiles(new FourDigitFilenameFilter());
270 for (File year : years) {
271 File months[] = year.listFiles(new TwoDigitFilenameFilter());
272 for (File month : months) {
273 File days[] = month.listFiles(new TwoDigitFilenameFilter());
274 for (File day : days) {
275 File blogEntryFiles[] = day.listFiles(new BlogEntryFilenameFilter());
276 for (File blogEntryFile : blogEntryFiles) {
277 String filename = blogEntryFile.getName();
278 String id = filename.substring(0, filename.indexOf('.'));
279 File oldFile = blogEntryFile;
280 File newDirectory = new File(dao.getPath(blog, id, gmt));
281 File newFile = new File(newDirectory, filename);
282
283 if (!oldFile.equals(newFile)) {
284 log.info("Moving " + id + " to " + newFile.getAbsolutePath() + " from " + oldFile.getAbsolutePath());
285 newDirectory.mkdirs();
286 oldFile.renameTo(newFile);
287 }
288 }
289 }
290 }
291 }
292 }
293
294 /**
295 * Restructures how static pages are stored on disk.
296 *
297 * @param blog a Blog instance
298 */
299 public static void restructureStaticPages(Blog blog) {
/*
P/P * Method: void restructureStaticPages(Blog)
*
* Preconditions:
* blog != null
*
* Presumptions:
* Local_6[Local_4]@307 != null
* files.length@307 <= 232-1
* java.io.File:getName(...)@314 != null
* java.io.File:getName(...)@315 != null
* java.io.File:getName(...)@324 != null
* ...
*
* Test Vectors:
* java.io.File:exists(...)@316: {1}, {0}
* java.io.File:exists(...)@325: {1}, {0}
* java.io.File:isDirectory(...)@303: {1}, {0}
* java.lang.String:endsWith(...)@314: {0}, {1}
*/
300 log.info("Restructuring static pages");
301 File root = new File(blog.getRoot(), "pages");
302 // Upon first start this directory does not exist yet
303 if(!root.isDirectory()) {
304 root.mkdir();
305 }
306
/*
P/P * Method: void net.sourceforge.pebble.util.Utilities$1()
*/
307 File files[] = root.listFiles(new FilenameFilter() {
308 public boolean accept(File dir, String name) {
/*
P/P * Method: bool accept(File, String)
*
* Preconditions:
* name != null
*
* Postconditions:
* init'ed(return_value)
*/
309 return name.matches("(\\d+\\.xml)|(\\d+\\.xml\\.bak)");
310 }
311 });
312
313 for (File file : files) {
314 if (file.getName().endsWith(".xml")) {
315 File staticPageDirectory = new File(root, file.getName().substring(0, file.getName().indexOf(".xml")));
316 if (!staticPageDirectory.exists()) {
317 log.info("Creating static page directory at " + staticPageDirectory.getAbsolutePath());
318 staticPageDirectory.mkdir();
319 }
320 File destination = new File(staticPageDirectory, file.getName());
321 log.info("Moving " + file.getAbsolutePath() + " to " + destination.getAbsolutePath());
322 file.renameTo(destination);
323 } else {
324 File staticPageDirectory = new File(root, file.getName().substring(0, file.getName().indexOf(".xml")));
325 if (!staticPageDirectory.exists()) {
326 log.info("Creating static page directory at " + staticPageDirectory.getAbsolutePath());
327 staticPageDirectory.mkdir();
328 }
329 SimpleDateFormat archiveFileExtension = new SimpleDateFormat("yyyyMMdd-HHmmss");
330 archiveFileExtension.setTimeZone(blog.getTimeZone());
331 File destination = new File(staticPageDirectory, file.getName().substring(0, file.getName().length()-3) + archiveFileExtension.format(new Date(file.lastModified())));
332 log.info("Moving " + file.getAbsolutePath() + " to " + destination.getAbsolutePath());
333 file.renameTo(destination);
334 }
335 }
336 }
337
338 public static void main(String[] args) throws Exception {
/*
P/P * Method: void main(String[])
*
* Preconditions:
* args != null
* (soft) init'ed(args[0])
* (soft) init'ed(args[1])
*
* Presumptions:
* java.lang.System.out != null
*
* Test Vectors:
* args.length: {2}, {0,1, 3..+Inf}
* args[1]: Addr_Set{null}, Inverse{null}
* java.lang.String:equalsIgnoreCase(...)@353: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@355: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@357: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@359: {0}, {1}
*/
339 if (args.length != 2) {
340 System.out.println("Usage : pebble.util.Utilities %1 %2");
341 System.out.println(" %1 : location of Pebble blog");
342 System.out.println(" %2 : [ipAddressListener|fixHtmlInResponses|convertCategories]");
343
344 return;
345 }
346
347 DAOFactory.setConfiguredFactory(new FileDAOFactory());
348 Blog blog = new Blog(args[0]);
349
350 String action = args[1];
351 if (action == null) {
352 // do nothing
353 } else if (action.equalsIgnoreCase("ipAddressListener")) {
354 buildIpAddressLists(blog);
355 } else if (action.equalsIgnoreCase("fixHtmlInResponses")) {
356 fixHtmlInResponses(blog);
357 } else if (action.equalsIgnoreCase("buildIndexes")) {
358 buildIndexes(blog);
359 } else if (action.equalsIgnoreCase("convertCategories")) {
360 convertCategories(blog);
361 }
362
363 }
364 /**
365 * this is a very simple way to honor https transfer. As
366 * the frontend depends upon setting a base url in the html/head block, it
367 * should at least contain the https scheme if the current page has been
368 * requested through https.
369 * If there is no https involved, pebbles previous behaviour
370 * (as of version 2.3.1) does not change. This change implies also changing
371 * several frontend jsps WEB-INF/tags/page.tag to use the value calculated
372 * here instead of ${blog.url}. In the context of the patch that introduced
373 * this method, the values have been named ${blogUrl} and ${multiBlogUrl}
374
375 * @param currentScheme the scheme the current request has been sent through (e.g. request.getScheme())
376 * @param blogUrl the ordinary blog url that might be changed to the secure one.
377 * @return value to be used as base url for the blog named in blogUrl
378 */
379 public static String calcBaseUrl(String currentScheme, String blogUrl) {
/*
P/P * Method: String calcBaseUrl(String, String)
*
* Preconditions:
* (soft) blogUrl != null
*
* Presumptions:
* net.sourceforge.pebble.Configuration:getSecureUrl(...)@381 != null
*
* Preconditions:
* (soft) net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.lang.String:equals(...)@381: {0}, {1}
* java.lang.String:startsWith(...)@381: {0}, {1}
*/
380 Configuration configuration = PebbleContext.getInstance().getConfiguration();
381 if ("https".equals(currentScheme) && configuration.getSecureUrl().startsWith("https")) {
382 return blogUrl.replace(configuration.getUrl(), configuration.getSecureUrl());
383 }
384 return blogUrl;
385 }
386
387 }
SofCheck Inspector Build Version : 2.22510
| utilities.java |
2010-Jun-25 19:40:32 |
| utilities.class |
2010-Jul-19 20:23:38 |
| utilities$1.class |
2010-Jul-19 20:23:38 |