1 38 package com.gargoylesoftware.htmlunit.jelly; 39 40 import java.lang.reflect.Field ; 41 42 import org.apache.commons.jelly.JellyTagException; 43 import org.apache.commons.jelly.XMLOutput; 44 45 import com.gargoylesoftware.htmlunit.BrowserVersion; 46 import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 49 55 public class WebClientTag extends HtmlUnitTagSupport { 56 private WebClient webClient_; 57 private String userId_; 58 private String password_; 59 private String browserVersionName_; 60 61 64 public WebClientTag() { 65 } 66 67 68 74 public void doTag(final XMLOutput xmlOutput) throws JellyTagException { 75 final BrowserVersion browserVersion = getBrowserVersion(); 76 if( browserVersion == null ) { 77 webClient_ = new WebClient(); 78 } 79 else { 80 webClient_ = new WebClient(browserVersion); 81 } 82 if( userId_ != null || password_ != null ) { 83 if( userId_ == null || password_ == null ) { 84 throw new JellyTagException("userid and password must either both be set or neither set"); 85 } 86 ((DefaultCredentialsProvider) webClient_.getCredentialsProvider()).addCredentials( userId_, password_ ); 87 } 88 89 final String varName = getVarValueOrNull(); 90 if( varName != null ) { 91 getContext().setVariable(varName, webClient_); 92 } 93 invokeBody(xmlOutput); 94 } 95 96 97 101 public void setBrowserVersion( final String browserVersion ) { 102 browserVersionName_ = browserVersion; 103 } 104 105 106 private BrowserVersion getBrowserVersion() { 107 if( browserVersionName_ == null ) { 108 return null; 109 } 110 111 try { 112 final Field field = BrowserVersion.class.getDeclaredField(browserVersionName_); 113 return (BrowserVersion)field.get(null); 114 } 115 catch( final NoSuchFieldException e ) { 116 return null; 117 } 118 catch( final IllegalAccessException e ) { 119 return null; 122 } 123 } 124 125 129 public WebClient getWebClient() { 130 return webClient_; 131 } 132 133 134 138 public void setUserid( final String userid ) { 139 userId_ = userid; 140 } 141 142 143 147 public void setPassword( final String password ) { 148 password_ = password; 149 } 150 } 151 | Popular Tags |