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.rest.*; 018import org.apache.juneau.svl.*; 019 020/** 021 * Localized string variable resolver. 022 * 023 * <p> 024 * The format for this var is <js>"$L{key[,args...]}"</js>. 025 * 026 * <p> 027 * This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver or a 028 * session object on the resolver session. 029 * 030 * <p> 031 * Values are pulled from the {@link RestRequest#getMessage(String,Object[])} method. 032 * These in turn are pulled from the resource bundle associated with the servlet class where the request was made. 033 * 034 * <p> 035 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved. 036 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 037 * 038 * <ul class='seealso'> 039 * <li class='link'>{@doc juneau-rest-server.SvlVariables} 040 * </ul> 041 */ 042public class LocalizationVar extends MultipartVar { 043 044 private static final String SESSION_req = "req"; 045 046 /** The name of this variable. */ 047 public static final String NAME = "L"; 048 049 /** 050 * Constructor. 051 */ 052 public LocalizationVar() { 053 super(NAME); 054 } 055 056 @Override /* Var */ 057 public String resolve(VarResolverSession session, String[] args) { 058 if (args.length > 0) { 059 String key = args[0]; 060 String[] a = (args.length > 1) ? Arrays.copyOfRange(args, 1, args.length) : new String[0]; 061 return session.getSessionObject(RestRequest.class, SESSION_req, true).getMessage(key, (Object[])a); 062 } 063 return ""; 064 } 065 066 @Override /* Var */ 067 public boolean canResolve(VarResolverSession session) { 068 return session.hasSessionObject(SESSION_req); 069 } 070}