package REST_Plugins; import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.extentech.ExtenXLS.WorkSheetHandle; import com.extentech.ExtenXLS.ESS.WorkBookCommander; import com.extentech.ExtenXLS.plugin.RestCall; import com.extentech.ExtenXLS.plugin.RestTest; import com.extentech.ExtenXLS.web.WebWorkBook; import com.extentech.security.AclEntry; import com.extentech.security.User; /** * This custom plugin example overrides the buqilt in PluginWorkBook and adds two methods. * * as of this writing these methods exist in production however the example is valid. * * @author John McMahon :: May 2, 2011 :: Copyright ©2010 Extentech Inc. * */ public class PluginWorkBook extends com.extentech.ExtenXLS.plugin.builtin.PluginWorkBook { /** * get the entire workbook * * @param parameters * @return */ @RestTest ( target = "testPluginWorkBook.xls", url = "0" ) @RestCall ( formats = {"xml","pdf","xls","xlsx","xhtml"}, methods = {"GET"} ) public static Object get(Map parameters) { WebWorkBook wbh = (WebWorkBook)parameters.get(WorkBookCommander.WORKBOOK); // Override the default 'get' method to protect by default protectWorkBook(wbh, true, "HARD2GUESS" ); Object strax = com.extentech.ExtenXLS.plugin.builtin.PluginWorkBook.get(parameters); return strax; } /** * set all of the Sheets in the WorkBook to the same protection status * * this is a convenience method to allow use of Sheet level protection * throughout the workbook. * * locking the WorkSheets is a prerequisite for Cell locking * * @return WorkBook protection status */ @RestTest ( target = "testPluginWorkBook.xls", url = "workbook/true") @RestCall ( formats = {"txt"}, methods = {"GET"} ) public static String setprotected(Map parameters) { WebWorkBook wbh = (WebWorkBook)parameters.get(WorkBookCommander.WORKBOOK); String bval = (String)parameters.get(WorkBookCommander.VALUE); HttpServletRequest req = (HttpServletRequest)parameters.get(WorkBookCommander.REQUEST); String password = req.getParameter("password"); try { boolean lck = false; boolean success = true; if(bval != null) lck = bval.equalsIgnoreCase("true"); success = protectWorkBook(wbh, lck, password); return String.valueOf(success); }catch(Exception e) { return WorkBookCommander.returnXMLErrorResponse("Set WorkBook Protection: " + bval + " failed. " + e.toString()); } } private static boolean protectWorkBook(WebWorkBook wbh, boolean lck, String password ){ WorkSheetHandle[] shts = wbh.getWorkSheets(); boolean success = true; for(WorkSheetHandle c : shts) { c.setProtected(lck, password); if(!c.getProtected() == lck) success = false; } return success; } /** * Check acccess for different methods based on userid, memeid, and methodcalled * * This method cannot be reached unless the user has at least read permissions * to the meme, so methods that only read can return true without checking. */ @Override public boolean checkAccess(Map parameters){ User usr = (User)parameters.get(WorkBookCommander.USER); String command = (String)parameters.get(WorkBookCommander.COMMAND); String memeid = (String)parameters.get(WorkBookCommander.ID); // defer to superclass for non-overridden methods if(super.checkAccess(parameters)) return true; command = command.toLowerCase(); if(command.equals("setprotected")){ if (usr.checkAccess("meme_" + memeid, AclEntry.UPDATE)){ return true; } } return false; } }