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 * <h5 class='section'>See Also:</h5>
039 * <ul>
040 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
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 /* Parameter */
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         return session.getSessionObject(RestRequest.class, RequestVar.SESSION_req, true).getMessage(key, (Object[])a);
061      }
062      return "";
063   }
064}