001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.vars; 014 015import java.util.*; 016 017import org.apache.juneau.cp.*; 018import org.apache.juneau.http.response.*; 019import org.apache.juneau.rest.*; 020import org.apache.juneau.svl.*; 021 022/** 023 * Localized string variable resolver. 024 * 025 * <p> 026 * The format for this var is <js>"$L{key[,args...]}"</js>. 027 * 028 * <p> 029 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 030 * 031 * <p> 032 * Values are pulled from the {@link RestRequest#getMessage(String,Object[])} method. 033 * These in turn are pulled from the resource bundle associated with the servlet class where the request was made. 034 * 035 * <p> 036 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved. 037 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 038 * 039 * <h5 class='section'>See Also:</h5><ul> 040 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.SvlVariables">SVL Variables</a> 041 * </ul> 042 */ 043public class LocalizationVar extends MultipartVar { 044 045 /** The name of this variable. */ 046 public static final String NAME = "L"; 047 048 /** 049 * Constructor. 050 */ 051 public LocalizationVar() { 052 super(NAME); 053 } 054 055 @Override /* Var */ 056 public String resolve(VarResolverSession session, String[] args) { 057 if (args.length > 0) { 058 String key = args[0]; 059 String[] a = (args.length > 1) ? Arrays.copyOfRange(args, 1, args.length) : new String[0]; 060 Messages messages = null; 061 if (session.getBean(RestRequest.class).isPresent()) 062 messages = session.getBean(RestRequest.class).get().getMessages(); 063 if (messages == null) 064 messages = session.getBean(Messages.class).orElseThrow(InternalServerError::new); 065 return messages.getString(key, (Object[])a); 066 } 067 return ""; 068 } 069 070 @Override /* Var */ 071 public boolean canResolve(VarResolverSession session) { 072 return session.getBean(Messages.class).isPresent() || session.getBean(RestRequest.class).isPresent(); 073 } 074}