View Javadoc

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