001/* 002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * tdelprat 016 * 017 */ 018 019package org.nuxeo.wizard.context; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Properties; 026 027import javax.servlet.http.HttpServletRequest; 028 029import org.nuxeo.common.Environment; 030import org.nuxeo.launcher.config.ConfigurationGenerator; 031 032/** 033 * Simple Context management 034 * 035 * @author Tiry (tdelprat@nuxeo.com) 036 * @since 5.4.2 037 */ 038public class Context { 039 040 public static final String CONTEXT_ATTRIBUTE = "context"; 041 042 protected static ParamCollector collector; 043 044 protected static String baseUrl; 045 046 protected static Boolean browserInternetAccess; 047 048 protected Map<String, String> errors = new HashMap<>(); 049 050 protected Map<String, String> infos = new HashMap<>(); 051 052 protected static Map<String, String> connectMap; 053 054 protected static String distributionKey = null; 055 056 protected HttpServletRequest req; 057 058 protected Context(HttpServletRequest req) { 059 this.req = req; 060 } 061 062 public static Context instance(HttpServletRequest req) { 063 Context ctx = (Context) req.getAttribute(CONTEXT_ATTRIBUTE); 064 if (ctx == null) { 065 ctx = new Context(req); 066 req.setAttribute(CONTEXT_ATTRIBUTE, ctx); 067 } 068 return ctx; 069 } 070 071 public static void reset() { 072 collector = null; 073 connectMap = null; 074 } 075 076 public ParamCollector getCollector() { 077 if (collector == null) { 078 collector = new ParamCollector(); 079 } 080 return collector; 081 } 082 083 public String getDistributionKey() { 084 if (distributionKey == null) { 085 ConfigurationGenerator configurationGenerator = new ConfigurationGenerator(); 086 configurationGenerator.init(); 087 try { 088 Properties distribution = new Properties(); 089 distribution.load(new FileInputStream(new File(configurationGenerator.getConfigDir(), 090 "distribution.properties"))); 091 String name = distribution.getProperty(Environment.DISTRIBUTION_NAME, "unknown").toLowerCase(); 092 String server = distribution.getProperty(Environment.DISTRIBUTION_SERVER, "unknown").toLowerCase(); 093 String version = distribution.getProperty(Environment.DISTRIBUTION_VERSION, "unknown").toLowerCase(); 094 String pkg = distribution.getProperty(Environment.DISTRIBUTION_PACKAGE, "unknown").toLowerCase(); 095 distributionKey = name + "-" + server + "-" + version + "-" + pkg; 096 } catch (Exception e) { 097 distributionKey = "unknown"; 098 } 099 } 100 return distributionKey; 101 } 102 103 public void trackError(String fieldId, String message) { 104 errors.put(fieldId, message); 105 } 106 107 public boolean hasErrors() { 108 return errors.size() > 0; 109 } 110 111 public void trackInfo(String fieldId, String message) { 112 infos.put(fieldId, message); 113 } 114 115 public boolean hasInfos() { 116 return infos.size() > 0; 117 } 118 119 public Map<String, String> getErrorsMap() { 120 return errors; 121 } 122 123 public Map<String, String> getInfosMap() { 124 return infos; 125 } 126 127 public String getFieldsInErrorAsJson() { 128 StringBuffer sb = new StringBuffer("["); 129 for (String key : errors.keySet()) { 130 sb.append("'"); 131 sb.append(key); 132 sb.append("',"); 133 } 134 sb.append("END]"); 135 return sb.toString().replace(",END", ""); 136 } 137 138 public void storeConnectMap(Map<String, String> map) { 139 connectMap = map; 140 } 141 142 public boolean isConnectRegistrationDone() { 143 return connectMap != null && "true".equals(connectMap.get("registrationOK")); 144 } 145 146 public static Map<String, String> getConnectMap() { 147 return connectMap; 148 } 149 150 public void setBaseUrl(String base) { 151 baseUrl = base; 152 } 153 154 public String getBaseUrl() { 155 return baseUrl; 156 } 157 158 public boolean isBrowserInternetAccessChecked() { 159 if (browserInternetAccess == null) { 160 return false; 161 } else { 162 return true; 163 } 164 } 165 166 public boolean hasBrowserInternetAccess() { 167 if (browserInternetAccess == null) { 168 return false; 169 } 170 return browserInternetAccess; 171 } 172 173 public void setBrowserInternetAccess(boolean browserInternetAccess) { 174 Context.browserInternetAccess = browserInternetAccess; 175 } 176 177}