1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.chain.web.javax;
18
19 import java.util.Locale;
20 import java.util.function.BiConsumer;
21
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.chain.Context;
24
25 /**
26 * Base {@link Command} implementation for setting the
27 * response locale for this response to the {@code Locale} stored
28 * under the context attribute key returned by the {@code localeKey}
29 * property.
30 *
31 * @param <C> Type of the context associated with this command
32 *
33 * @author Stefan Graff
34 * @since Chain 1.3
35 */
36 public abstract class SetLocaleCommand<C extends WebContext> implements Command<C> {
37
38 // ----------------------------------------------------- Instance Variables
39
40 /**
41 * BiConsumer to set the {@link Locale} into the {@code context}.
42 */
43 private final BiConsumer<C, Locale> localeBiConsumer;
44
45 // ----------------------------------------------------------- Constructors
46
47 /**
48 * Construct a new instance to set the locale into the context.
49 *
50 * @param localeBiConsumer BiConsumer to set the {@link Locale} into
51 * the {@code context}
52 */
53 public SetLocaleCommand(final BiConsumer<C, Locale> localeBiConsumer) {
54 this.localeBiConsumer = localeBiConsumer;
55 }
56
57 // -------------------------------------------------------------- Properties
58
59 /**
60 * The context attribute key used to retrieve the {@code Locale}.
61 */
62 private String localeKey = "locale";
63
64 /**
65 * Return the context attribute key under which we will retrieve
66 * the response {@code Locale}.
67 *
68 * @return The context attribute key of the request {@code Locale}.
69 */
70 public String getLocaleKey() {
71 return this.localeKey;
72 }
73
74 /**
75 * Set the context attribute key under which we will retrieve
76 * the response {@code Locale}.
77 *
78 * @param localeKey The new context attribute key
79 */
80 public void setLocaleKey(String localeKey) {
81 this.localeKey = localeKey;
82 }
83
84 // --------------------------------------------------------- Command Methods
85
86 /**
87 * Retrieve the {@code Locale} stored under the specified
88 * context attribute key, and establish it on this response.
89 *
90 * @param context The {@link Context} we are operating on
91 *
92 * @return {@code false} so that processing will continue
93 *
94 * @throws Exception If an error occurs during execution.
95 */
96 @Override
97 public boolean execute(C context) throws Exception {
98 localeBiConsumer.accept(context, (Locale) context.get(getLocaleKey()));
99 return false;
100 }
101 }