001/* 002 * (C) Copyright 2011 Nuxeo SAS (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.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 * Nuxeo - initial API and implementation 016 */ 017package org.nuxeo.connect.client.ui; 018 019import java.io.Serializable; 020import java.util.HashMap; 021import java.util.Map; 022 023import javax.faces.context.FacesContext; 024import javax.servlet.http.HttpServletRequest; 025import javax.servlet.http.HttpSession; 026 027import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 028 029/** 030 * Provide contextual access to the {@link ListingFilterSetting} for each listing. Use HttpSession to store a map of 031 * {@link ListingFilterSetting} This class is used to share state between the WebEngine and the JSF parts 032 * 033 * @author Tiry (tdelprat@nuxeo.com) 034 */ 035public class SharedPackageListingsSettings implements Serializable { 036 037 private static final long serialVersionUID = 1L; 038 039 protected Map<String, ListingFilterSetting> settings = new HashMap<String, ListingFilterSetting>(); 040 041 public static final String SESSION_KEY = "org.nuxeo.connect.client.ui.PackageListingSettings"; 042 043 public ListingFilterSetting get(String listName) { 044 if (!settings.containsKey(listName)) { 045 settings.put(listName, new ListingFilterSetting()); 046 } 047 return settings.get(listName); 048 } 049 050 public static SharedPackageListingsSettings instance() { 051 052 HttpServletRequest request = null; 053 if (FacesContext.getCurrentInstance() != null) { 054 request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 055 } else if (RequestContext.getActiveContext() != null) { 056 request = RequestContext.getActiveContext().getRequest(); 057 } 058 059 if (request != null) { 060 return instance(request); 061 } 062 return null; 063 } 064 065 public static SharedPackageListingsSettings instance(HttpServletRequest request) { 066 return instance(request.getSession(true)); 067 } 068 069 public static SharedPackageListingsSettings instance(HttpSession session) { 070 Object val = session.getAttribute(SESSION_KEY); 071 if (val == null || !(val instanceof SharedPackageListingsSettings)) { 072 val = new SharedPackageListingsSettings(); 073 session.setAttribute(SESSION_KEY, val); 074 } 075 return (SharedPackageListingsSettings) val; 076 } 077 078}