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.console.views;
018
019import org.apache.juneau.commons.svl.*;
020import org.apache.juneau.console.widgets.*;
021
022/**
023 * The <js>"$FV{name}"</js> / <js>"$FV{name,default}"</js> server-values variable.
024 *
025 * <p>
026 * Resolves author-declared scalar values from a {@link ServerValuesRegistry} placed in the per-render session by
027 * the widget emitter.  It is a {@link DefaultingVar}: <js>"$FV{name}"</js> resolves the named provider and
028 * <js>"$FV{name,default}"</js> falls back to {@code default} when the provider is missing or returns
029 * <jk>null</jk>.
030 *
031 * <p>
032 * <b>Not {@code $W}.</b>  This var emits declared scalar values as plain text (the caller places the returned
033 * string into html5 beans and the serializer entity-encodes it).  It is distinct from {@code $W}
034 * ({@code org.apache.juneau.html.HtmlWidgetVar}), which emits <b>raw</b> widget HTML.  {@code $FV} never injects
035 * HTML.
036 *
037 * <p>
038 * {@link #allowRecurse()} is <jk>false</jk>: a provider's returned string is emitted literally and is never
039 * re-parsed as SVL (untrusted computed data must not be recursively resolved).  When no registry is present in
040 * the session the var is not resolvable and the <js>"$FV{...}"</js> token is left literal (fail-open).
041 *
042 * @since 10.0.0
043 */
044public class ServerValuesVar extends DefaultingVar {
045
046   /** The name of this variable. */
047   public static final String NAME = "FV";
048
049   /**
050    * Constructor.
051    */
052   public ServerValuesVar() {
053      super(NAME);
054   }
055
056   @Override /* Overridden from Var */
057   protected boolean allowRecurse() {
058      return false;
059   }
060
061   @Override /* Overridden from Var */
062   protected boolean canResolve(VarResolverSession session) {
063      return session.getBean(ServerValuesRegistry.class).isPresent();
064   }
065
066   @Override /* Overridden from Var */
067   public String resolve(VarResolverSession session, String key) {
068      var reg = session.getBean(ServerValuesRegistry.class).orElse(null);
069      if (reg == null)
070         return null;
071      return reg.resolve(session, key);
072   }
073}