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.apache.commons.lang.StringEscapeUtils.escapeHtml; 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.nuxeo.launcher.config.ConfigurationGenerator; 035 036/** 037 * Manages all the parameters entered by User to configure his Nuxeo server 038 * 039 * @author Tiry (tdelprat@nuxeo.com) 040 * @since 5.4.2 041 */ 042public class ParamCollector { 043 044 public static final String Key = "collector"; 045 046 private static final String SKIP_SECTIONS_KEY = "nuxeo.wizard.skippedsections"; 047 048 private List<String> sectionsToSkip; 049 050 private ConfigurationGenerator configurationGenerator; 051 052 public ConfigurationGenerator getConfigurationGenerator() { 053 return configurationGenerator; 054 } 055 056 protected Map<String, String> configurationParams = new HashMap<>(); 057 058 protected Map<String, String> connectParams = new HashMap<>(); 059 060 public ParamCollector() { 061 configurationGenerator = new ConfigurationGenerator(); 062 configurationGenerator.init(); 063 String skipSections = configurationGenerator.getUserConfig().getProperty(SKIP_SECTIONS_KEY, ""); 064 sectionsToSkip = Arrays.asList(skipSections.split(",")); 065 } 066 067 public boolean isSectionSkipped(String section) { 068 return sectionsToSkip.contains(section); 069 } 070 071 public void addConfigurationParam(String name, String value) { 072 if (value == null) { 073 configurationParams.remove(name); 074 } else { 075 configurationParams.put(name, value); 076 } 077 } 078 079 public void addConnectParam(String name, String value) { 080 configurationParams.put(name, value); 081 } 082 083 public Map<String, String> getConfigurationParams() { 084 return configurationParams; 085 } 086 087 public Map<String, String> getChangedParameters() { 088 return configurationGenerator.getChangedParameters(configurationParams); 089 } 090 091 public String getConfigurationParam(String name) { 092 return getConfigurationParam(name, ""); 093 } 094 095 public String getConfigurationParam(String name, String defaultValue) { 096 String param = configurationParams.get(name); 097 if (param == null) { 098 param = configurationGenerator.getUserConfig().getProperty(name, defaultValue); 099 } 100 return param; 101 } 102 103 public String getConfigurationParamValue(String name) { 104 return configurationParams.get(name); 105 } 106 107 public Map<String, String> getConnectParams() { 108 return connectParams; 109 } 110 111 public void collectConfigurationParams(HttpServletRequest req) { 112 Enumeration<String> names = req.getParameterNames(); 113 while (names.hasMoreElements()) { 114 String name = names.nextElement(); 115 if (name.startsWith("org.nuxeo.") || name.startsWith("nuxeo.") || name.startsWith("mail.")) { 116 String value = req.getParameter(name); 117 if (!value.isEmpty() || (value.isEmpty() && configurationParams.containsKey(name))) { 118 addConfigurationParam(name, escapeHtml(value)); 119 } 120 } 121 } 122 } 123 124 /** 125 * @see ConfigurationGenerator#changeDBTemplate(String) 126 * @param templateName database template to use 127 */ 128 public void changeDBTemplate(String templateName) { 129 configurationGenerator.changeDBTemplate(templateName); 130 } 131 132 /** 133 * @since 8.1 134 */ 135 public void removeDbKeys() { 136 List<String> keys = new ArrayList<>(); 137 for (String key : configurationParams.keySet()) { 138 if (key.startsWith("nuxeo.db") || key.startsWith("nuxeo.mongodb")) { 139 keys.add(key); 140 } 141 } 142 for (String key : keys) { 143 configurationParams.remove(key); 144 } 145 } 146 147}