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