1 38 package com.gargoylesoftware.htmlunit.jelly; 39 40 import java.io.IOException ; 41 import java.net.MalformedURLException ; 42 import java.net.URL ; 43 import java.util.ArrayList ; 44 import java.util.Collections ; 45 import java.util.List ; 46 47 import org.apache.commons.jelly.JellyContext; 48 import org.apache.commons.jelly.JellyTagException; 49 import org.apache.commons.jelly.XMLOutput; 50 51 import com.gargoylesoftware.htmlunit.Page; 52 import com.gargoylesoftware.htmlunit.SubmitMethod; 53 import com.gargoylesoftware.htmlunit.WebClient; 54 import com.gargoylesoftware.htmlunit.WebRequestSettings; 55 56 63 public class GetPageTag extends HtmlUnitTagSupport { 64 private String url_ = null; 65 private List parameters_ = null; 66 private String method_ = "get"; 67 private String webClientName_; 68 69 72 public GetPageTag() { 73 } 74 75 76 81 public void doTag(final XMLOutput xmlOutput) throws JellyTagException { 82 invokeBody(xmlOutput); 83 84 final JellyContext jellyContext = getContext(); 85 86 final WebClient webClient; 87 if( webClientName_ != null ) { 88 webClient = (WebClient)jellyContext.getVariable(webClientName_); 89 if( webClient == null ) { 90 throw new JellyTagException("No webclient found for name ["+webClientName_+"]"); 91 } 92 } 93 else { 94 webClient = getWebClient(); 95 } 96 97 final Page page; 98 try { 99 final WebRequestSettings settings = new WebRequestSettings(getUrl(), getSubmitMethod()); 100 settings.setRequestParameters(getParameters()); 101 page = webClient.getPage(settings); 102 jellyContext.setVariable( getVarValueOrDie(), page ); 103 } 104 catch( final IOException e ) { 105 throw new JellyTagException(e); 106 } 107 } 108 109 110 114 public void setUri( final String url ) { 115 url_ = url; 116 } 117 118 119 124 public URL getUrl() throws JellyTagException { 125 if( url_ == null ) { 126 throw new JellyTagException("url attribute is mandatory"); 127 } 128 129 try { 130 return new URL (url_); 131 } 132 catch( final MalformedURLException e ) { 133 throw new JellyTagException("url attribute is malformed: "+url_); 134 } 135 } 136 137 138 142 public synchronized void addParameter( final String parameter ) { 143 if( parameters_ == null ) { 144 parameters_ = new ArrayList (); 145 } 146 parameters_.add(parameter); 147 } 148 149 150 154 public synchronized List getParameters() { 155 if( parameters_ == null ) { 156 return Collections.EMPTY_LIST; 157 } 158 else { 159 return parameters_; 160 } 161 } 162 163 164 168 public void setMethod( final String method ) { 169 method_ = method; 170 } 171 172 173 177 public void setWebclient( final String webClientName ) { 178 webClientName_ = webClientName; 179 } 180 181 182 187 public SubmitMethod getSubmitMethod() throws JellyTagException { 188 try { 189 return SubmitMethod.getInstance(method_); 190 } 191 catch( final IllegalArgumentException e ) { 192 throw new JellyTagException("Value of method attribute is not a valid submit method: "+method_); 194 } 195 } 196 } 197 | Popular Tags |