ActionErrors errors = new ActionErrors();
FolderFormEx theForm = (FolderFormEx)actionForm;
ActionForward fwd = mapping.findForward("importBookmarks.page");
RollerRequest rreq = RollerRequest.getRollerRequest(request);
RollerSession rses = RollerSession.getRollerSession(request);
BookmarkManager bm = RollerFactory.getRoller().getBookmarkManager();
BasePageModel pageModel = new BasePageModel(
"bookmarksImport.title", request, response, mapping);
request.setAttribute("model", pageModel);
WebsiteData website = getWebsite(request);
pageModel.setWebsite(website);
// if user authorized and a file is being uploaded
if (rses.isUserAuthorizedToAuthor(website) && theForm.getBookmarksFile() != null) {
// this line is here for when the input page is upload-utf8.jsp,
// it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=utf-8");
}
boolean writeFile = false;
//retrieve the file representation
FormFile file = theForm.getBookmarksFile();
String data = null;
InputStream stream = null;
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4*1024000)) {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead=stream.read(buffer,0,8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
SimpleDateFormat formatter =
new SimpleDateFormat("yyyyMMddHHmmss");
Date now = new Date();
String folderName = "imported-" + formatter.format(now);
// Use Roller BookmarkManager to import bookmarks
bm.importBookmarks(website, folderName, data);
RollerFactory.getRoller().flush();
CacheManager.invalidate(website);
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("bookmarksImport.imported", folderName));
saveMessages(request, messages);
}
else {
data = "The file is greater than 4MB, "
+" and has not been written to stream."
+" File Size: "+file.getFileSize()+" bytes. "
+" This is a limitation of this particular "
+" web application, hard-coded in "
+" org.apache.struts.webapp.upload.UploadAction";
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("bookmarksImport.error",data));
}
}
}
catch (Exception e) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("bookmarksImport.error",e.toString()));
saveErrors(request,errors);
mLogger.error("ERROR: importing bookmarks",e);
}
finally {
if ( stream!=null ) {
try { stream.close(); }
catch (Exception e) { mLogger.error("Closing stream",e); };
}
}
//destroy the temporary file created
file.destroy();
}
else if (!rses.isUserAuthorizedToAuthor(website)) {
fwd = mapping.findForward("access-denied");
}
return fwd;
}