001/* 002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * tdelprat, jcarsique 018 * 019 */ 020 021package org.nuxeo.wizard.context; 022 023import static org.nuxeo.launcher.config.ConfigurationGenerator.NUXEO_DEV_SYSTEM_PROP; 024 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.Enumeration; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import javax.servlet.http.HttpServletRequest; 033 034import org.apache.commons.text.StringEscapeUtils; 035import org.nuxeo.launcher.config.ConfigurationGenerator; 036 037/** 038 * Manages all the parameters entered by User to configure his Nuxeo server 039 * 040 * @author Tiry ([email protected]) 041 * @since 5.4.2 042 */ 043public class ParamCollector { 044 045 public static final String Key = "collector"; 046 047 private static final String SKIP_SECTIONS_KEY = "nuxeo.wizard.skippedsections"; 048 049 private List<String> sectionsToSkip; 050 051 private ConfigurationGenerator configurationGenerator; 052 053 public ConfigurationGenerator getConfigurationGenerator() { 054 return configurationGenerator; 055 } 056 057 protected Map<String, String> configurationParams = new HashMap<>(); 058 059 protected Map<String, String> connectParams = new HashMap<>(); 060 061 public ParamCollector() { 062 configurationGenerator = new ConfigurationGenerator(); 063 configurationGenerator.init(); 064 String skipSections = configurationGenerator.getUserConfig().getProperty(SKIP_SECTIONS_KEY, ""); 065 sectionsToSkip = Arrays.asList(skipSections.split(",")); 066 } 067 068 public boolean isSectionSkipped(String section) { 069 return sectionsToSkip.contains(section); 070 } 071 072 public void addConfigurationParam(String name, String value) { 073 if (value == null) { 074 configurationParams.remove(name); 075 } else { 076 configurationParams.put(name, value); 077 } 078 } 079 080 public void addConnectParam(String name, String value) { 081 configurationParams.put(name, value); 082 } 083 084 public Map<String, String> getConfigurationParams() { 085 return configurationParams; 086 } 087 088 public Map<String, String> getChangedParameters() { 089 Map<String, String> changedParameters = configurationGenerator.getChangedParameters(configurationParams); 090 // Don't consider "org.nuxeo.dev" as changed if unset in the config and unchecked in the form 091 if (configurationGenerator.getUserConfig().get(NUXEO_DEV_SYSTEM_PROP) == null 092 && !Boolean.parseBoolean(changedParameters.get(NUXEO_DEV_SYSTEM_PROP))) { 093 changedParameters.remove(NUXEO_DEV_SYSTEM_PROP); 094 } 095 return changedParameters; 096 } 097 098 public String getConfigurationParam(String name) { 099 return getConfigurationParam(name, ""); 100 } 101 102 public String getConfigurationParam(String name, String defaultValue) { 103 String param = configurationParams.get(name); 104 if (param == null) { 105 param = configurationGenerator.getUserConfig().getProperty(name, defaultValue); 106 } 107 return param; 108 } 109 110 public String getConfigurationParamValue(String name) { 111 return configurationParams.get(name); 112 } 113 114 public Map<String, String> getConnectParams() { 115 return connectParams; 116 } 117 118 public void collectConfigurationParams(HttpServletRequest req) { 119 Enumeration<String> names = req.getParameterNames(); 120 while (names.hasMoreElements()) { 121 String name = names.nextElement(); 122 if (name.startsWith("org.nuxeo.") || name.startsWith("nuxeo.") || name.startsWith("mail.")) { 123 String value = req.getParameter(name); 124 if (!value.isEmpty() || (value.isEmpty() && configurationParams.containsKey(name))) { 125 addConfigurationParam(name, StringEscapeUtils.escapeHtml4(value)); 126 } 127 } 128 } 129 } 130 131 /** 132 * @see ConfigurationGenerator#changeDBTemplate(String) 133 * @param templateName database template to use 134 */ 135 public void changeDBTemplate(String templateName) { 136 configurationGenerator.changeDBTemplate(templateName); 137 } 138 139 /** 140 * @since 8.1 141 */ 142 public void removeDbKeys() { 143 List<String> keys = new ArrayList<>(); 144 for (String key : configurationParams.keySet()) { 145 if (key.startsWith("nuxeo.db") || key.startsWith("nuxeo.mongodb")) { 146 keys.add(key); 147 } 148 } 149 for (String key : keys) { 150 configurationParams.remove(key); 151 } 152 } 153 154}