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.Function;
21
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.chain.Context;
24
25 /**
26 * Base {@link Command} implementation for retrieving the
27 * requested Locale from our {@link Context}, and storing it under the
28 * context attribute key returned by the {@code localeKey} property.
29 *
30 * @param <C> Type of the context associated with this command
31 *
32 * @author Stefan Graff
33 * @since Chain 1.3
34 */
35 public class GetLocaleCommand<C extends WebContext> implements Command<C> {
36
37 // ----------------------------------------------------- Instance Variables
38
39 /**
40 * Function to get the {@link Locale} from the {@code context}.
41 */
42 private final Function<C, Locale> localeFunction;
43
44 // ----------------------------------------------------------- Constructors
45
46 /**
47 * Construct a new instance to get the locale from the context.
48 *
49 * @param localeFunction Function to get the {@link Locale} from
50 * the {@code context}
51 */
52 public GetLocaleCommand(final Function<C, Locale> localeFunction) {
53 this.localeFunction = localeFunction;
54 }
55
56 // -------------------------------------------------------------- Properties
57
58 /**
59 * The context attribute key used to store the {@code Locale}.
60 */
61 private String localeKey = "locale";
62
63 /**
64 * Return the context attribute key under which we will store
65 * the request {@code Locale}.
66 *
67 * @return The context attribute key of the request {@code Locale}.
68 */
69 public String getLocaleKey() {
70 return this.localeKey;
71 }
72
73 /**
74 * Set the context attribute key under which we will store
75 * the request {@code Locale}.
76 *
77 * @param localeKey The new context attribute key
78 */
79 public void setLocaleKey(String localeKey) {
80 this.localeKey = localeKey;
81 }
82
83 // --------------------------------------------------------- Command Methods
84
85 /**
86 * Retrieve the {@code Locale} for this request, and store it
87 * under the specified context attribute.
88 *
89 * @param context The {@link Context} we are operating on
90 *
91 * @return {@code false} so that processing will continue
92 *
93 * @throws Exception If an error occurs during execution.
94 */
95 @Override
96 public boolean execute(C context) throws Exception {
97 context.put(getLocaleKey(), localeFunction.apply(context));
98 return false;
99 }
100 }