1 18 19 package org.apache.roller.ui.authoring.struts.actions; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.InputStream ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.struts.action.Action; 29 import org.apache.struts.action.ActionError; 30 import org.apache.struts.action.ActionErrors; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.upload.FormFile; 35 import org.apache.roller.model.BookmarkManager; 36 import org.apache.roller.model.RollerFactory; 37 import org.apache.roller.ui.core.RollerRequest; 38 import org.apache.roller.ui.authoring.struts.formbeans.FolderFormEx; 39 40 import java.text.SimpleDateFormat ; 41 import java.util.Date ; 42 import org.apache.struts.action.ActionMessage; 43 import org.apache.struts.action.ActionMessages; 44 import org.apache.roller.RollerException; 45 import org.apache.roller.model.Roller; 46 import org.apache.roller.pojos.FolderData; 47 import org.apache.roller.pojos.WebsiteData; 48 import org.apache.roller.ui.core.BasePageModel; 49 import org.apache.roller.ui.core.RequestConstants; 50 import org.apache.roller.ui.core.RollerSession; 51 import org.apache.roller.util.cache.CacheManager; 52 53 62 public final class ImportBookmarksFormAction extends Action { 63 64 private static Log mLogger = 65 LogFactory.getFactory().getInstance(RollerRequest.class); 66 67 70 public ActionForward execute( 71 ActionMapping mapping, 72 ActionForm actionForm, 73 HttpServletRequest request, 74 HttpServletResponse response) 75 throws Exception { 76 77 ActionErrors errors = new ActionErrors(); 78 FolderFormEx theForm = (FolderFormEx)actionForm; 79 ActionForward fwd = mapping.findForward("importBookmarks.page"); 80 81 RollerRequest rreq = RollerRequest.getRollerRequest(request); 82 RollerSession rses = RollerSession.getRollerSession(request); 83 BookmarkManager bm = RollerFactory.getRoller().getBookmarkManager(); 84 85 BasePageModel pageModel = new BasePageModel( 86 "bookmarksImport.title", request, response, mapping); 87 request.setAttribute("model", pageModel); 88 89 WebsiteData website = getWebsite(request); 90 pageModel.setWebsite(website); 91 92 if (rses.isUserAuthorizedToAuthor(website) && theForm.getBookmarksFile() != null) { 94 95 String encoding = request.getCharacterEncoding(); 98 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { 99 response.setContentType("text/html; charset=utf-8"); 100 } 101 boolean writeFile = false; 102 103 FormFile file = theForm.getBookmarksFile(); 105 String data = null; 106 InputStream stream = null; 107 try { 108 109 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 111 stream = file.getInputStream(); 112 if (!writeFile) { 113 if (file.getFileSize() < (4*1024000)) { 115 116 byte[] buffer = new byte[8192]; 117 int bytesRead = 0; 118 while ((bytesRead=stream.read(buffer,0,8192)) != -1) { 119 baos.write(buffer, 0, bytesRead); 120 } 121 data = new String (baos.toByteArray()); 122 123 SimpleDateFormat formatter = 124 new SimpleDateFormat ("yyyyMMddHHmmss"); 125 Date now = new Date (); 126 String folderName = "imported-" + formatter.format(now); 127 128 130 bm.importBookmarks(website, folderName, data); 131 RollerFactory.getRoller().flush(); 132 CacheManager.invalidate(website); 133 134 ActionMessages messages = new ActionMessages(); 135 messages.add(ActionMessages.GLOBAL_MESSAGE, 136 new ActionMessage("bookmarksImport.imported", folderName)); 137 saveMessages(request, messages); 138 } 139 else { 140 data = "The file is greater than 4MB, " 141 +" and has not been written to stream." 142 +" File Size: "+file.getFileSize()+" bytes. " 143 +" This is a limitation of this particular " 144 +" web application, hard-coded in " 145 +" org.apache.struts.webapp.upload.UploadAction"; 146 errors.add(ActionErrors.GLOBAL_ERROR, 147 new ActionError("bookmarksImport.error",data)); 148 } 149 } 150 151 } 152 catch (Exception e) { 153 errors.add(ActionErrors.GLOBAL_ERROR, 154 new ActionError("bookmarksImport.error",e.toString())); 155 saveErrors(request,errors); 156 mLogger.error("ERROR: importing bookmarks",e); 157 } 158 finally { 159 if ( stream!=null ) { 160 try { stream.close(); } 161 catch (Exception e) { mLogger.error("Closing stream",e); }; 162 } 163 } 164 file.destroy(); 166 } 167 else if (!rses.isUserAuthorizedToAuthor(website)) { 168 fwd = mapping.findForward("access-denied"); 169 } 170 return fwd; 171 } 172 173 178 public static WebsiteData getWebsite(HttpServletRequest request) throws RollerException { 179 RollerRequest rreq = RollerRequest.getRollerRequest(request); 180 WebsiteData website = rreq.getWebsite(); 181 String folderid = request.getParameter(RequestConstants.FOLDER_ID); 182 if (website == null && folderid != null) { 183 BookmarkManager bm = RollerFactory.getRoller().getBookmarkManager(); 184 FolderData folder = bm.getFolder(folderid); 185 website = folder.getWebsite(); 186 } 187 if (website != null) { 188 request.getSession().setAttribute(RequestConstants.WEBLOG_SESSION_STASH, website.getHandle()); 189 } 190 else { 191 String handle = (String )request.getSession().getAttribute(RequestConstants.WEBLOG_SESSION_STASH); 192 Roller roller = RollerFactory.getRoller(); 193 website = roller.getUserManager().getWebsiteByHandle(handle); 194 } 195 return website; 196 } 197 } 198 199 | Popular Tags |