GetLocaleCommand.java

  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.jakarta;

  18. import java.util.Locale;
  19. import java.util.function.Function;

  20. import org.apache.commons.chain.Command;
  21. import org.apache.commons.chain.Context;

  22. /**
  23.  * Base {@link Command} implementation for retrieving the
  24.  * requested Locale from our {@link Context}, and storing it under the
  25.  * context attribute key returned by the {@code localeKey} property.
  26.  *
  27.  * @param <C> Type of the context associated with this command
  28.  *
  29.  * @author Stefan Graff
  30.  * @since Chain 1.3
  31.  */
  32. public class GetLocaleCommand<C extends WebContext> implements Command<C> {

  33.     // ----------------------------------------------------- Instance Variables

  34.     /**
  35.      * Function to get the {@link Locale} from the {@code context}.
  36.      */
  37.     private final Function<C, Locale> localeFunction;

  38.     // ----------------------------------------------------------- Constructors

  39.     /**
  40.      * Construct a new instance to get the locale from the context.
  41.      *
  42.      * @param localeFunction Function to get the {@link Locale} from
  43.      *                       the {@code context}
  44.      */
  45.     public GetLocaleCommand(final Function<C, Locale> localeFunction) {
  46.         this.localeFunction = localeFunction;
  47.     }

  48.     // -------------------------------------------------------------- Properties

  49.     /**
  50.      * The context attribute key used to store the {@code Locale}.
  51.      */
  52.     private String localeKey = "locale";

  53.     /**
  54.      * Return the context attribute key under which we will store
  55.      * the request {@code Locale}.
  56.      *
  57.      * @return The context attribute key of the request {@code Locale}.
  58.      */
  59.     public String getLocaleKey() {
  60.         return this.localeKey;
  61.     }

  62.     /**
  63.      * Set the context attribute key under which we will store
  64.      * the request {@code Locale}.
  65.      *
  66.      * @param localeKey The new context attribute key
  67.      */
  68.     public void setLocaleKey(String localeKey) {
  69.         this.localeKey = localeKey;
  70.     }

  71.     // --------------------------------------------------------- Command Methods

  72.     /**
  73.      * Retrieve the {@code Locale} for this request, and store it
  74.      * under the specified context attribute.
  75.      *
  76.      * @param context The {@link Context} we are operating on
  77.      *
  78.      * @return {@code false} so that processing will continue
  79.      *
  80.      * @throws Exception If an error occurs during execution.
  81.      */
  82.     @Override
  83.     public boolean execute(C context) throws Exception {
  84.         context.put(getLocaleKey(), localeFunction.apply(context));
  85.         return false;
  86.     }
  87. }