1 2 package org.roller.presentation.bookmarks.actions; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.struts.action.Action; 7 import org.apache.struts.action.ActionError; 8 import org.apache.struts.action.ActionErrors; 9 import org.apache.struts.action.ActionForm; 10 import org.apache.struts.action.ActionForward; 11 import org.apache.struts.action.ActionMapping; 12 import org.apache.struts.upload.FormFile; 13 import org.roller.model.BookmarkManager; 14 import org.roller.presentation.RollerRequest; 15 import org.roller.presentation.bookmarks.formbeans.FolderFormEx; 16 17 import java.io.ByteArrayOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 28 37 public final class ImportBookmarksFormAction extends Action 38 { 39 private static Log mLogger = 40 LogFactory.getFactory().getInstance(RollerRequest.class); 41 42 45 public ActionForward execute( 46 ActionMapping mapping, 47 ActionForm actionForm, 48 HttpServletRequest request, 49 HttpServletResponse response) 50 throws IOException , ServletException 51 { 52 ActionErrors errors = new ActionErrors(); 53 FolderFormEx theForm = (FolderFormEx)actionForm; 54 ActionForward fwd = mapping.findForward("importBookmarks.page"); 55 if ( theForm.getBookmarksFile() != null ) 56 { 57 String encoding = request.getCharacterEncoding(); 60 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) 61 { 62 response.setContentType("text/html; charset=utf-8"); 63 } 64 65 boolean writeFile = false; 67 FormFile file = theForm.getBookmarksFile(); 69 79 String data = null; 80 81 InputStream stream = null; 82 try 83 { 84 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 86 stream = file.getInputStream(); 87 if (!writeFile) 88 { 89 if (file.getFileSize() < (4*1024000)) { 91 92 byte[] buffer = new byte[8192]; 93 int bytesRead = 0; 94 while ((bytesRead=stream.read(buffer,0,8192)) != -1) { 95 baos.write(buffer, 0, bytesRead); 96 } 97 data = new String (baos.toByteArray()); 98 99 SimpleDateFormat formatter = 100 new SimpleDateFormat ("yyyyMMddHHmmss"); 101 Date now = new Date (); 102 String folderName = "imported-" + formatter.format(now); 103 104 RollerRequest rreq = 106 RollerRequest.getRollerRequest(request); 107 BookmarkManager bm = 108 rreq.getRoller().getBookmarkManager(); 109 bm.importBookmarks(rreq.getWebsite(), folderName, data); 110 111 rreq.getRoller().commit(); 112 } 113 else 114 { 115 data = "The file is greater than 4MB, " 116 +" and has not been written to stream." 117 +" File Size: "+file.getFileSize()+" bytes. " 118 +" This is a limitation of this particular " 119 +" web application, hard-coded in " 120 +" org.apache.struts.webapp.upload.UploadAction"; 121 } 122 } 123 else 124 { 125 137 } 138 } 139 catch (Exception e) 140 { 141 errors.add(ActionErrors.GLOBAL_ERROR, 142 new ActionError("error.importing.bookmarks",e.toString())); 143 saveErrors(request,errors); 144 mLogger.error("ERROR: importing bookmarks",e); 145 } 146 finally 147 { 148 if ( stream!=null ) 149 { 150 try { stream.close(); } 151 catch (Exception e) { mLogger.error("Closing stream",e); }; 152 } 153 } 154 155 file.destroy(); 157 } 158 return fwd; 159 } 160 } 161 162 | Popular Tags |