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