List of usage examples for org.apache.commons.configuration XMLConfiguration setProperty
public void setProperty(String key, Object value)
From source file:org.parosproxy.paros.Constant.java
private void initializeFilesAndDirectories() { FileCopier copier = new FileCopier(); File f = null;/*from w w w .j a va2 s. co m*/ Logger log = null; // default to use application directory 'log' System.setProperty(SYSTEM_PAROS_USER_LOG, "log"); // Set up the version from the manifest PROGRAM_VERSION = getVersionFromManifest(); PROGRAM_TITLE = PROGRAM_NAME + " " + PROGRAM_VERSION; if (zapHome == null) { zapHome = getDefaultHomeDirectory(true); } zapHome = getAbsolutePath(zapHome); f = new File(zapHome); FILE_CONFIG = zapHome + FILE_CONFIG; FOLDER_SESSION = zapHome + FOLDER_SESSION; DBNAME_UNTITLED = zapHome + DBNAME_UNTITLED; ACCEPTED_LICENSE = zapHome + ACCEPTED_LICENSE; DIRBUSTER_CUSTOM_DIR = zapHome + DIRBUSTER_DIR; FUZZER_DIR = zapHome + FUZZER_DIR; FOLDER_LOCAL_PLUGIN = zapHome + FOLDER_LOCAL_PLUGIN; try { System.setProperty(SYSTEM_PAROS_USER_LOG, zapHome); if (!f.isDirectory()) { if (!f.mkdir()) { // ZAP: report failure to create directory System.out.println("Failed to create directory " + f.getAbsolutePath()); } } // Setup the logging File logFile = new File(zapHome + "/log4j.properties"); if (!logFile.exists()) { copier.copy(new File(zapInstall, "xml/log4j.properties"), logFile); } System.setProperty("log4j.configuration", logFile.getAbsolutePath()); PropertyConfigurator.configure(logFile.getAbsolutePath()); log = Logger.getLogger(Constant.class); f = new File(FILE_CONFIG); if (!f.isFile()) { File oldf; if (isDevBuild() || isDailyBuild()) { // try standard location oldf = new File(getDefaultHomeDirectory(false) + FILE_SEPARATOR + FILE_CONFIG_NAME); } else { // try old location oldf = new File(zapHome + FILE_SEPARATOR + "zap" + FILE_SEPARATOR + FILE_CONFIG_NAME); } if (oldf.exists() && Paths.get(zapHome).equals(Paths.get(getDefaultHomeDirectory(true)))) { // Dont copy old configs if they've specified a non std directory log.info("Copying defaults from " + oldf.getAbsolutePath() + " to " + FILE_CONFIG); copier.copy(oldf, f); } else { log.info("Copying defaults from " + getPathDefaultConfigFile() + " to " + FILE_CONFIG); copier.copy(getPathDefaultConfigFile().toFile(), f); } } f = new File(FOLDER_SESSION); if (!f.isDirectory()) { log.info("Creating directory " + FOLDER_SESSION); if (!f.mkdir()) { // ZAP: report failure to create directory System.out.println("Failed to create directory " + f.getAbsolutePath()); } } f = new File(DIRBUSTER_CUSTOM_DIR); if (!f.isDirectory()) { log.info("Creating directory " + DIRBUSTER_CUSTOM_DIR); if (!f.mkdir()) { // ZAP: report failure to create directory System.out.println("Failed to create directory " + f.getAbsolutePath()); } } f = new File(FUZZER_DIR); if (!f.isDirectory()) { log.info("Creating directory " + FUZZER_DIR); if (!f.mkdir()) { // ZAP: report failure to create directory System.out.println("Failed to create directory " + f.getAbsolutePath()); } } f = new File(FOLDER_LOCAL_PLUGIN); if (!f.isDirectory()) { log.info("Creating directory " + FOLDER_LOCAL_PLUGIN); if (!f.mkdir()) { // ZAP: report failure to create directory System.out.println("Failed to create directory " + f.getAbsolutePath()); } } } catch (Exception e) { System.err.println("Unable to initialize home directory! " + e.getMessage()); e.printStackTrace(System.err); System.exit(1); } // Upgrade actions try { try { // ZAP: Changed to use ZapXmlConfiguration, to enforce the same character encoding when reading/writing configurations. XMLConfiguration config = new ZapXmlConfiguration(FILE_CONFIG); config.setAutoSave(false); long ver = config.getLong("version"); if (ver == VERSION_TAG) { // Nothing to do } else if (isDevBuild() || isDailyBuild()) { // Nothing to do } else { // Backup the old one log.info("Backing up config file to " + FILE_CONFIG + ".bak"); f = new File(FILE_CONFIG); try { copier.copy(f, new File(FILE_CONFIG + ".bak")); } catch (IOException e) { String msg = "Failed to backup config file " + FILE_CONFIG + " to " + FILE_CONFIG + ".bak " + e.getMessage(); System.err.println(msg); log.error(msg, e); } if (ver == V_PAROS_TAG) { upgradeFrom1_1_0(config); upgradeFrom1_2_0(config); } if (ver <= V_1_0_0_TAG) { // Nothing to do } if (ver <= V_1_1_0_TAG) { upgradeFrom1_1_0(config); } if (ver <= V_1_2_0_TAG) { upgradeFrom1_2_0(config); } if (ver <= V_1_2_1_TAG) { // Nothing to do } if (ver <= V_1_3_0_TAG) { // Nothing to do } if (ver <= V_1_3_1_TAG) { // Nothing to do } if (ver <= V_1_4_1_TAG) { upgradeFrom1_4_1(config); } if (ver <= V_2_0_0_TAG) { upgradeFrom2_0_0(config); } if (ver <= V_2_1_0_TAG) { // Nothing to do } if (ver <= V_2_2_0_TAG) { upgradeFrom2_2_0(config); } if (ver <= V_2_3_1_TAG) { upgradeFrom2_3_1(config); } log.info("Upgraded from " + ver); // Update the version config.setProperty("version", VERSION_TAG); config.save(); } } catch (ConfigurationException | ConversionException | NoSuchElementException e) { // if there is any error in config file (eg config file not exist, corrupted), // overwrite previous configuration file // ZAP: changed to use the correct file copier.copy(getPathDefaultConfigFile().toFile(), new File(FILE_CONFIG)); } } catch (Exception e) { System.err.println("Unable to upgrade config file " + FILE_CONFIG + " " + e.getMessage()); e.printStackTrace(System.err); System.exit(1); } // ZAP: Init i18n String lang; Locale locale = Locale.ENGLISH; try { // Select the correct locale // ZAP: Changed to use ZapXmlConfiguration, to enforce the same character encoding when reading/writing configurations. XMLConfiguration config = new ZapXmlConfiguration(FILE_CONFIG); config.setAutoSave(false); lang = config.getString(OptionsParamView.LOCALE, OptionsParamView.DEFAULT_LOCALE); if (lang.length() == 0) { lang = OptionsParamView.DEFAULT_LOCALE; } String[] langArray = lang.split("_"); locale = new Locale(langArray[0], langArray[1]); } catch (Exception e) { System.out.println("Failed to initialise locale " + e); } Locale.setDefault(locale); messages = new I18N(locale); }
From source file:org.parosproxy.paros.Constant.java
private void copyProperty(XMLConfiguration fromConfig, XMLConfiguration toConfig, String key) { toConfig.setProperty(key, fromConfig.getProperty(key)); }
From source file:org.parosproxy.paros.Constant.java
private void upgradeFrom1_4_1(XMLConfiguration config) { // As the POST_FORM option for the spider has been updated from int to boolean, keep // compatibility for old versions if (!config.getProperty("spider.postform").toString().equals("0")) { config.setProperty("spider.postform", "true"); config.setProperty("spider.processform", "true"); } else {//from ww w .j a v a2 s.co m config.setProperty("spider.postform", "false"); config.setProperty("spider.processform", "false"); } // Move the old session tokens to the new "httpsessions" hierarchy and // delete the old "session" hierarchy as it's no longer used/needed. String[] tokens = config.getStringArray("session.tokens"); for (int i = 0; i < tokens.length; ++i) { String elementBaseKey = "httpsessions.tokens.token(" + i + ")."; config.setProperty(elementBaseKey + "name", tokens[i]); config.setProperty(elementBaseKey + "enabled", Boolean.TRUE); } config.clearTree("session"); // Update the anti CSRF tokens elements/hierarchy. tokens = config.getStringArray("anticsrf.tokens"); config.clearTree("anticsrf.tokens"); for (int i = 0; i < tokens.length; ++i) { String elementBaseKey = "anticsrf.tokens.token(" + i + ")."; config.setProperty(elementBaseKey + "name", tokens[i]); config.setProperty(elementBaseKey + "enabled", Boolean.TRUE); } // Update the invoke applications elements/hierarchy. List<Object[]> oldData = new ArrayList<>(); for (int i = 0;; i++) { String baseKey = "invoke.A" + i + "."; String host = config.getString(baseKey + "name"); if (host == null || "".equals(host)) { break; } Object[] data = new Object[6]; data[0] = host; data[1] = config.getString(baseKey + "directory", ""); data[2] = config.getString(baseKey + "command"); data[3] = config.getString(baseKey + "parameters"); data[4] = Boolean.valueOf(config.getBoolean(baseKey + "output", true)); data[5] = Boolean.valueOf(config.getBoolean(baseKey + "note", false)); oldData.add(data); } config.clearTree("invoke.A"); for (int i = 0, size = oldData.size(); i < size; ++i) { String elementBaseKey = "invoke.apps.app(" + i + ")."; Object[] data = oldData.get(i); config.setProperty(elementBaseKey + "name", data[0]); config.setProperty(elementBaseKey + "directory", data[1]); config.setProperty(elementBaseKey + "command", data[2]); config.setProperty(elementBaseKey + "parameters", data[3]); config.setProperty(elementBaseKey + "output", data[4]); config.setProperty(elementBaseKey + "note", data[5]); config.setProperty(elementBaseKey + "enabled", Boolean.TRUE); } // Update the authentication elements/hierarchy. oldData = new ArrayList<>(); for (int i = 0;; i++) { String baseKey = "connection.auth.A" + i + "."; String host = config.getString(baseKey + "hostName"); if (host == null || "".equals(host)) { break; } Object[] data = new Object[5]; data[0] = host; data[1] = Integer.valueOf(config.getString(baseKey + "port", "80")); data[2] = config.getString(baseKey + "userName"); data[3] = config.getString(baseKey + "password"); data[4] = config.getString(baseKey + "realm"); oldData.add(data); } config.clearTree("connection.auth.A"); for (int i = 0, size = oldData.size(); i < size; ++i) { String elementBaseKey = "connection.auths.auth(" + i + ")."; Object[] data = oldData.get(i); config.setProperty(elementBaseKey + "name", "Auth " + i); config.setProperty(elementBaseKey + "hostName", data[0]); config.setProperty(elementBaseKey + "port", data[1]); config.setProperty(elementBaseKey + "userName", data[2]); config.setProperty(elementBaseKey + "password", data[3]); config.setProperty(elementBaseKey + "realm", data[4]); config.setProperty(elementBaseKey + "enabled", Boolean.TRUE); } // Update the passive scan elements/hierarchy. String[] names = config.getStringArray("pscans.names"); oldData = new ArrayList<>(); for (String pscanName : names) { String baseKey = "pscans." + pscanName + "."; Object[] data = new Object[8]; data[0] = pscanName; data[1] = config.getString(baseKey + "type"); data[2] = config.getString(baseKey + "config"); data[3] = config.getString(baseKey + "reqUrlRegex"); data[4] = config.getString(baseKey + "reqHeadRegex"); data[5] = config.getString(baseKey + "resHeadRegex"); data[6] = config.getString(baseKey + "resBodyRegex"); data[7] = Boolean.valueOf(config.getBoolean(baseKey + "enabled")); oldData.add(data); } config.clearTree("pscans.names"); for (String pscanName : names) { config.clearTree("pscans." + pscanName); } for (int i = 0, size = oldData.size(); i < size; ++i) { String elementBaseKey = "pscans.autoTagScanners.scanner(" + i + ")."; Object[] data = oldData.get(i); config.setProperty(elementBaseKey + "name", data[0]); config.setProperty(elementBaseKey + "type", data[1]); config.setProperty(elementBaseKey + "config", data[2]); config.setProperty(elementBaseKey + "reqUrlRegex", data[3]); config.setProperty(elementBaseKey + "reqHeadRegex", data[4]); config.setProperty(elementBaseKey + "resHeadRegex", data[5]); config.setProperty(elementBaseKey + "resBodyRegex", data[6]); config.setProperty(elementBaseKey + "enabled", data[7]); } }
From source file:org.parosproxy.paros.Constant.java
private void upgradeFrom2_0_0(XMLConfiguration config) { String forcedBrowseFile = config.getString("bruteforce.defaultFile", ""); if (!"".equals(forcedBrowseFile)) { String absolutePath = ""; // Try the 'local' dir first File f = new File(DIRBUSTER_CUSTOM_DIR + File.separator + forcedBrowseFile); if (!f.exists()) { f = new File(DIRBUSTER_DIR + File.separator + forcedBrowseFile); }//w w w. java 2 s .c o m if (f.exists()) { absolutePath = f.getAbsolutePath(); } config.setProperty("bruteforce.defaultFile", absolutePath); } // Remove the manual request editor configurations that were incorrectly created. config.clearTree("nullrequest"); config.clearTree("nullresponse"); }
From source file:org.parosproxy.paros.Constant.java
private void upgradeFrom2_2_0(XMLConfiguration config) { try {/*from w w w.j av a2 s. co m*/ if (!config.getBoolean(OptionsParamCheckForUpdates.CHECK_ON_START, false)) { /* * Check-for-updates on start set to false - force another prompt to ask the user, * as this option can have been unset incorrectly before. * And we want to encourage users to use this ;) */ config.setProperty(OptionsParamCheckForUpdates.DAY_LAST_CHECKED, ""); } } catch (Exception e) { // At one stage this was an integer, which will cause an exception to be thrown config.setProperty(OptionsParamCheckForUpdates.DAY_LAST_CHECKED, ""); } // Clear the block list - addons were incorrectly added to this if an update failed config.setProperty(AddOnLoader.ADDONS_BLOCK_LIST, ""); }
From source file:org.settings4j.helper.configuration.ConfigurationToConnectorAdapterTest.java
@Test public void testAdapterXmlConfigWithDefaultDelimiter() throws Exception { final Settings4jRepository testSettings = createSimpleSettings4jConfig(); // start test => create adapter and add to Settings4jRepository final XMLConfiguration configuration = addXmlConfiguration(// testSettings, "myXmlConfigConnector", "xmlConfigWithDefaultDelimiter.xml", "/"); // configure some values configuration.setProperty(TEST_VALUE_KEY, "Hello Windows World"); configuration.save();//ww w . java 2 s.com // validate result assertThat(testSettings.getSettings().getString(TEST_VALUE_KEY), is("Hello Windows World")); assertThat(testSettings.getSettings().getString("unknown/Value"), is(nullValue())); }
From source file:org.zaproxy.gradle.UpdateDailyZapVersionsEntries.java
@TaskAction public void update() throws Exception { File dailyRelease = from.get().getAsFile(); if (!Files.isRegularFile(dailyRelease.toPath())) { throw new IllegalArgumentException( "The provided daily release does not exist or it's not a file: " + dailyRelease); }/* www . j a va 2 s .c om*/ if (checksumAlgorithm.get().isEmpty()) { throw new IllegalArgumentException("The checksum algorithm must not be empty."); } if (baseDownloadUrl.get().isEmpty()) { throw new IllegalArgumentException("The base download URL must not be empty."); } String fileName = dailyRelease.getName(); String dailyVersion = getDailyVersion(fileName); String hash = createChecksum(checksumAlgorithm.get(), dailyRelease); String size = String.valueOf(dailyRelease.length()); String url = baseDownloadUrl.get() + dailyVersion.substring(2) + "/" + fileName; for (File zapVersionsFile : into.getFiles()) { if (!Files.isRegularFile(zapVersionsFile.toPath())) { throw new IllegalArgumentException("The provided path is not a file: " + zapVersionsFile); } XMLConfiguration zapVersionsXml = new CustomXmlConfiguration(); zapVersionsXml.load(zapVersionsFile); zapVersionsXml.setProperty(DAILY_VERSION_ELEMENT, dailyVersion); zapVersionsXml.setProperty(DAILY_FILE_ELEMENT, fileName); zapVersionsXml.setProperty(DAILY_HASH_ELEMENT, hash); zapVersionsXml.setProperty(DAILY_SIZE_ELEMENT, size); zapVersionsXml.setProperty(DAILY_URL_ELEMENT, url); zapVersionsXml.save(zapVersionsFile); } }
From source file:pl.otros.logview.gui.LogViewMainFrame.java
/** * @param args porgram CLI arguments/*from ww w . j av a 2 s. c om*/ * @throws InitializationException * @throws InvocationTargetException * @throws InterruptedException */ public static void main(final String[] args) throws InitializationException, InterruptedException, InvocationTargetException { if (args.length > 0 && "-batch".equals(args[0])) { try { String[] batchArgs = new String[args.length - 1]; System.arraycopy(args, 1, batchArgs, 0, batchArgs.length); BatchProcessor.main(batchArgs); } catch (IOException e) { System.err.println("Error during batch processing: " + e.getMessage()); e.printStackTrace(); } catch (ConfigurationException e) { System.err.println("Error during batch processing: " + e.getMessage()); e.printStackTrace(); } return; } SingleInstanceRequestResponseDelegate singleInstanceRequestResponseDelegate = SingleInstanceRequestResponseDelegate .getInstance(); singleInstance = SingleInstance.request("OtrosLogViewer", singleInstanceRequestResponseDelegate, singleInstanceRequestResponseDelegate, args); if (singleInstance == null) { LOGGER.info("OtrosLogViewer is already running, params send using requestAction"); System.exit(0); } GuiJulHandler handler = new GuiJulHandler(); handler.setLevel(Level.ALL); Logger olvLogger = Logger.getLogger("pl.otros.logview"); olvLogger.setLevel(Level.ALL); olvLogger.addHandler(handler); LOGGER.info("Starting application"); OtrosSplash.setMessage("Starting application"); OtrosSplash.setMessage("Loading configuration"); final XMLConfiguration c = getConfiguration("config.xml"); if (!c.containsKey(ConfKeys.UUID)) { c.setProperty(ConfKeys.UUID, UUID.randomUUID().toString()); } IconsLoader.loadIcons(); OtrosSplash.setMessage("Loading icons"); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { try { OtrosSplash.setMessage("Loading L&F"); String lookAndFeel = c.getString("lookAndFeel", "com.jgoodies.looks.plastic.PlasticXPLookAndFeel"); LOGGER.config("Initializing look and feelL: " + lookAndFeel); PlasticLookAndFeel.setTabStyle(Plastic3DLookAndFeel.TAB_STYLE_METAL_VALUE); UIManager.setLookAndFeel(lookAndFeel); } catch (Throwable e1) { LOGGER.warning("Cannot initialize LookAndFeel: " + e1.getMessage()); } try { final DataConfiguration c1 = new OtrosConfiguration(c); final LogViewMainFrame mf = new LogViewMainFrame(c1); // mf.exitAction was instantiated in the constructor (previous line) // Not sure retrieving this from most appropriate Apache config // object. mf.exitAction.setConfirm(c.getBoolean("generalBehavior.confirmExit", true)); /* TODO: Implement User Preferences screen or checkbox on exit widget * that will update the same config object something like: * c.setProperty("generalBehavior.confirmExit", newValue); */ mf.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { c.setProperty("gui.state", mf.getExtendedState()); if (mf.getExtendedState() == Frame.NORMAL) { c.setProperty("gui.width", mf.getWidth()); c.setProperty("gui.height", mf.getHeight()); } } @Override public void componentMoved(ComponentEvent e) { c.setProperty("gui.location.x", mf.getLocation().x); c.setProperty("gui.location.y", mf.getLocation().y); } }); mf.addWindowListener(mf.exitAction); SingleInstanceRequestResponseDelegate.openFilesFromStartArgs(mf.otrosApplication, Arrays.asList(args), mf.otrosApplication.getAppProperties().getCurrentDir()); } catch (InitializationException e) { LOGGER.log(Level.SEVERE, "Cannot initialize main frame", e); } } }); }
From source file:pl.otros.logview.gui.LogViewMainFrame.java
private static XMLConfiguration getConfiguration(String file) { XMLConfiguration commonConfiguration = new XMLConfiguration(); File commonConfigurationFile = new File(file); // load common configuration if (commonConfigurationFile.exists()) { LOGGER.info("Loading common configuration from " + commonConfigurationFile.getAbsolutePath()); try {//from w ww. j av a2 s .c om commonConfiguration.load(commonConfigurationFile); } catch (ConfigurationException e) { LOGGER.severe("Can't load configuration, creating new " + e.getMessage()); } } else { LOGGER.info("Common configuration file do not exist"); } // load user specific configuration if (!AllPluginables.USER_CONFIGURATION_DIRECTORY.exists()) { LOGGER.info("Creating user specific OtrosLogViewer configuration directory " + AllPluginables.USER_CONFIGURATION_DIRECTORY.getAbsolutePath()); AllPluginables.USER_CONFIGURATION_DIRECTORY.mkdirs(); AllPluginables.USER_FILTER.mkdirs(); AllPluginables.USER_LOG_IMPORTERS.mkdirs(); AllPluginables.USER_MARKERS.mkdirs(); AllPluginables.USER_MESSAGE_FORMATTER_COLORZIERS.mkdirs(); } XMLConfiguration userConfiguration = new XMLConfiguration(); File userConfigurationFile = new File(AllPluginables.USER_CONFIGURATION_DIRECTORY + File.separator + file); userConfiguration.setFile(userConfigurationFile); if (userConfigurationFile.exists()) { try { userConfiguration.load(); } catch (ConfigurationException e) { LOGGER.severe(String.format("Can't load user configuration from %s: %s", userConfigurationFile.getAbsolutePath(), e.getMessage())); } } Iterator<?> keys = commonConfiguration.getKeys(); while (keys.hasNext()) { String key = (String) keys.next(); if (!userConfiguration.containsKey(key)) { userConfiguration.setProperty(key, commonConfiguration.getProperty(key)); } } userConfiguration.setAutoSave(true); return userConfiguration; }