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 018 * 019 */ 020 021package org.nuxeo.wizard.context; 022 023import java.io.File; 024import java.io.FileInputStream; 025import java.util.HashMap; 026import java.util.Map; 027import java.util.Properties; 028 029import javax.servlet.http.HttpServletRequest; 030 031import org.nuxeo.common.Environment; 032import org.nuxeo.launcher.config.ConfigurationGenerator; 033 034/** 035 * Simple Context management 036 * 037 * @author Tiry ([email protected]) 038 * @since 5.4.2 039 */ 040public class Context { 041 042 public static final String CONTEXT_ATTRIBUTE = "context"; 043 044 protected static ParamCollector collector; 045 046 protected static String baseUrl; 047 048 protected static Boolean browserInternetAccess; 049 050 protected Map<String, String> errors = new HashMap<>(); 051 052 protected Map<String, String> infos = new HashMap<>(); 053 054 protected static Map<String, String> connectMap; 055 056 protected static String distributionKey = null; 057 058 protected HttpServletRequest req; 059 060 protected Context(HttpServletRequest req) { 061 this.req = req; 062 } 063 064 public static Context instance(HttpServletRequest req) { 065 Context ctx = (Context) req.getAttribute(CONTEXT_ATTRIBUTE); 066 if (ctx == null) { 067 ctx = new Context(req); 068 req.setAttribute(CONTEXT_ATTRIBUTE, ctx); 069 } 070 return ctx; 071 } 072 073 public static void reset() { 074 collector = null; 075 connectMap = null; 076 } 077 078 public ParamCollector getCollector() { 079 if (collector == null) { 080 collector = new ParamCollector(); 081 } 082 return collector; 083 } 084 085 public String getDistributionKey() { 086 if (distributionKey == null) { 087 ConfigurationGenerator configurationGenerator = new ConfigurationGenerator(); 088 configurationGenerator.init(); 089 try { 090 Properties distribution = new Properties(); 091 distribution.load(new FileInputStream(new File(configurationGenerator.getConfigDir(), 092 "distribution.properties"))); 093 String name = distribution.getProperty(Environment.DISTRIBUTION_NAME, "unknown").toLowerCase(); 094 String server = distribution.getProperty(Environment.DISTRIBUTION_SERVER, "unknown").toLowerCase(); 095 String version = distribution.getProperty(Environment.DISTRIBUTION_VERSION, "unknown").toLowerCase(); 096 String pkg = distribution.getProperty(Environment.DISTRIBUTION_PACKAGE, "unknown").toLowerCase(); 097 distributionKey = name + "-" + server + "-" + version + "-" + pkg; 098 } catch (Exception e) { 099 distributionKey = "unknown"; 100 } 101 } 102 return distributionKey; 103 } 104 105 public void trackError(String fieldId, String message) { 106 errors.put(fieldId, message); 107 } 108 109 public boolean hasErrors() { 110 return errors.size() > 0; 111 } 112 113 public void trackInfo(String fieldId, String message) { 114 infos.put(fieldId, message); 115 } 116 117 public boolean hasInfos() { 118 return infos.size() > 0; 119 } 120 121 public Map<String, String> getErrorsMap() { 122 return errors; 123 } 124 125 public Map<String, String> getInfosMap() { 126 return infos; 127 } 128 129 public String getFieldsInErrorAsJson() { 130 StringBuffer sb = new StringBuffer("["); 131 for (String key : errors.keySet()) { 132 sb.append("'"); 133 sb.append(key); 134 sb.append("',"); 135 } 136 sb.append("END]"); 137 return sb.toString().replace(",END", ""); 138 } 139 140 public void storeConnectMap(Map<String, String> map) { 141 connectMap = map; 142 } 143 144 public boolean isConnectRegistrationDone() { 145 return connectMap != null && "true".equals(connectMap.get("registrationOK")); 146 } 147 148 public static Map<String, String> getConnectMap() { 149 return connectMap; 150 } 151 152 public void setBaseUrl(String base) { 153 baseUrl = base; 154 } 155 156 public String getBaseUrl() { 157 return baseUrl; 158 } 159 160 public boolean isBrowserInternetAccessChecked() { 161 if (browserInternetAccess == null) { 162 return false; 163 } else { 164 return true; 165 } 166 } 167 168 public boolean hasBrowserInternetAccess() { 169 if (browserInternetAccess == null) { 170 return false; 171 } 172 return browserInternetAccess; 173 } 174 175 public void setBrowserInternetAccess(boolean browserInternetAccess) { 176 Context.browserInternetAccess = browserInternetAccess; 177 } 178 179}