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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.jsf.negotiators;
020
021import javax.faces.context.FacesContext;
022
023import org.nuxeo.runtime.api.Framework;
024import org.nuxeo.theme.styling.negotiation.AbstractNegotiator;
025import org.nuxeo.theme.styling.service.ThemeStylingService;
026import org.nuxeo.theme.styling.service.descriptors.FlavorDescriptor;
027
028/**
029 * Negotiator that returns the default flavor configured for negotiated theme page.
030 *
031 * @see ThemeStylingService
032 * @see FlavorDescriptor
033 * @since 5.5
034 */
035public class DefaultPageFlavor extends AbstractNegotiator {
036
037    @Override
038    public String getResult(String target, Object context) {
039        FacesContext faces = null;
040        if (context instanceof FacesContext) {
041            faces = (FacesContext) context;
042        } else {
043            return null;
044        }
045        String theme = (String) faces.getExternalContext().getRequestMap().get(getProperty("negotiatedPageVariable"));
046        if (theme != null) {
047            ThemeStylingService service = Framework.getService(ThemeStylingService.class);
048            return service.getDefaultFlavorName(theme);
049        }
050        return null;
051    }
052}