1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v 1.41 2004/07/05 22:46:58 olegk Exp $
  3. * $Revision: 1.41 $
  4. * $Date: 2004/07/05 22:46:58 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import org.apache.commons.httpclient.auth.AuthState;
  33. import org.apache.commons.httpclient.params.HttpMethodParams;
  34. /**
  35. * <p>
  36. * HttpMethod interface represents a request to be sent via a
  37. * {@link HttpConnection HTTP connection} and a corresponding response.
  38. * </p>
  39. * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  40. * @author Rod Waldhoff
  41. * @author <a href="jsdever@apache.org">Jeff Dever</a>
  42. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  43. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  44. *
  45. * @version $Revision: 1.41 $ $Date: 2004/07/05 22:46:58 $
  46. *
  47. * @since 1.0
  48. */
  49. public interface HttpMethod {
  50. // ------------------------------------------- Property Setters and Getters
  51. /**
  52. * Obtains the name of the HTTP method as used in the HTTP request line,
  53. * for example <tt>"GET"</tt> or <tt>"POST"</tt>.
  54. *
  55. * @return the name of this method
  56. */
  57. String getName();
  58. /**
  59. * Gets the host configuration for this method. The configuration specifies
  60. * the server, port, protocol, and proxy server via which this method will
  61. * send its HTTP request.
  62. *
  63. * @return the HostConfiguration or <code>null</code> if none is set
  64. */
  65. HostConfiguration getHostConfiguration();
  66. /**
  67. * Sets the path of the HTTP method.
  68. * It is responsibility of the caller to ensure that the path is
  69. * properly encoded (URL safe).
  70. *
  71. * @param path The path of the HTTP method. The path is expected
  72. * to be URL encoded.
  73. */
  74. void setPath(String path);
  75. /**
  76. * Returns the path of the HTTP method.
  77. *
  78. * Calling this method <em>after</em> the request has been executed will
  79. * return the <em>actual</em> path, following any redirects automatically
  80. * handled by this HTTP method.
  81. *
  82. * @return the path of the HTTP method, in URL encoded form
  83. */
  84. String getPath();
  85. /**
  86. * Returns the URI for this method. The URI will be absolute if the host
  87. * configuration has been set and relative otherwise.
  88. *
  89. * @return the URI for this method
  90. *
  91. * @throws URIException if a URI cannot be constructed
  92. */
  93. URI getURI() throws URIException;
  94. /**
  95. * Sets the URI for this method.
  96. *
  97. * @param uri URI to be set
  98. *
  99. * @throws URIException if a URI cannot be set
  100. *
  101. * @since 3.0
  102. */
  103. void setURI(URI uri) throws URIException;
  104. /**
  105. * Defines how strictly the method follows the HTTP protocol specification.
  106. * (See RFC 2616 and other relevant RFCs.) In the strict mode the method precisely
  107. * implements the requirements of the specification, whereas in non-strict mode
  108. * it attempts to mimic the exact behaviour of commonly used HTTP agents,
  109. * which many HTTP servers expect.
  110. *
  111. * @param strictMode <tt>true</tt> for strict mode, <tt>false</tt> otherwise
  112. *
  113. * @deprecated Use {@link org.apache.commons.httpclient.params.HttpParams#setParameter(String, Object)}
  114. * to exercise a more granular control over HTTP protocol strictness.
  115. *
  116. * @see #isStrictMode()
  117. */
  118. void setStrictMode(boolean strictMode);
  119. /**
  120. * Returns the value of the strict mode flag.
  121. *
  122. * @return <tt>true</tt> if strict mode is enabled, <tt>false</tt> otherwise
  123. *
  124. * @deprecated Use {@link org.apache.commons.httpclient.params.HttpParams#setParameter(String, Object)}
  125. * to exercise a more granular control over HTTP protocol strictness.
  126. *
  127. * @see #setStrictMode(boolean)
  128. */
  129. boolean isStrictMode();
  130. /**
  131. * Sets the specified request header, overwriting any
  132. * previous value.
  133. * Note that header-name matching is case insensitive.
  134. * @param headerName the header's name
  135. * @param headerValue the header's value
  136. *
  137. * @see #setRequestHeader(Header)
  138. * @see #getRequestHeader(String)
  139. * @see #removeRequestHeader(String)
  140. */
  141. void setRequestHeader(String headerName, String headerValue);
  142. /**
  143. * Sets the specified request header, overwriting any
  144. * previous value.
  145. * Note that header-name matching is case insensitive.
  146. * @param header the header to be set
  147. *
  148. * @see #setRequestHeader(String,String)
  149. * @see #getRequestHeader(String)
  150. * @see #removeRequestHeader(String)
  151. */
  152. void setRequestHeader(Header header);
  153. /**
  154. * Adds the specified request header, <em>not</em> overwriting any previous value.
  155. * If the same header is added multiple times, perhaps with different values,
  156. * multiple instances of that header will be sent in the HTTP request.
  157. * Note that header-name matching is case insensitive.
  158. * @param headerName the header's name
  159. * @param headerValue the header's value
  160. *
  161. * @see #addRequestHeader(Header)
  162. * @see #getRequestHeader(String)
  163. * @see #removeRequestHeader(String)
  164. */
  165. void addRequestHeader(String headerName, String headerValue);
  166. /**
  167. * Adds the specified request header, <em>not</em> overwriting any previous value.
  168. * If the same header is added multiple times, perhaps with different values,
  169. * multiple instances of that header will be sent in the HTTP request.
  170. * Note that header-name matching is case insensitive.
  171. * @param header the header
  172. *
  173. * @see #addRequestHeader(String,String)
  174. * @see #getRequestHeader(String)
  175. * @see #removeRequestHeader(String)
  176. */
  177. void addRequestHeader(Header header);
  178. /**
  179. * Gets the request header with the given name.
  180. * If there are multiple headers with the same name,
  181. * there values will be combined with the ',' separator as specified by RFC2616.
  182. * Note that header-name matching is case insensitive.
  183. * @param headerName the header name
  184. * @return the header
  185. */
  186. Header getRequestHeader(String headerName);
  187. /**
  188. * Removes all request headers with the given name.
  189. * Note that header-name matching is case insensitive.
  190. * @param headerName the header name
  191. */
  192. void removeRequestHeader(String headerName);
  193. /**
  194. * Removes the given request header.
  195. *
  196. * @param header the header
  197. *
  198. * @since 3.0
  199. */
  200. void removeRequestHeader(Header header);
  201. /**
  202. * Returns <tt>true</tt> if the HTTP method should automatically follow HTTP redirects
  203. * (status code 302, etc.), <tt>false</tt> otherwise.
  204. *
  205. * @return <tt>true</tt> if the method will automatically follow HTTP redirects,
  206. * <tt>false</tt> otherwise
  207. */
  208. boolean getFollowRedirects();
  209. /**
  210. * Sets whether or not the HTTP method should automatically follow HTTP redirects
  211. * (status code 302, etc.)
  212. *
  213. * @param followRedirects <tt>true</tt> if the method will automatically follow redirects,
  214. * <tt>false</tt> otherwise.
  215. */
  216. void setFollowRedirects(boolean followRedirects);
  217. /**
  218. * Sets the query string of the HTTP method.
  219. * It is responsibility of the caller to ensure that the path is
  220. * properly encoded (URL safe). The string must not include an initial '?' character.
  221. *
  222. * @param queryString the query to be used in the request, with no leading '?' character
  223. *
  224. * @see #getQueryString()
  225. * @see #setQueryString(NameValuePair[])
  226. */
  227. void setQueryString(String queryString);
  228. /**
  229. * Sets the query string of this HTTP method. The pairs are encoded as UTF-8 characters.
  230. * To use a different charset the parameters can be encoded manually using EncodingUtil
  231. * and set as a single String.
  232. *
  233. * @param params An array of <code>NameValuePair</code>s to use as the query string.
  234. * The name/value pairs will be automatically URL encoded and should not
  235. * have been encoded previously.
  236. *
  237. * @see #getQueryString()
  238. * @see #setQueryString(String)
  239. * @see org.apache.commons.httpclient.util.EncodingUtil#formUrlEncode(NameValuePair[], String)
  240. */
  241. void setQueryString(NameValuePair[] params);
  242. /**
  243. * Returns the query string of this HTTP method.
  244. *
  245. * @return the query string in URL encoded form, without a leading '?'.
  246. *
  247. * @see #setQueryString(NameValuePair[])
  248. * @see #setQueryString(String)
  249. */
  250. String getQueryString();
  251. /**
  252. * Returns the current request headers for this HTTP method. The returned headers
  253. * will be in the same order that they were added with <code>addRequestHeader</code>.
  254. * If there are multiple request headers with the same name (e.g. <code>Cookie</code>),
  255. * they will be returned as multiple entries in the array.
  256. *
  257. * @return an array containing all of the request headers
  258. *
  259. * @see #addRequestHeader(Header)
  260. * @see #addRequestHeader(String,String)
  261. */
  262. Header[] getRequestHeaders();
  263. /**
  264. * Returns the request headers with the given name. Note that header-name matching is
  265. * case insensitive.
  266. * @param headerName the name of the headers to be returned.
  267. * @return an array of zero or more headers
  268. *
  269. * @since 3.0
  270. */
  271. Header[] getRequestHeaders(String headerName);
  272. // ---------------------------------------------------------------- Queries
  273. /**
  274. * Returns <tt>true</tt> the method is ready to execute, <tt>false</tt> otherwise.
  275. *
  276. * @return <tt>true</tt> if the method is ready to execute, <tt>false</tt> otherwise.
  277. */
  278. boolean validate();
  279. /**
  280. * Returns the status code associated with the latest response.
  281. *
  282. * @return The status code from the most recent execution of this method.
  283. * If the method has not yet been executed, the result is undefined.
  284. */
  285. int getStatusCode();
  286. /**
  287. * Returns the status text (or "reason phrase") associated with the latest
  288. * response.
  289. *
  290. * @return The status text from the most recent execution of this method.
  291. * If the method has not yet been executed, the result is undefined.
  292. */
  293. String getStatusText();
  294. /**
  295. * Returns the response headers from the most recent execution of this request.
  296. *
  297. * @return A newly-created array containing all of the response headers,
  298. * in the order in which they appeared in the response.
  299. */
  300. Header[] getResponseHeaders();
  301. /**
  302. * Returns the specified response header. Note that header-name matching is
  303. * case insensitive.
  304. *
  305. * @param headerName The name of the header to be returned.
  306. *
  307. * @return The specified response header. If the repsonse contained multiple
  308. * instances of the header, its values will be combined using the ','
  309. * separator as specified by RFC2616.
  310. */
  311. Header getResponseHeader(String headerName);
  312. /**
  313. * Returns the response headers with the given name. Note that header-name matching is
  314. * case insensitive.
  315. * @param headerName the name of the headers to be returned.
  316. * @return an array of zero or more headers
  317. *
  318. * @since 3.0
  319. */
  320. Header[] getResponseHeaders(String headerName);
  321. /**
  322. * Returns the response footers from the most recent execution of this request.
  323. *
  324. * @return an array containing the response footers in the order that they
  325. * appeared in the response. If the response had no footers,
  326. * an empty array will be returned.
  327. */
  328. Header[] getResponseFooters();
  329. /**
  330. * Return the specified response footer. Note that footer-name matching is
  331. * case insensitive.
  332. *
  333. * @param footerName The name of the footer.
  334. * @return The response footer.
  335. */
  336. Header getResponseFooter(String footerName);
  337. /**
  338. * Returns the response body of the HTTP method, if any, as an array of bytes.
  339. * If the method has not yet been executed or the response has no body, <code>null</code>
  340. * is returned. Note that this method does not propagate I/O exceptions.
  341. * If an error occurs while reading the body, <code>null</code> will be returned.
  342. *
  343. * @return The response body, or <code>null</code> if the
  344. * body is not available.
  345. *
  346. * @throws IOException if an I/O (transport) problem occurs
  347. */
  348. byte[] getResponseBody() throws IOException;
  349. /**
  350. * Returns the response body of the HTTP method, if any, as a {@link String}.
  351. * If response body is not available or cannot be read, <tt>null</tt> is returned.
  352. * The raw bytes in the body are converted to a <code>String</code> using the
  353. * character encoding specified in the response's <tt>Content-Type</tt> header, or
  354. * ISO-8859-1 if the response did not specify a character set.
  355. * <p>
  356. * Note that this method does not propagate I/O exceptions.
  357. * If an error occurs while reading the body, <code>null</code> will be returned.
  358. *
  359. * @return The response body converted to a <code>String</code>, or <code>null</code>
  360. * if the body is not available.
  361. *
  362. * @throws IOException if an I/O (transport) problem occurs
  363. */
  364. String getResponseBodyAsString() throws IOException;
  365. /**
  366. * Returns the response body of the HTTP method, if any, as an InputStream.
  367. * If the response had no body or the method has not yet been executed,
  368. * <code>null</code> is returned. Additionally, <code>null</code> may be returned
  369. * if {@link #releaseConnection} has been called or
  370. * if this method was called previously and the resulting stream was closed.
  371. *
  372. * @return The response body, or <code>null</code> if it is not available
  373. *
  374. * @throws IOException if an I/O (transport) problem occurs
  375. */
  376. InputStream getResponseBodyAsStream() throws IOException;
  377. /**
  378. * Returns <tt>true</tt> if the HTTP method has been already {@link #execute executed},
  379. * but not {@link #recycle recycled}.
  380. *
  381. * @return <tt>true</tt> if the method has been executed, <tt>false</tt> otherwise
  382. */
  383. boolean hasBeenUsed();
  384. // --------------------------------------------------------- Action Methods
  385. /**
  386. * Executes this method using the specified <code>HttpConnection</code> and
  387. * <code>HttpState</code>.
  388. *
  389. * @param state the {@link HttpState state} information to associate with this method
  390. * @param connection the {@link HttpConnection connection} used to execute
  391. * this HTTP method
  392. *
  393. * @throws IOException If an I/O (transport) error occurs. Some transport exceptions
  394. * can be recovered from.
  395. * @throws HttpException If a protocol exception occurs. Usually protocol exceptions
  396. * cannot be recovered from.
  397. *
  398. * @return the integer status code if one was obtained, or <tt>-1</tt>
  399. */
  400. int execute(HttpState state, HttpConnection connection)
  401. throws HttpException, IOException;
  402. /**
  403. * Aborts the execution of the HTTP method.
  404. *
  405. * @see #execute(HttpState, HttpConnection)
  406. *
  407. * @since 3.0
  408. */
  409. void abort();
  410. /**
  411. * Recycles the HTTP method so that it can be used again.
  412. * Note that all of the instance variables will be reset
  413. * once this method has been called. This method will also
  414. * release the connection being used by this HTTP method.
  415. *
  416. * @see #releaseConnection()
  417. *
  418. * @deprecated no longer supported and will be removed in the future
  419. * version of HttpClient
  420. */
  421. void recycle();
  422. /**
  423. * Releases the connection being used by this HTTP method. In particular the
  424. * connection is used to read the response (if there is one) and will be held
  425. * until the response has been read. If the connection can be reused by other
  426. * HTTP methods it is NOT closed at this point.
  427. * <p>
  428. * After this method is called, {@link #getResponseBodyAsStream} will return
  429. * <code>null</code>, and {@link #getResponseBody} and {@link #getResponseBodyAsString}
  430. * <em>may</em> return <code>null</code>.
  431. */
  432. void releaseConnection();
  433. /**
  434. * Add a footer to this method's response.
  435. * <p>
  436. * <b>Note:</b> This method is for
  437. * internal use only and should not be called by external clients.
  438. *
  439. * @param footer the footer to add
  440. *
  441. * @since 2.0
  442. */
  443. void addResponseFooter(Header footer);
  444. /**
  445. * Returns the Status-Line from the most recent response for this method,
  446. * or <code>null</code> if the method has not been executed.
  447. *
  448. * @return the status line, or <code>null</code> if the method has not been executed
  449. *
  450. * @since 2.0
  451. */
  452. StatusLine getStatusLine();
  453. /**
  454. * Returns <tt>true</tt> if the HTTP method should automatically handle HTTP
  455. * authentication challenges (status code 401, etc.), <tt>false</tt> otherwise
  456. *
  457. * @return <tt>true</tt> if authentication challenges will be processed
  458. * automatically, <tt>false</tt> otherwise.
  459. *
  460. * @since 2.0
  461. *
  462. * @see #setDoAuthentication(boolean)
  463. */
  464. boolean getDoAuthentication();
  465. /**
  466. * Sets whether or not the HTTP method should automatically handle HTTP
  467. * authentication challenges (status code 401, etc.)
  468. *
  469. * @param doAuthentication <tt>true</tt> to process authentication challenges
  470. * automatically, <tt>false</tt> otherwise.
  471. *
  472. * @since 2.0
  473. *
  474. * @see #getDoAuthentication()
  475. */
  476. void setDoAuthentication(boolean doAuthentication);
  477. /**
  478. * Returns {@link HttpMethodParams HTTP protocol parameters} associated with this method.
  479. *
  480. * @since 3.0
  481. *
  482. * @see HttpMethodParams
  483. */
  484. public HttpMethodParams getParams();
  485. /**
  486. * Assigns {@link HttpMethodParams HTTP protocol parameters} for this method.
  487. *
  488. * @since 3.0
  489. *
  490. * @see HttpMethodParams
  491. */
  492. public void setParams(final HttpMethodParams params);
  493. /**
  494. * Returns the target host {@link AuthState authentication state}
  495. *
  496. * @return host authentication state
  497. *
  498. * @since 3.0
  499. */
  500. public AuthState getHostAuthState();
  501. /**
  502. * Returns the proxy {@link AuthState authentication state}
  503. *
  504. * @return host authentication state
  505. *
  506. * @since 3.0
  507. */
  508. public AuthState getProxyAuthState();
  509. /**
  510. * Returns <tt>true</tt> if the HTTP has been transmitted to the target
  511. * server in its entirety, <tt>false</tt> otherwise. This flag can be useful
  512. * for recovery logic. If the request has not been transmitted in its entirety,
  513. * it is safe to retry the failed method.
  514. *
  515. * @return <tt>true</tt> if the request has been sent, <tt>false</tt> otherwise
  516. */
  517. boolean isRequestSent();
  518. }