View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/methods/OptionsMethod.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient.methods;
32  
33  import org.apache.commons.httpclient.Header;
34  import org.apache.commons.httpclient.HttpConnection;
35  import org.apache.commons.httpclient.HttpMethodBase;
36  import org.apache.commons.httpclient.HttpState;
37  
38  import org.apache.commons.logging.LogFactory;
39  import org.apache.commons.logging.Log;
40  import java.util.Enumeration;
41  import java.util.StringTokenizer;
42  import java.util.Vector;
43  
44  
45  /***
46   * Implements the HTTP OPTIONS method.
47   * <p>
48   * The HTTP OPTIONS method is defined in section 9.2 of 
49   * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
50   * <blockquote>
51   *  The OPTIONS method represents a request for information about the
52   *  communication options available on the request/response chain
53   *  identified by the Request-URI. This method allows the client to
54   *  determine the options and/or requirements associated with a resource,
55   *  or the capabilities of a server, without implying a resource action
56   *  or initiating a resource retrieval.
57   * </blockquote>
58   * </p>
59   *
60   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
61   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
62   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
63   *
64   * @version $Revision: 1425331 $
65   * @since 1.0
66   */
67  public class OptionsMethod
68      extends HttpMethodBase {
69  
70  
71      // --------------------------------------------------------- Class Variables
72  
73      /*** Log object for this class. */
74      private static final Log LOG = LogFactory.getLog(OptionsMethod.class);
75  
76      // ----------------------------------------------------------- Constructors
77  
78  
79      /***
80       * Method constructor.
81       *
82       * @since 1.0
83       */
84      public OptionsMethod() {
85      }
86  
87  
88      /***
89       * Constructor specifying a URI.
90       *
91       * @param uri either an absolute or relative URI
92       *
93       * @since 1.0
94       */
95      public OptionsMethod(String uri) {
96          super(uri);
97      }
98  
99  
100     // ----------------------------------------------------- Instance Variables
101 
102 
103     /***
104      * Methods allowed.
105      */
106     private Vector methodsAllowed = new Vector();
107 
108 
109     // --------------------------------------------------------- Public Methods
110 
111     /***
112      * Get the name.
113      * @return "OPTIONS"
114      * @since 2.0
115      */
116     public String getName() {
117         return "OPTIONS";
118     }
119 
120 
121     /***
122      * Is the specified method allowed ?
123      * 
124      * @param method The method to check.
125      * @return true if the specified method is allowed.
126      * @since 1.0
127      */
128     public boolean isAllowed(String method) {
129         checkUsed();
130         return methodsAllowed.contains(method);
131     }
132 
133 
134     /***
135      * Get a list of allowed methods.
136      * @return An enumeration of all the allowed methods.
137      *
138      * @since 1.0
139      */
140     public Enumeration getAllowedMethods() {
141         checkUsed();
142         return methodsAllowed.elements();
143     }
144 
145 
146     // ----------------------------------------------------- HttpMethod Methods
147 
148     /***
149      * <p>
150      * This implementation will parse the <tt>Allow</tt> header to obtain 
151      * the set of methods supported by the resource identified by the Request-URI.
152      * </p>
153      *
154      * @param state the {@link HttpState state} information associated with this method
155      * @param conn the {@link HttpConnection connection} used to execute
156      *        this HTTP method
157      *
158      * @see #readResponse
159      * @see #readResponseHeaders
160      * @since 2.0
161      */
162     protected void processResponseHeaders(HttpState state, HttpConnection conn) {
163         LOG.trace("enter OptionsMethod.processResponseHeaders(HttpState, HttpConnection)");
164 
165         Header allowHeader = getResponseHeader("allow");
166         if (allowHeader != null) {
167             String allowHeaderValue = allowHeader.getValue();
168             StringTokenizer tokenizer =
169                 new StringTokenizer(allowHeaderValue, ",");
170             while (tokenizer.hasMoreElements()) {
171                 String methodAllowed =
172                     tokenizer.nextToken().trim().toUpperCase();
173                 methodsAllowed.addElement(methodAllowed);
174             }
175         }
176     }
177 
178     /***
179      * Return true if the method needs a content-length header in the request.
180      *
181      * @return true if a content-length header will be expected by the server
182      *
183      * @since 1.0
184      * 
185      * @deprecated only entity enclosing methods set content length header
186      */
187     public boolean needContentLength() {
188         return false;
189     }
190 
191 
192 }