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.juneau.rest.vars;
018
019import java.util.*;
020
021import org.apache.juneau.cp.*;
022import org.apache.juneau.http.response.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.svl.*;
025
026/**
027 * Localized string variable resolver.
028 *
029 * <p>
030 * The format for this var is <js>"$L{key[,args...]}"</js>.
031 *
032 * <p>
033 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
034 *
035 * <p>
036 * Values are pulled from the {@link RestRequest#getMessage(String,Object[])} method.
037 * These in turn are pulled from the resource bundle associated with the servlet class where the request was made.
038 *
039 * <p>
040 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
041 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
042 *
043 * <h5 class='section'>See Also:</h5><ul>
044 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a>
045 * </ul>
046 */
047public class LocalizationVar extends MultipartVar {
048
049   /** The name of this variable. */
050   public static final String NAME = "L";
051
052   /**
053    * Constructor.
054    */
055   public LocalizationVar() {
056      super(NAME);
057   }
058
059   @Override /* Var */
060   public String resolve(VarResolverSession session, String[] args) {
061      if (args.length > 0) {
062         String key = args[0];
063         String[] a = (args.length > 1) ? Arrays.copyOfRange(args, 1, args.length) : new String[0];
064         Messages messages = null;
065         if (session.getBean(RestRequest.class).isPresent())
066            messages = session.getBean(RestRequest.class).get().getMessages();
067         if (messages == null)
068            messages = session.getBean(Messages.class).orElseThrow(InternalServerError::new);
069         return messages.getString(key, (Object[])a);
070      }
071      return "";
072   }
073
074   @Override /* Var */
075   public boolean canResolve(VarResolverSession session) {
076      return session.getBean(Messages.class).isPresent() || session.getBean(RestRequest.class).isPresent();
077   }
078}