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