1 package com.dotmarketing.portlets.wysiwyg.action; 2 3 import java.io.IOException ; 4 5 import javax.portlet.ActionRequest; 6 import javax.portlet.ActionResponse; 7 import javax.portlet.PortletConfig; 8 import javax.servlet.jsp.PageContext ; 9 10 import org.apache.struts.action.ActionForm; 11 import org.apache.struts.action.ActionMapping; 12 13 import com.dotmarketing.factories.InodeFactory; 14 import com.dotmarketing.portlets.files.factories.FileFactory; 15 import com.dotmarketing.portlets.files.model.File; 16 import com.dotmarketing.util.Logger; 17 import com.dotmarketing.util.UtilMethods; 18 import com.dotmarketing.util.WebKeys; 19 import com.liferay.portal.struts.PortletAction; 20 import com.liferay.portal.util.Constants; 21 import com.liferay.util.servlet.SessionMessages; 22 25 26 public class EditTextAction extends PortletAction { 27 28 public void processAction( 29 ActionMapping mapping, ActionForm form, PortletConfig config, 30 ActionRequest req, ActionResponse res) 31 throws Exception { 32 33 String cmd = req.getParameter(Constants.CMD); 34 Logger.debug(this, "Inside EditTextAction cmd=" + cmd); 35 36 try { 37 Logger.debug(this, "Going to run SaveTextAction"); 38 _retrieveTextAction(req, res, config, form); 39 } 40 catch (Exception ae) { 41 Logger.error(this, ae.toString(), ae); 42 req.setAttribute(PageContext.EXCEPTION, ae); 43 setForward(req, Constants.COMMON_ERROR); 44 } 45 46 if ((cmd != null) && cmd.equals(Constants.SAVE)) { 47 try { 48 Logger.debug(this, "Going to run SaveTextAction"); 49 _saveTextAction(req, res, config, form); 50 setForward(req, "portlet.ext.wysiwyg.window_close"); 51 } 52 catch (Exception ae) { 53 Logger.error(this, ae.toString(), ae); 54 req.setAttribute(PageContext.EXCEPTION, ae); 55 setForward(req, Constants.COMMON_ERROR); 56 } 57 } 58 else { 59 setForward(req, "portlet.ext.wysiwyg.edit_text"); 60 } 61 } 62 private void _saveTextAction(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form) 63 throws Exception { 64 65 File file = (File) InodeFactory.getInode(req.getParameter("inode"), File.class); 66 67 Logger.debug(this, "I'm inside SaveTextAction"); 68 String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName()); 70 71 String filePath = FileFactory.getRealAssetsRootPath(); 73 74 String fileInodePath = String.valueOf(file.getInode()); 76 if (fileInodePath.length()==1) { 77 fileInodePath = fileInodePath + "0"; 78 } 79 try{ 80 81 fileInodePath = fileInodePath.substring(0,1) + java.io.File.separator + fileInodePath.substring(1,2); 83 84 java.io.File currF = new java.io.File (filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension); 86 87 java.io.FileOutputStream fout = new java.io.FileOutputStream (currF); 89 java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream (fout); 91 92 String text = req.getParameter("text"); 93 bout.write(text.getBytes()); 94 bout.flush(); 95 bout.close(); 96 97 SessionMessages.add(req, "message", "message.file.text.save"); 98 99 } 100 catch (IOException e) { 101 Logger.error(this, "Error opening file in SaveTextAction", e); 102 } 103 104 } 105 private void _retrieveTextAction(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form) 106 throws Exception { 107 108 File file = (File) InodeFactory.getInode(req.getParameter("inode"), File.class); 109 110 String fileExtension = com.dotmarketing.util.UtilMethods.getFileExtension(file.getFileName()); 112 113 String filePath = FileFactory.getRealAssetsRootPath(); 115 116 String fileInodePath = String.valueOf(file.getInode()); 118 if (fileInodePath.length()==1) { 119 fileInodePath = fileInodePath + "0"; 120 } 121 try{ 122 fileInodePath = fileInodePath.substring(0,1) + java.io.File.separator + fileInodePath.substring(1,2); 124 125 java.io.File currF = new java.io.File (filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension); 127 128 Logger.debug(this, "Opening file: " + filePath + java.io.File.separator + fileInodePath + java.io.File.separator + file.getInode() + "." + fileExtension); 129 130 java.io.FileInputStream is = new java.io.FileInputStream (currF); 132 java.io.BufferedInputStream bin = new java.io.BufferedInputStream (is); 134 135 StringBuffer strBuffer = new StringBuffer (); 136 137 byte[] strText = new byte[bin.available()]; 138 139 int result = bin.read(strText); 140 141 String strFinal = new String (strText); 142 143 req.setAttribute(WebKeys.WYSIWYG_EDIT_TEXT, UtilMethods.escapeHTMLSpecialChars(strFinal)); 144 } 145 catch (IOException e) { 146 Logger.error(this, "error opening file in EditTextAction", e); 147 } 148 } 149 150 } 151 | Popular Tags |