1 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 ; 40 import java.io.*; 41 import java.util.HashMap ; 42 import java.util.Map ; 43 44 51 public class MoblogPluginUtils { 52 53 private static Log _logger = LogFactory.getLog(MoblogPluginUtils.class); 54 55 62 public static Mailbox readMailboxSettingsForBlog(ServletConfig servletConfig, Blog blog) { 63 Mailbox mailbox = new Mailbox(); 64 mailbox.setEnabled(false); 65 66 mailbox.setId(blog.getId()); 67 68 String blogID = blog.getBlogId(); 69 mailbox.setBlogId(blogID); 70 mailbox.setBlogBaseURL(blog.getBlogBaseURL()); 71 72 String 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 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 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 resourceUrl = servletConfig.getServletContext().getRealPath(mailbox.getUrlPrefix()); 95 mailbox.setOutputDirectory(resourceUrl); 96 97 String blogCategoryID = blog.getProperty(MoblogPlugin.PROPERTY_CATEGORY); 98 mailbox.setCategoryId(blogCategoryID); 99 100 Boolean enabled = Boolean.valueOf(blog.getProperty(MoblogPlugin.PROPERTY_ENABLED)); 101 mailbox.setEnabled(enabled.booleanValue()); 102 103 String [] types; 104 105 String 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 imageTypesMap = new HashMap (); 114 for (int i = 0; i < types.length; i++) { 115 String type = types[i]; 116 imageTypesMap.put(type, type); 117 } 118 mailbox.setImageMimeTypes(imageTypesMap); 119 } 120 } 121 122 String 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 attachmentTypesMap = new HashMap (); 128 for (int i = 0; i < types.length; i++) { 129 String type = types[i]; 130 attachmentTypesMap.put(type, type); 131 } 132 mailbox.setAttachmentMimeTypes(attachmentTypesMap); 133 } 134 } else { 135 mailbox.setAttachmentMimeTypes(new HashMap ()); 136 } 137 138 String 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 textTypesMap = new HashMap (); 148 for (int i = 0; i < types.length; i++) { 149 String type = types[i]; 150 textTypesMap.put(type, type); 151 } 152 mailbox.setTextMimeTypes(textTypesMap); 153 } 154 } 155 156 String 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 String authorizedAddresses = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES); 166 if (!BlojsomUtils.checkNullOrBlank(authorizedAddresses)) { 167 String [] addresses = BlojsomUtils.parseCommaList(authorizedAddresses); 168 mailbox.setAuthorizedAddresses(BlojsomUtils.arrayOfStringsToMap(addresses)); 169 } else { 170 mailbox.setAuthorizedAddresses(new HashMap ()); 171 } 172 173 String 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 193 public static int saveFile(String filename, String extension, InputStream input) throws IOException { 194 int count = 0; 195 if (filename == null) { 196 return count; 197 } 198 199 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 |