001/*
002 * (C) Copyright 2011 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 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.connect.client.ui;
020
021import static org.nuxeo.ecm.webengine.jaxrs.context.RequestContext.getActiveContext;
022
023import java.io.Serializable;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.concurrent.ConcurrentHashMap;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpSession;
030
031/**
032 * Provide contextual access to the {@link ListingFilterSetting} for each listing. Use HttpSession to store a map of
033 * {@link ListingFilterSetting} This class is used to share state between the WebEngine and the JSF parts
034 *
035 * @author Tiry (tdelprat@nuxeo.com)
036 */
037public class SharedPackageListingsSettings implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    /**
042     * @since 8.10
043     */
044    private static final Map<String, RequestResolver> resolvers;
045
046    protected Map<String, ListingFilterSetting> settings = new HashMap<String, ListingFilterSetting>();
047
048    public static final String SESSION_KEY = "org.nuxeo.connect.client.ui.PackageListingSettings";
049
050    static {
051        resolvers = new ConcurrentHashMap<>();
052        addRequestResolver(new RequestResolver() {
053            @Override
054            public boolean isActive() {
055                return getActiveContext() != null;
056            }
057
058            @Override
059            public HttpServletRequest resolve() {
060                return getActiveContext().getRequest();
061            }
062
063            @Override
064            public String getId() {
065                return "webengine";
066            }
067        });
068    }
069
070    public static void addRequestResolver(RequestResolver resolver) {
071        resolvers.put(resolver.getId(), resolver);
072    }
073
074    public static void removeRequestResolver(String id) {
075        resolvers.remove(id);
076    }
077
078    public ListingFilterSetting get(String listName) {
079        if (!settings.containsKey(listName)) {
080            settings.put(listName, new ListingFilterSetting());
081        }
082        return settings.get(listName);
083    }
084
085    public static SharedPackageListingsSettings instance() {
086        return resolvers.values()
087                        .stream()
088                        .filter(RequestResolver::isActive)
089                        .findFirst()
090                        .map(RequestResolver::resolve)
091                        .map(SharedPackageListingsSettings::instance)
092                        .orElse(null);
093    }
094
095    public static SharedPackageListingsSettings instance(HttpServletRequest request) {
096        return instance(request.getSession(true));
097    }
098
099    public static SharedPackageListingsSettings instance(HttpSession session) {
100        Object val = session.getAttribute(SESSION_KEY);
101        if (val == null || !(val instanceof SharedPackageListingsSettings)) {
102            val = new SharedPackageListingsSettings();
103            session.setAttribute(SESSION_KEY, val);
104        }
105        return (SharedPackageListingsSettings) val;
106    }
107
108    /**
109     * @since 8.10
110     */
111    public interface RequestResolver {
112        boolean isActive();
113
114        HttpServletRequest resolve();
115
116        String getId();
117    }
118}