KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > moblog > MoblogPluginUtils


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
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 notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.moblog;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.util.BlojsomConstants;
37 import org.blojsom.util.BlojsomUtils;
38
39 import javax.servlet.ServletConfig JavaDoc;
40 import java.io.*;
41 import java.util.HashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * Moblog Plugin Utils
46  *
47  * @author David Czarnecki
48  * @version $Id: MoblogPluginUtils.java,v 1.4 2006/09/26 02:55:20 czarneckid Exp $
49  * @since blojsom 3.0
50  */

51 public class MoblogPluginUtils {
52
53     private static Log _logger = LogFactory.getLog(MoblogPluginUtils.class);
54
55     /**
56      * Read in the mailbox settings for a given blog
57      *
58      * @param blog {@link Blog}
59      * @return {@link Mailbox} populated with settings and authorized e-mail addresses or <code>null</code> if there
60      * was an error reading any configuration information
61      */

62     public static Mailbox readMailboxSettingsForBlog(ServletConfig JavaDoc servletConfig, Blog blog) {
63         Mailbox mailbox = new Mailbox();
64         mailbox.setEnabled(false);
65
66         mailbox.setId(blog.getId());
67         
68         String JavaDoc blogID = blog.getBlogId();
69         mailbox.setBlogId(blogID);
70         mailbox.setBlogBaseURL(blog.getBlogBaseURL());
71
72         String JavaDoc hostname = blog.getProperty(MoblogPlugin.PROPERTY_HOSTNAME);
73         if (!BlojsomUtils.checkNullOrBlank(hostname)) {
74             mailbox.setHostName(hostname);
75         } else {
76             mailbox.setEnabled(false);
77         }
78
79         String JavaDoc userid = blog.getProperty(MoblogPlugin.PROPERTY_USERID);
80         if (!BlojsomUtils.checkNullOrBlank(userid)) {
81             mailbox.setUserId(userid);
82         } else {
83             mailbox.setEnabled(false);
84         }
85
86         String JavaDoc password = blog.getProperty(MoblogPlugin.PROPERTY_PASSWORD);
87         if (!BlojsomUtils.checkNullOrBlank(password)) {
88             mailbox.setPassword(password);
89         } else {
90             mailbox.setEnabled(false);
91         }
92
93         mailbox.setUrlPrefix(BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY + blogID + "/");
94         String JavaDoc resourceUrl = servletConfig.getServletContext().getRealPath(mailbox.getUrlPrefix());
95         mailbox.setOutputDirectory(resourceUrl);
96
97         String JavaDoc blogCategoryID = blog.getProperty(MoblogPlugin.PROPERTY_CATEGORY);
98         mailbox.setCategoryId(blogCategoryID);
99
100         Boolean JavaDoc enabled = Boolean.valueOf(blog.getProperty(MoblogPlugin.PROPERTY_ENABLED));
101         mailbox.setEnabled(enabled.booleanValue());
102
103         String JavaDoc[] types;
104
105         // Extract the image mime types
106
String JavaDoc imageMimeTypes = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_IMAGE_MIME_TYPES);
107         if (BlojsomUtils.checkNullOrBlank(imageMimeTypes)) {
108             imageMimeTypes = MoblogPlugin.DEFAULT_IMAGE_MIME_TYPES;
109         }
110         if (!BlojsomUtils.checkNullOrBlank(imageMimeTypes)) {
111             types = BlojsomUtils.parseCommaList(imageMimeTypes);
112             if (types.length > 0) {
113                 Map JavaDoc imageTypesMap = new HashMap JavaDoc();
114                 for (int i = 0; i < types.length; i++) {
115                     String JavaDoc type = types[i];
116                     imageTypesMap.put(type, type);
117                 }
118                 mailbox.setImageMimeTypes(imageTypesMap);
119             }
120         }
121
122         // Extract the attachment mime types
123
String JavaDoc attachmentMimeTypes = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_ATTACHMENT_MIME_TYPES);
124         if (!BlojsomUtils.checkNullOrBlank(attachmentMimeTypes)) {
125             types = BlojsomUtils.parseCommaList(attachmentMimeTypes);
126             if (types.length > 0) {
127                 Map JavaDoc attachmentTypesMap = new HashMap JavaDoc();
128                 for (int i = 0; i < types.length; i++) {
129                     String JavaDoc type = types[i];
130                     attachmentTypesMap.put(type, type);
131                 }
132                 mailbox.setAttachmentMimeTypes(attachmentTypesMap);
133             }
134         } else {
135             mailbox.setAttachmentMimeTypes(new HashMap JavaDoc());
136         }
137
138         // Extract the text mime types
139
String JavaDoc textMimeTypes = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_TEXT_MIME_TYPES);
140         if (BlojsomUtils.checkNullOrBlank(textMimeTypes)) {
141             textMimeTypes = MoblogPlugin.DEFAULT_TEXT_MIME_TYPES;
142         }
143
144         if (!BlojsomUtils.checkNullOrBlank(textMimeTypes)) {
145             types = BlojsomUtils.parseCommaList(textMimeTypes);
146             if (types.length > 0) {
147                 Map JavaDoc textTypesMap = new HashMap JavaDoc();
148                 for (int i = 0; i < types.length; i++) {
149                     String JavaDoc type = types[i];
150                     textTypesMap.put(type, type);
151                 }
152                 mailbox.setTextMimeTypes(textTypesMap);
153             }
154         }
155
156         // Extract the secret word
157
String JavaDoc secretWord = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_SECRET_WORD);
158         if (BlojsomUtils.checkNullOrBlank(secretWord)) {
159             mailbox.setSecretWord(null);
160         } else {
161             mailbox.setSecretWord(secretWord);
162         }
163
164         // Configure authorized email addresses for moblog posting
165
String JavaDoc authorizedAddresses = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES);
166         if (!BlojsomUtils.checkNullOrBlank(authorizedAddresses)) {
167             String JavaDoc[] addresses = BlojsomUtils.parseCommaList(authorizedAddresses);
168             mailbox.setAuthorizedAddresses(BlojsomUtils.arrayOfStringsToMap(addresses));
169         } else {
170             mailbox.setAuthorizedAddresses(new HashMap JavaDoc());
171         }
172
173         // Configure ignore regular expression
174
String JavaDoc ignoreExpression = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_IGNORE_EXPRESSION);
175         if (BlojsomUtils.checkNullOrBlank(ignoreExpression)) {
176             mailbox.setIgnoreExpression(null);
177         } else {
178             mailbox.setIgnoreExpression(ignoreExpression);
179         }
180
181         return mailbox;
182     }
183
184     /**
185      * Save a file to disk
186      *
187      * @param filename Base filename
188      * @param extension File extension
189      * @param input Input from which to read and write a file
190      * @return # of bytes written to disk
191      * @throws IOException If there is an error writing the file
192      */

193     public static int saveFile(String JavaDoc filename, String JavaDoc extension, InputStream input) throws IOException {
194         int count = 0;
195         if (filename == null) {
196             return count;
197         }
198
199         // Do not overwrite existing file
200
File file = new File(filename + extension);
201         for (int i = 0; file.exists(); i++) {
202             file = new File(filename + i + extension);
203         }
204         FileOutputStream fos = new FileOutputStream(file);
205         BufferedOutputStream bos = new BufferedOutputStream(fos);
206
207         BufferedInputStream bis = new BufferedInputStream(input);
208         int aByte;
209         while ((aByte = bis.read()) != -1) {
210             bos.write(aByte);
211             count++;
212         }
213
214         bos.flush();
215         bos.close();
216         bis.close();
217
218         return count;
219     }
220 }
Popular Tags