001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.negotiation;
023
024import java.util.List;
025
026import javax.servlet.http.HttpServletRequest;
027
028import org.nuxeo.theme.Manager;
029import org.nuxeo.theme.types.TypeFamily;
030
031public abstract class AbstractNegotiator implements Negotiator {
032
033    private static final String SPEC_PREFIX = "nxtheme://theme";
034
035    // FIXME: can be called 'web' under webengine
036    private static final String DEFAULT_NEGOTIATION_STRATEGY = "default";
037
038    protected final String strategy;
039
040    protected final Object context;
041
042    protected final HttpServletRequest request;
043
044    public abstract String getTemplateEngineName();
045
046    protected AbstractNegotiator(String strategy, Object context, HttpServletRequest request) {
047        this.strategy = strategy;
048        this.context = context;
049        this.request = request;
050    }
051
052    public final String getSpec() throws NegotiationException {
053        return String.format("%s/%s/%s/%s/%s/%s/%s", SPEC_PREFIX, negotiate("engine"), negotiate("mode"),
054                getTemplateEngineName(), negotiate("theme"), negotiate("perspective"), negotiate("collection"));
055    }
056
057    public final synchronized String negotiate(String object) throws NegotiationException {
058        if (strategy == null) {
059            throw new NegotiationException("No negotiation strategy is set.");
060        }
061        NegotiationType negotiation = (NegotiationType) Manager.getTypeRegistry().lookup(TypeFamily.NEGOTIATION,
062                String.format("%s/%s", strategy, object));
063        // Try with the 'default' strategy
064        if (negotiation == null) {
065            negotiation = (NegotiationType) Manager.getTypeRegistry().lookup(TypeFamily.NEGOTIATION,
066                    String.format("%s/%s", DEFAULT_NEGOTIATION_STRATEGY, object));
067        }
068        if (negotiation == null) {
069            throw new NegotiationException("Could not obtain negotiation for: " + strategy + " (strategy) " + object
070                    + " (object)");
071        }
072        final List<Scheme> schemes = negotiation.getSchemes();
073
074        String outcome = null;
075        if (schemes != null) {
076            for (Scheme scheme : negotiation.getSchemes()) {
077                outcome = scheme.getOutcome(context);
078                if (outcome != null) {
079                    break;
080                }
081            }
082        }
083        if (outcome == null) {
084            throw new NegotiationException("No negotiation outcome found for:  " + strategy + " (strategy) " + object
085                    + " (object)");
086        } else {
087            // add result to the request
088            request.setAttribute(NEGOTIATION_RESULT_PREFIX + object, outcome);
089        }
090        return outcome;
091    }
092}