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
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$
65 * @since 1.0
66 */
67 public class OptionsMethod
68 extends HttpMethodBase {
69
70
71
72
73 /*** Log object for this class. */
74 private static final Log LOG = LogFactory.getLog(OptionsMethod.class);
75
76
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
101
102
103 /***
104 * Methods allowed.
105 */
106 private Vector methodsAllowed = new Vector();
107
108
109
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
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 }