public synchronized List<String> getAllPolicyNames() { if (allPolicyNames == null) { allPolicyNames = new ArrayList<String>(); for (String file : Constant.getPoliciesDir().list()) { if (file.endsWith(POLICY_EXTENSION)) { logger.debug("Found policy file " + file); allPolicyNames.add(file.substring(0, file.lastIndexOf(POLICY_EXTENSION))); } } if (allPolicyNames.size() == 0) { // No policies :( Create a default one ScanPolicy defaultPolicy = new ScanPolicy(); defaultPolicy.setName(DEFAULT_POLICY_NAME); // Load from the 'old' configs defaultPolicy.getPluginFactory().loadAllPlugin(extension.getScannerParam().getConfig()); try { // Note this will add the name to allPolicyNames this.savePolicy(defaultPolicy); } catch (ConfigurationException e) { logger.debug( "Failed to create default scan policy in " + Constant.getPoliciesDir().getAbsolutePath(), e); } } Collections.sort(allPolicyNames); } return allPolicyNames; }
private ScanPolicy loadPolicy(File file) throws ConfigurationException { ScanPolicy policy = new ScanPolicy(new ZapXmlConfiguration(file)); if (!file.getName().equals(policy.getName() + POLICY_EXTENSION)) { // The file name takes precedence in case theres another policy with the same name policy.setName(file.getName().substring(0, file.getName().indexOf(POLICY_EXTENSION))); } return policy; }
public void importPolicy(File file) throws ConfigurationException, IOException { logger.debug("Import policy from " + file.getAbsolutePath()); ScanPolicy policy = new ScanPolicy(new ZapXmlConfiguration(file)); String baseName = file.getName(); if (baseName.endsWith(POLICY_EXTENSION)) { // Stip off the extension for the 'friendly name' and if we need to prevent overwriting an // existing one baseName = baseName.substring(0, baseName.indexOf(POLICY_EXTENSION)); } String finalName = baseName; File newFile = new File(Constant.getPoliciesDir(), finalName + POLICY_EXTENSION); int i = 2; while (newFile.exists()) { finalName = baseName + i; newFile = new File(Constant.getPoliciesDir(), finalName + POLICY_EXTENSION); i++; } policy.setName(finalName); this.savePolicy(policy); }