1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/OptionsMethod.java,v 1.15 2004/04/18 23:51:37 jsdever Exp $
  3. * $Revision: 1.15 $
  4. * $Date: 2004/04/18 23:51:37 $
  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.methods;
  30. import org.apache.commons.httpclient.Header;
  31. import org.apache.commons.httpclient.HttpConnection;
  32. import org.apache.commons.httpclient.HttpMethodBase;
  33. import org.apache.commons.httpclient.HttpState;
  34. import org.apache.commons.logging.LogFactory;
  35. import org.apache.commons.logging.Log;
  36. import java.util.Enumeration;
  37. import java.util.StringTokenizer;
  38. import java.util.Vector;
  39. /**
  40. * Implements the HTTP OPTIONS method.
  41. * <p>
  42. * The HTTP OPTIONS method is defined in section 9.2 of
  43. * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
  44. * <blockquote>
  45. * The OPTIONS method represents a request for information about the
  46. * communication options available on the request/response chain
  47. * identified by the Request-URI. This method allows the client to
  48. * determine the options and/or requirements associated with a resource,
  49. * or the capabilities of a server, without implying a resource action
  50. * or initiating a resource retrieval.
  51. * </blockquote>
  52. * </p>
  53. *
  54. * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  55. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  56. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  57. *
  58. * @version $Revision: 1.15 $
  59. * @since 1.0
  60. */
  61. public class OptionsMethod
  62. extends HttpMethodBase {
  63. // --------------------------------------------------------- Class Variables
  64. /** Log object for this class. */
  65. private static final Log LOG = LogFactory.getLog(OptionsMethod.class);
  66. // ----------------------------------------------------------- Constructors
  67. /**
  68. * Method constructor.
  69. *
  70. * @since 1.0
  71. */
  72. public OptionsMethod() {
  73. }
  74. /**
  75. * Constructor specifying a URI.
  76. *
  77. * @param uri either an absolute or relative URI
  78. *
  79. * @since 1.0
  80. */
  81. public OptionsMethod(String uri) {
  82. super(uri);
  83. }
  84. // ----------------------------------------------------- Instance Variables
  85. /**
  86. * Methods allowed.
  87. */
  88. private Vector methodsAllowed = new Vector();
  89. // --------------------------------------------------------- Public Methods
  90. /**
  91. * Get the name.
  92. * @return "OPTIONS"
  93. * @since 2.0
  94. */
  95. public String getName() {
  96. return "OPTIONS";
  97. }
  98. /**
  99. * Is the specified method allowed ?
  100. *
  101. * @param method The method to check.
  102. * @return true if the specified method is allowed.
  103. * @since 1.0
  104. */
  105. public boolean isAllowed(String method) {
  106. checkUsed();
  107. return methodsAllowed.contains(method);
  108. }
  109. /**
  110. * Get a list of allowed methods.
  111. * @return An enumeration of all the allowed methods.
  112. *
  113. * @since 1.0
  114. */
  115. public Enumeration getAllowedMethods() {
  116. checkUsed();
  117. return methodsAllowed.elements();
  118. }
  119. // ----------------------------------------------------- HttpMethod Methods
  120. /**
  121. * <p>
  122. * This implementation will parse the <tt>Allow</tt> header to obtain
  123. * the set of methods supported by the resource identified by the Request-URI.
  124. * </p>
  125. *
  126. * @param state the {@link HttpState state} information associated with this method
  127. * @param conn the {@link HttpConnection connection} used to execute
  128. * this HTTP method
  129. *
  130. * @see #readResponse
  131. * @see #readResponseHeaders
  132. * @since 2.0
  133. */
  134. protected void processResponseHeaders(HttpState state, HttpConnection conn) {
  135. LOG.trace("enter OptionsMethod.processResponseHeaders(HttpState, HttpConnection)");
  136. Header allowHeader = getResponseHeader("allow");
  137. if (allowHeader != null) {
  138. String allowHeaderValue = allowHeader.getValue();
  139. StringTokenizer tokenizer =
  140. new StringTokenizer(allowHeaderValue, ",");
  141. while (tokenizer.hasMoreElements()) {
  142. String methodAllowed =
  143. tokenizer.nextToken().trim().toUpperCase();
  144. methodsAllowed.addElement(methodAllowed);
  145. }
  146. }
  147. }
  148. /**
  149. * Return true if the method needs a content-length header in the request.
  150. *
  151. * @return true if a content-length header will be expected by the server
  152. *
  153. * @since 1.0
  154. *
  155. * @deprecated only entity enclosing methods set content length header
  156. */
  157. public boolean needContentLength() {
  158. return false;
  159. }
  160. }