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.html.*; 018import org.apache.juneau.html.annotation.*; 019import org.apache.juneau.rest.*; 020import org.apache.juneau.rest.widget.*; 021import org.apache.juneau.svl.*; 022 023/** 024 * HTML widget variable resolver. 025 * 026 * <p> 027 * The format for this var is <js>"$W{widgetName}"</js>. 028 * 029 * <p> 030 * Widgets are simple class that produce some sort of string based on a passed-in HTTP request. 031 * 032 * <p> 033 * They're registered via the following mechanisms: 034 * <ul> 035 * <li>{@link HtmlDocConfig#widgets() @HtmlDocConfig(widgets)} 036 * </ul> 037 * 038 * <ul class='seealso'> 039 * <li class='link'>{@doc juneau-rest-server.SvlVariables} 040 * </ul> 041 * 042 * @deprecated Use {@link HtmlWidgetVar} 043 */ 044@Deprecated 045public class WidgetVar extends SimpleVar { 046 047 private static final String SESSION_req = "req"; 048 private static final String SESSION_res = "res"; 049 050 /** 051 * The name of this variable. 052 */ 053 public static final String NAME = "W"; 054 055 /** 056 * Constructor. 057 */ 058 public WidgetVar() { 059 super(NAME); 060 } 061 062 @Override /* Var */ 063 public String resolve(VarResolverSession session, String key) throws Exception { 064 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true); 065 RestResponse res = session.getSessionObject(RestResponse.class, SESSION_res, true); 066 067 boolean isScript = false, isStyle = false; 068 069 if (key.endsWith(".script")) { 070 key = key.substring(0, key.length() - 7); 071 isScript = true; 072 } 073 074 if (key.endsWith(".style")) { 075 key = key.substring(0, key.length() - 6); 076 isStyle = true; 077 } 078 079 HtmlWidget w = req.getWidgets().get(key); 080 081 if (w == null) { 082 Map<String,Widget> widgetMap = session.getSessionObject(Map.class, "htmlWidgets", false); 083 if (widgetMap != null) 084 w = widgetMap.get(key); 085 } 086 087 if (w == null) 088 return "unknown-widget-"+key; 089 090 if (w instanceof Widget) { 091 Widget w2 = (Widget)w; 092 if (isScript) 093 return w2.getScript(req, res); 094 if (isStyle) 095 return w2.getStyle(req, res); 096 return w2.getHtml(req, res); 097 } 098 099 return w.getHtml(session); 100 } 101 102 @Override /* Var */ 103 public boolean canResolve(VarResolverSession session) { 104 return session.hasSessionObject(SESSION_req) && session.hasSessionObject(SESSION_res); 105 } 106}