1 31 package org.apache.commons.httpclient.methods; 32 33 import java.io.IOException ; 34 import java.util.Iterator ; 35 import java.util.Vector ; 36 37 import org.apache.commons.httpclient.HttpConnection; 38 import org.apache.commons.httpclient.HttpConstants; 39 import org.apache.commons.httpclient.HttpException; 40 import org.apache.commons.httpclient.HttpState; 41 import org.apache.commons.httpclient.NameValuePair; 42 import org.apache.commons.httpclient.util.EncodingUtil; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 77 public class PostMethod extends EntityEnclosingMethod { 78 80 81 private static final Log LOG = LogFactory.getLog(PostMethod.class); 82 83 84 public static final String FORM_URL_ENCODED_CONTENT_TYPE = 85 "application/x-www-form-urlencoded"; 86 87 90 private Vector params = new Vector (); 91 92 94 99 public PostMethod() { 100 super(); 101 } 102 103 110 public PostMethod(String uri) { 111 super(uri); 112 } 113 114 123 public PostMethod(String uri, String tempDir) { 124 super(uri, tempDir); 125 } 126 127 137 public PostMethod(String uri, String tempDir, String tempFile) { 138 super(uri, tempDir, tempFile); 139 } 140 141 143 150 public String getName() { 151 return "POST"; 152 } 153 154 155 166 protected boolean hasRequestContent() { 167 LOG.trace("enter PostMethod.hasRequestContent()"); 168 if (!this.params.isEmpty()) { 169 return true; 170 } else { 171 return super.hasRequestContent(); 172 } 173 } 174 175 183 protected void clearRequestBody() { 184 LOG.trace("enter PostMethod.clearRequestBody()"); 185 this.params.clear(); 186 super.clearRequestBody(); 187 } 188 189 201 protected byte[] generateRequestBody() { 202 LOG.trace("enter PostMethod.renerateRequestBody()"); 203 if (!this.params.isEmpty()) { 204 String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); 205 return HttpConstants.getContentBytes(content); 206 } else { 207 return super.generateRequestBody(); 208 } 209 } 210 211 212 221 public void setParameter(String parameterName, String parameterValue) { 222 LOG.trace("enter PostMethod.setParameter(String, String)"); 223 224 removeParameter(parameterName); 225 addParameter(parameterName, parameterValue); 226 } 227 228 240 public NameValuePair getParameter(String paramName) { 241 LOG.trace("enter PostMethod.getParameter(String)"); 242 243 if (paramName == null) { 244 return null; 245 } 246 247 Iterator iter = this.params.iterator(); 248 249 while (iter.hasNext()) { 250 NameValuePair parameter = (NameValuePair) iter.next(); 251 252 if (paramName.equals(parameter.getName())) { 253 return parameter; 254 } 255 } 256 return null; 257 } 258 259 270 public NameValuePair[] getParameters() { 271 LOG.trace("enter PostMethod.getParameters()"); 272 273 int numPairs = this.params.size(); 274 Object [] objectArr = this.params.toArray(); 275 NameValuePair[] nvPairArr = new NameValuePair[numPairs]; 276 277 for (int i = 0; i < numPairs; i++) { 278 nvPairArr[i] = (NameValuePair) objectArr[i]; 279 } 280 281 return nvPairArr; 282 } 283 284 294 public void addParameter(String paramName, String paramValue) 295 throws IllegalArgumentException { 296 LOG.trace("enter PostMethod.addParameter(String, String)"); 297 298 if ((paramName == null) || (paramValue == null)) { 299 throw new IllegalArgumentException ( 300 "Arguments to addParameter(String, String) cannot be null"); 301 } 302 super.clearRequestBody(); 303 this.params.add(new NameValuePair(paramName, paramValue)); 304 } 305 306 316 public void addParameter(NameValuePair param) 317 throws IllegalArgumentException { 318 LOG.trace("enter PostMethod.addParameter(NameValuePair)"); 319 320 if (param == null) { 321 throw new IllegalArgumentException ("NameValuePair may not be null"); 322 } 323 addParameter(param.getName(), param.getValue()); 324 } 325 326 334 public void addParameters(NameValuePair[] parameters) { 335 LOG.trace("enter PostMethod.addParameters(NameValuePair[])"); 336 337 if (parameters == null) { 338 LOG.warn("Attempt to addParameters(null) ignored"); 339 } else { 340 super.clearRequestBody(); 341 for (int i = 0; i < parameters.length; i++) { 342 this.params.add(parameters[i]); 343 } 344 } 345 } 346 347 361 public boolean removeParameter(String paramName) 362 throws IllegalArgumentException { 363 LOG.trace("enter PostMethod.removeParameter(String)"); 364 365 if (paramName == null) { 366 throw new IllegalArgumentException ( 367 "Argument passed to removeParameter(String) cannot be null"); 368 } 369 boolean removed = false; 370 Iterator iter = this.params.iterator(); 371 372 while (iter.hasNext()) { 373 NameValuePair pair = (NameValuePair) iter.next(); 374 375 if (paramName.equals(pair.getName())) { 376 iter.remove(); 377 removed = true; 378 } 379 } 380 return removed; 381 } 382 383 397 public boolean removeParameter(String paramName, String paramValue) 398 throws IllegalArgumentException { 399 LOG.trace("enter PostMethod.removeParameter(String, String)"); 400 401 if (paramName == null) { 402 throw new IllegalArgumentException ("Parameter name may not be null"); 403 } 404 if (paramValue == null) { 405 throw new IllegalArgumentException ("Parameter value may not be null"); 406 } 407 408 Iterator iter = this.params.iterator(); 409 410 while (iter.hasNext()) { 411 NameValuePair pair = (NameValuePair) iter.next(); 412 413 if (paramName.equals(pair.getName()) 414 && paramValue.equals(pair.getValue())) { 415 iter.remove(); 416 return true; 417 } 418 } 419 420 return false; 421 } 422 423 432 public void setRequestBody(NameValuePair[] parametersBody) 433 throws IllegalArgumentException { 434 LOG.trace("enter PostMethod.setRequestBody(NameValuePair[])"); 435 436 if (parametersBody == null) { 437 throw new IllegalArgumentException ("Array of parameters may not be null"); 438 } 439 clearRequestBody(); 440 addParameters(parametersBody); 441 } 442 443 460 protected void addRequestHeaders(HttpState state, HttpConnection conn) 461 throws IOException , HttpException { 462 super.addRequestHeaders(state, conn); 463 464 if (!this.params.isEmpty()) { 465 if (getRequestHeader("Content-Type") == null) { 467 setRequestHeader("Content-Type", FORM_URL_ENCODED_CONTENT_TYPE); 468 } 469 } 470 } 471 472 } 473 | Popular Tags |