1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.net.URL ; 41 import java.util.Collections ; 42 import java.util.HashMap ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 import org.org.apache.commons.httpclient.auth.CredentialsProvider; 47 import org.apache.commons.lang.ClassUtils; 48 49 56 public class WebRequestSettings { 57 private URL url_; 58 private SubmitMethod submitMethod_ = SubmitMethod.GET; 59 private FormEncodingType encodingType_ = FormEncodingType.URL_ENCODED; 60 private Map additionalHeaders_ = new HashMap (); 61 private CredentialsProvider credentialsProvider_ = null; 62 63 64 private List requestParameters_ = Collections.EMPTY_LIST; 65 private String requestBody_ = null; 66 67 70 public WebRequestSettings(final URL target) { 71 setURL(target); 72 } 73 74 78 public WebRequestSettings(final URL target, final SubmitMethod submitMethod) { 79 this(target); 80 setSubmitMethod(submitMethod); 81 } 82 83 86 public URL getURL() { 87 return url_; 88 } 89 90 93 public void setURL(final URL url) { 94 url_ = url; 95 } 96 97 100 public FormEncodingType getEncodingType() { 101 return encodingType_; 102 } 103 104 107 public void setEncodingType(final FormEncodingType encodingType) { 108 encodingType_ = encodingType; 109 } 110 111 114 public List getRequestParameters() { 115 return requestParameters_; 116 } 117 118 122 public void setRequestParameters(final List requestParameters) throws RuntimeException { 123 if( requestBody_ != null ) { 124 final String msg = "Trying to set the request parameters, but the request body has already been specified;" 125 + "the two are mutually exclusive!"; 126 throw new RuntimeException ( msg ); 127 } 128 requestParameters_ = requestParameters; 129 } 130 131 136 public String getRequestBody() { 137 return requestBody_; 138 } 139 140 144 public void setRequestBody(final String requestBody) throws RuntimeException { 145 if( requestParameters_ != null && requestParameters_.size() > 0 ) { 146 String msg = "Trying to set the request body, but the request parameters have already been specified;" 147 + "the two are mutually exclusive!"; 148 throw new RuntimeException ( msg ); 149 } 150 if( submitMethod_ != SubmitMethod.POST ) { 151 String msg = "The request body may only be set for POST requests!"; 152 throw new RuntimeException ( msg ); 153 } 154 requestBody_ = requestBody; 155 } 156 157 160 public SubmitMethod getSubmitMethod() { 161 return submitMethod_; 162 } 163 164 167 public void setSubmitMethod(final SubmitMethod submitMethod) { 168 submitMethod_ = submitMethod; 169 } 170 173 public Map getAdditionalHeaders() { 174 return additionalHeaders_; 175 } 176 177 180 public void setAdditionalHeaders(final Map additionalHeaders) { 181 additionalHeaders_ = additionalHeaders; 182 } 183 184 189 public void addAdditionalHeader(final String name, final String value) { 190 additionalHeaders_.put( name, value ); 191 } 192 193 196 public CredentialsProvider getCredentialsProvider() { 197 return credentialsProvider_; 198 } 199 202 public void setCredentialsProvider(final CredentialsProvider credentialsProvider) { 203 credentialsProvider_ = credentialsProvider; 204 } 205 206 210 public String toString() { 211 final StringBuffer buffer = new StringBuffer (); 212 213 buffer.append(ClassUtils.getShortClassName(getClass())); 214 215 buffer.append("[<"); 216 buffer.append("url=\"" + url_.toExternalForm() + "\""); 217 buffer.append(", " + submitMethod_); 218 buffer.append(", " + encodingType_); 219 buffer.append(", " + requestParameters_); 220 buffer.append(", " + additionalHeaders_); 221 buffer.append(", " + credentialsProvider_); 222 buffer.append(">]"); 223 224 return buffer.toString(); 225 } 226 227 } 228 | Popular Tags |