1 19 20 package com.sslexplorer.input.tags; 21 22 import java.util.Collection ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.PageContext ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.taglib.TagUtils; 31 import org.apache.struts.taglib.html.BaseFieldTag; 32 import org.apache.struts.util.MessageResources; 33 34 import com.sslexplorer.boot.PropertyClass; 35 import com.sslexplorer.boot.PropertyClassManager; 36 import com.sslexplorer.boot.PropertyDefinition; 37 import com.sslexplorer.core.CoreUtil; 38 import com.sslexplorer.properties.attributes.AttributeDefinition; 39 import com.sslexplorer.properties.attributes.AttributesPropertyClass; 40 41 47 public class VariablesTag extends BaseFieldTag { 48 49 final static Log log = LogFactory.getLog(VariablesTag.class); 50 51 53 protected String variables; 54 protected String fragment; 55 protected String inputId; 56 protected boolean includeUserAttributes; 57 protected boolean includeRequestAttributes; 58 protected boolean includeSession; 59 protected String titleKey; 60 protected boolean disabled; 61 62 65 protected static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.taglib.bean.LocalStrings"); 66 67 70 public VariablesTag() { 71 includeUserAttributes = true; 72 includeSession = true; 73 includeRequestAttributes = false; 74 disabled = false; 75 } 76 77 82 public void setTitleKey(String titleKey) { 83 this.titleKey = titleKey; 84 } 85 86 91 public void setInputId(String inputId) { 92 this.inputId = inputId; 93 } 94 95 100 public void setVariables(String variables) { 101 this.variables = variables; 102 } 103 104 111 public void setIncludeUserAttributes(boolean includeUserAttributes) { 112 this.includeUserAttributes = includeUserAttributes; 113 } 114 115 121 public void setIncludeSession(boolean includeSession) { 122 this.includeSession = includeSession; 123 } 124 125 131 public void setIncludeRequest(boolean includeRequest) { 132 this.includeRequestAttributes = includeRequest; 133 } 134 139 public int doEndTag() throws JspException { 140 if(fragment != null) { 141 TagUtils.getInstance().write(pageContext, fragment); 142 } 143 return (EVAL_PAGE); 144 } 145 146 151 public void release() { 152 super.release(); 153 fragment = null; 154 variables = null; 155 inputId = null; 156 includeUserAttributes = true; 157 includeSession = true; 158 includeRequestAttributes = false; 159 disabled = false; 160 } 161 162 167 public int doStartTag() throws JspException { 168 if (disabled) 169 return SKIP_PAGE; 170 171 if(getBundle() == null || getBundle().equals("")) { 172 setBundle("navigation"); 173 } 174 fragment = null; 175 if (inputId != null) { 176 fragment = generateReplacementVariableChooserFragment(titleKey, pageContext, getBundle(), getLocale(), inputId, variables, includeSession, includeUserAttributes, includeRequestAttributes); 177 } else { 178 log.warn("Both variables and inputId attributes must be specified for variables tag"); 179 return SKIP_PAGE; 180 } 181 return (EVAL_BODY_AGAIN); 182 } 183 184 public static String generateReplacementVariableChooserFragment(String titleKey, PageContext pageContext, String bundle, String locale, String inputId, String variables, boolean includeSession, boolean includeUserAttributes, boolean includeRequest) throws JspException { 185 StringBuffer buf = new StringBuffer (); 186 187 String title = null; 188 if (titleKey != null) { 189 title = TagUtils.getInstance().message(pageContext, bundle, locale, titleKey, new String [] {}); 190 } 191 if (title == null) { 192 title = TagUtils.getInstance().message(pageContext, bundle, locale, 193 "replacementVariablesChooser.title", new String [] {}); 194 if(title == null) { 195 title = "Replacement Variables"; 196 } 197 } 198 buf.append("<div class=\"component_replacementVariablesToggle\">"); 199 200 207 buf.append("<img onclick=\"togglePopupBelowLeft(document.getElementById('replacementVariablesChooser"); 208 buf.append(inputId); 209 buf.append("'), document.getElementById('"); 210 buf.append(inputId); 211 buf.append("'))\" "); 212 buf.append("src=\""); 213 buf.append(CoreUtil.getThemePath(pageContext.getSession())); 214 buf.append("/images/variables.gif\"/>"); 215 216 buf.append("</div>"); 217 buf.append("<div id=\"replacementVariablesChooser"); 218 buf.append(inputId); 219 buf.append("\" style=\"position:absolute;display: none;overflow:visible;top:4px;left:4px;z-index:900;\">"); 220 buf.append("<div class=\"replacementVariablesMain\">"); 221 buf.append("<div class=\"replacementVariablesTitleBar\">"); 222 buf.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); 223 buf.append("<tr><td class=\"title\">"); 224 buf.append(title); 225 buf.append("</td><td class=\"close\"><img SRC=\""); 226 buf.append(CoreUtil.getThemePath(pageContext.getSession())); 227 buf.append("/images/actions/erase.gif\" "); 228 buf.append("onclick=\"document.getElementById('"); 229 buf.append(inputId); 230 buf.append("').value = ''\"/><img SRC=\""); 231 buf.append(CoreUtil.getThemePath(pageContext.getSession())); 232 buf.append("/images/actions/closeReplacementVariables.gif\" "); 233 buf.append("onclick=\"togglePopup(document.getElementById('replacementVariablesChooser"); 234 buf.append(inputId); 235 buf.append("'))\"/>"); 236 buf.append("</td></tr></table></div><div class=\"replacementVariablesContent\">"); 237 buf.append("<ul>"); 238 if(variables != null) { 239 StringTokenizer t = new StringTokenizer (variables, ","); 240 while (t.hasMoreTokens()) { 241 String n = t.nextToken(); 242 addVariable(n, n, buf, pageContext, bundle, locale, inputId); 243 } 244 } 245 if(includeSession) { 246 addVariable("session:username", null, buf, pageContext, bundle, locale, inputId); 247 addVariable("session:password", null, buf, pageContext, bundle, locale, inputId); 248 addVariable("session:email", null, buf, pageContext, bundle, locale, inputId); 249 addVariable("session:fullname", null, buf, pageContext, bundle, locale, inputId); 250 addVariable("session:clientProxyURL", null, buf, pageContext, bundle, locale, inputId); 251 } 252 253 if(includeRequest) { 254 addVariable("request:serverName", null, buf, pageContext, bundle, locale, inputId); 255 addVariable("request:serverPort", null, buf, pageContext, bundle, locale, inputId); 256 addVariable("request:userAgent", null, buf, pageContext, bundle, locale, inputId); 257 } 258 259 if (includeUserAttributes) { 260 Collection <PropertyDefinition> l; 261 try { 262 for (PropertyClass propertyClass : PropertyClassManager.getInstance().getPropertyClasses()) { 263 if (propertyClass instanceof AttributesPropertyClass) { 264 l = propertyClass.getDefinitions(); 265 for (PropertyDefinition d : l) { 266 AttributeDefinition def = (AttributeDefinition)d; 267 if (def.isReplaceable()) { 268 addVariable(def.getPropertyClass().getName() + ":" + def.getName(), def.getDescription(), buf, pageContext, bundle, locale, inputId); 269 } 270 } 271 } 272 } 273 } catch (Exception e) { 274 log.error("Failed to get user attribute definitions."); 275 } 276 } 277 buf.append("</ul>"); 278 buf.append("</div>"); 279 buf.append("</div>"); 280 return buf.toString(); 281 } 282 283 static void addVariable(String variable, String desc, StringBuffer buf, PageContext pageContext, String bundle, String locale, String inputId) throws JspException { 284 buf.append("<li><a onmouseover=\"return escape('"); 285 if(desc == null) { 286 desc = TagUtils.getInstance().message(pageContext, bundle, locale, "replacementVariable." + variable + ".description", new String [] {}); 287 if(desc == null) { 288 desc = "${" + variable + "}"; 289 } 290 } 291 buf.append(desc); 292 buf.append("')\" HREF=\"#\" onclick=\"document.getElementById('"); 293 buf.append(inputId); 294 buf.append("').value = document.getElementById('"); 295 buf.append(inputId); 296 buf.append("').value + '${"); 297 buf.append(variable); 298 buf.append("}'; togglePopup(document.getElementById('replacementVariablesChooser"); 299 buf.append(inputId); 300 buf.append("'))\">"); 301 buf.append(variable); 302 buf.append("</a></li>"); 303 } 304 305 308 public void setDisabled(boolean disabled) { 309 this.disabled = disabled; 310 } 311 } 312 | Popular Tags |