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.Function; 021 022import org.apache.commons.chain.Command; 023import org.apache.commons.chain.Context; 024 025/** 026 * Base {@link Command} implementation for retrieving the 027 * requested Locale from our {@link Context}, and storing it under the 028 * context attribute key returned by the {@code localeKey} property. 029 * 030 * @param <C> Type of the context associated with this command 031 * 032 * @author Stefan Graff 033 * @since Chain 1.3 034 */ 035public class GetLocaleCommand<C extends WebContext> implements Command<C> { 036 037 // ----------------------------------------------------- Instance Variables 038 039 /** 040 * Function to get the {@link Locale} from the {@code context}. 041 */ 042 private final Function<C, Locale> localeFunction; 043 044 // ----------------------------------------------------------- Constructors 045 046 /** 047 * Construct a new instance to get the locale from the context. 048 * 049 * @param localeFunction Function to get the {@link Locale} from 050 * the {@code context} 051 */ 052 public GetLocaleCommand(final Function<C, Locale> localeFunction) { 053 this.localeFunction = localeFunction; 054 } 055 056 // -------------------------------------------------------------- Properties 057 058 /** 059 * The context attribute key used to store the {@code Locale}. 060 */ 061 private String localeKey = "locale"; 062 063 /** 064 * Return the context attribute key under which we will store 065 * the request {@code Locale}. 066 * 067 * @return The context attribute key of the request {@code Locale}. 068 */ 069 public String getLocaleKey() { 070 return this.localeKey; 071 } 072 073 /** 074 * Set the context attribute key under which we will store 075 * the request {@code Locale}. 076 * 077 * @param localeKey The new context attribute key 078 */ 079 public void setLocaleKey(String localeKey) { 080 this.localeKey = localeKey; 081 } 082 083 // --------------------------------------------------------- Command Methods 084 085 /** 086 * Retrieve the {@code Locale} for this request, and store it 087 * under the specified context attribute. 088 * 089 * @param context The {@link Context} we are operating on 090 * 091 * @return {@code false} so that processing will continue 092 * 093 * @throws Exception If an error occurs during execution. 094 */ 095 @Override 096 public boolean execute(C context) throws Exception { 097 context.put(getLocaleKey(), localeFunction.apply(context)); 098 return false; 099 } 100}