1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
71
72 /*** Log object for this class. */
73 private static final Log LOG = LogFactory.getLog(OptionsMethod.class);
74
75
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
100
101
102 /***
103 * Methods allowed.
104 */
105 private Vector methodsAllowed = new Vector();
106
107
108
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
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 }