KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > bookmarks > actions > ImportBookmarksFormAction


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 JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27
28 /////////////////////////////////////////////////////////////////////////////
29
/**
30  * @struts.action name="folderFormEx" path="/editor/importBookmarks"
31  * scope="request" input="/bookmarks/import.jsp" validate="false"
32  *
33  * @struts.action-forward name="importBookmarks.page" path="/bookmarks/import.jsp"
34  *
35  * TODO Should import into folder with same name as imported file
36  */

37 public final class ImportBookmarksFormAction extends Action
38 {
39     private static Log mLogger =
40         LogFactory.getFactory().getInstance(RollerRequest.class);
41
42     /**
43      * Request to import bookmarks
44      */

45     public ActionForward execute(
46         ActionMapping mapping,
47         ActionForm actionForm,
48         HttpServletRequest JavaDoc request,
49         HttpServletResponse JavaDoc response)
50         throws IOException JavaDoc, ServletException JavaDoc
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             //this line is here for when the input page is upload-utf8.jsp,
58
//it sets the correct character encoding for the response
59
String JavaDoc 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; //theForm.getWriteFile();
66

67             //retrieve the file representation
68
FormFile file = theForm.getBookmarksFile();
69         /*
70             //retrieve the file name
71             String fileName= file.getFileName();
72
73             //retrieve the content type
74             String contentType = file.getContentType();
75
76             //retrieve the file size
77             String size = (file.getFileSize() + " bytes");
78         */

79             String JavaDoc data = null;
80
81             InputStream JavaDoc stream = null;
82             try
83             {
84                 //retrieve the file data
85
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
86                 stream = file.getInputStream();
87                 if (!writeFile)
88                 {
89                     //only write files out that are less than 1MB
90
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 JavaDoc(baos.toByteArray());
98
99                         SimpleDateFormat JavaDoc formatter =
100                                 new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
101                         Date JavaDoc now = new Date JavaDoc();
102                         String JavaDoc folderName = "imported-" + formatter.format(now);
103                         
104                         // Use Roller BookmarkManager to import bookmarks
105
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                     //write the file to the file specified
126
/*OutputStream bos =
127                         new FileOutputStream(theForm.getFilePath());
128                     int bytesRead = 0;
129                     byte[] buffer = new byte[8192];
130                     while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
131                         bos.write(buffer, 0, bytesRead);
132                     }
133                     bos.close();
134                     data = "The file has been written to \""
135                         + theForm.getFilePath() + "\"";
136                     */

137                 }
138             }
139             catch (Exception JavaDoc 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 JavaDoc e) { mLogger.error("Closing stream",e); };
152                 }
153             }
154
155             //destroy the temporary file created
156
file.destroy();
157         }
158         return fwd;
159     }
160 }
161
162
Popular Tags