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