KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > ImportBookmarksFormAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18
19 package org.apache.roller.ui.authoring.struts.actions;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc;
41 import java.util.Date JavaDoc;
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 /////////////////////////////////////////////////////////////////////////////
54
/**
55  * @struts.action name="folderFormEx" path="/roller-ui/authoring/importBookmarks"
56  * scope="request" input=".import" validate="false"
57  *
58  * @struts.action-forward name="importBookmarks.page" path=".import"
59  *
60  * TODO Should import into folder with same name as imported file
61  */

62 public final class ImportBookmarksFormAction extends Action {
63       
64     private static Log mLogger =
65             LogFactory.getFactory().getInstance(RollerRequest.class);
66     
67     /**
68      * Request to import bookmarks
69      */

70     public ActionForward execute(
71             ActionMapping mapping,
72             ActionForm actionForm,
73             HttpServletRequest JavaDoc request,
74             HttpServletResponse JavaDoc response)
75             throws Exception JavaDoc {
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 user authorized and a file is being uploaded
93
if (rses.isUserAuthorizedToAuthor(website) && theForm.getBookmarksFile() != null) {
94             
95             // this line is here for when the input page is upload-utf8.jsp,
96
// it sets the correct character encoding for the response
97
String JavaDoc 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             //retrieve the file representation
104
FormFile file = theForm.getBookmarksFile();
105             String JavaDoc data = null;
106             InputStream JavaDoc stream = null;
107             try {
108                
109                 //retrieve the file data
110
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
111                 stream = file.getInputStream();
112                 if (!writeFile) {
113                     //only write files out that are less than 1MB
114
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 JavaDoc(baos.toByteArray());
122                         
123                         SimpleDateFormat JavaDoc formatter =
124                                 new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
125                         Date JavaDoc now = new Date JavaDoc();
126                         String JavaDoc folderName = "imported-" + formatter.format(now);
127                         
128                         // Use Roller BookmarkManager to import bookmarks
129

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 JavaDoc 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 JavaDoc e) { mLogger.error("Closing stream",e); };
162                 }
163             }
164             //destroy the temporary file created
165
file.destroy();
166         }
167         else if (!rses.isUserAuthorizedToAuthor(website)) {
168             fwd = mapping.findForward("access-denied");
169         }
170         return fwd;
171     }
172     
173     /**
174      * Other actions can get the website handle from request params, but
175      * request params don't come accross in a file-upload post so we have to
176      * stash the website handle in the session.
177      */

178     public static WebsiteData getWebsite(HttpServletRequest JavaDoc request) throws RollerException {
179         RollerRequest rreq = RollerRequest.getRollerRequest(request);
180         WebsiteData website = rreq.getWebsite();
181         String JavaDoc 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 JavaDoc handle = (String JavaDoc)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