001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.chain.web.jakarta;
018
019import java.util.Locale;
020import java.util.function.BiConsumer;
021
022import org.apache.commons.chain.Command;
023import org.apache.commons.chain.Context;
024
025/**
026 * Base {@link Command} implementation for setting the
027 * response locale for this response to the {@code Locale} stored
028 * under the context attribute key returned by the {@code localeKey}
029 * property.
030 *
031 * @param <C> Type of the context associated with this command
032 *
033 * @author Stefan Graff
034 * @since Chain 1.3
035 */
036public abstract class SetLocaleCommand<C extends WebContext> implements Command<C> {
037
038    // ----------------------------------------------------- Instance Variables
039
040    /**
041     * BiConsumer to set the {@link Locale} into the {@code context}.
042     */
043    private final BiConsumer<C, Locale> localeBiConsumer;
044
045    // ----------------------------------------------------------- Constructors
046
047    /**
048     * Construct a new instance to set the locale into the context.
049     *
050     * @param localeBiConsumer BiConsumer to set the {@link Locale} into
051     *                         the {@code context}
052     */
053    public SetLocaleCommand(final BiConsumer<C, Locale> localeBiConsumer) {
054        this.localeBiConsumer = localeBiConsumer;
055    }
056
057    // -------------------------------------------------------------- Properties
058
059    /**
060     * The context attribute key used to retrieve the {@code Locale}.
061     */
062    private String localeKey = "locale";
063
064    /**
065     * Return the context attribute key under which we will retrieve
066     * the response {@code Locale}.
067     *
068     * @return The context attribute key of the request {@code Locale}.
069     */
070    public String getLocaleKey() {
071        return this.localeKey;
072    }
073
074    /**
075     * Set the context attribute key under which we will retrieve
076     * the response {@code Locale}.
077     *
078     * @param localeKey The new context attribute key
079     */
080    public void setLocaleKey(String localeKey) {
081        this.localeKey = localeKey;
082    }
083
084    // --------------------------------------------------------- Command Methods
085
086    /**
087     * Retrieve the {@code Locale} stored under the specified
088     * context attribute key, and establish it on this response.
089     *
090     * @param context The {@link Context} we are operating on
091     *
092     * @return {@code false} so that processing will continue
093     *
094     * @throws Exception If an error occurs during execution.
095     */
096    @Override
097    public boolean execute(C context) throws Exception {
098        localeBiConsumer.accept(context, (Locale) context.get(getLocaleKey()));
099        return false;
100    }
101}