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 org.apache.juneau.rest.*;
016import org.apache.juneau.rest.annotation.*;
017import org.apache.juneau.rest.widget.*;
018import org.apache.juneau.svl.*;
019
020/**
021 * HTML widget variable resolver.
022 *
023 * <p>
024 * The format for this var is <js>"$W{widgetName}"</js>.
025 *
026 * <p>
027 * Widgets are simple class that produce some sort of string based on a passed-in HTTP request.
028 *
029 * <p>
030 * They're registered via the following mechanisms:
031 * <ul>
032 *    <li>{@link HtmlDoc#widgets() @HtmlDoc(widgets)}
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5>
036 * <ul>
037 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
038 * </ul>
039 */
040public class WidgetVar extends SimpleVar {
041
042   /**
043    * The name of the session or context object that identifies the {@link RestRequest} object.
044    */
045   private static final String SESSION_req = "req";
046
047   /**
048    * The name of this variable.
049    */
050   public static final String NAME = "W";
051
052   /**
053    * Constructor.
054    */
055   public WidgetVar() {
056      super(NAME);
057   }
058
059   @Override /* Parameter */
060   public String resolve(VarResolverSession session, String key) throws Exception {
061      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
062      boolean isScript = false, isStyle = false;
063
064      if (key.endsWith(".script")) {
065         key = key.substring(0, key.length() - 7);
066         isScript = true;
067      }
068
069      if (key.endsWith(".style")) {
070         key = key.substring(0, key.length() - 6);
071         isStyle = true;
072      }
073
074      Widget w = req.getWidgets().get(key);
075      if (w == null)
076         return "unknown-widget-"+key;
077
078      if (isScript)
079         return w.getScript(req);
080      if (isStyle)
081         return w.getStyle(req);
082      return w.getHtml(req);
083   }
084}