KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > methods > OptionsMethod


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/OptionsMethod.java,v 1.12.2.2 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.12.2.2 $
4  * $Date: 2004/02/22 18:21:15 $
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  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.methods;
33
34 import org.apache.commons.httpclient.Header;
35 import org.apache.commons.httpclient.HttpConnection;
36 import org.apache.commons.httpclient.HttpMethodBase;
37 import org.apache.commons.httpclient.HttpState;
38
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.commons.logging.Log;
41 import java.util.Enumeration JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43 import java.util.Vector JavaDoc;
44
45
46 /**
47  * Implements the HTTP OPTIONS method.
48  * <p>
49  * The HTTP OPTIONS method is defined in section 9.2 of
50  * <a HREF="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
51  * <blockquote>
52  * The OPTIONS method represents a request for information about the
53  * communication options available on the request/response chain
54  * identified by the Request-URI. This method allows the client to
55  * determine the options and/or requirements associated with a resource,
56  * or the capabilities of a server, without implying a resource action
57  * or initiating a resource retrieval.
58  * </blockquote>
59  * </p>
60  *
61  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
62  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
63  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
64  *
65  * @version $Revision: 1.12.2.2 $
66  * @since 1.0
67  */

68 public class OptionsMethod
69     extends HttpMethodBase {
70
71
72     // --------------------------------------------------------- Class Variables
73

74     /** Log object for this class. */
75     private static final Log LOG = LogFactory.getLog(OptionsMethod.class);
76
77     // ----------------------------------------------------------- Constructors
78

79
80     /**
81      * Method constructor.
82      *
83      * @since 1.0
84      */

85     public OptionsMethod() {
86     }
87
88
89     /**
90      * Constructor specifying a URI.
91      *
92      * @param uri either an absolute or relative URI
93      *
94      * @since 1.0
95      */

96     public OptionsMethod(String JavaDoc uri) {
97         super(uri);
98     }
99
100
101     // ----------------------------------------------------- Instance Variables
102

103
104     /**
105      * Methods allowed.
106      */

107     private Vector JavaDoc methodsAllowed = new Vector JavaDoc();
108
109
110     // --------------------------------------------------------- Public Methods
111

112     /**
113      * Get the name.
114      * @return "OPTIONS"
115      * @since 2.0
116      */

117     public String JavaDoc getName() {
118         return "OPTIONS";
119     }
120
121
122     /**
123      * Is the specified method allowed ?
124      *
125      * @param method The method to check.
126      * @return true if the specified method is allowed.
127      * @since 1.0
128      */

129     public boolean isAllowed(String JavaDoc method) {
130         checkUsed();
131         return methodsAllowed.contains(method);
132     }
133
134
135     /**
136      * Get a list of allowed methods.
137      * @return An enumeration of all the allowed methods.
138      *
139      * @since 1.0
140      */

141     public Enumeration JavaDoc getAllowedMethods() {
142         checkUsed();
143         return methodsAllowed.elements();
144     }
145
146
147     // ----------------------------------------------------- HttpMethod Methods
148

149     /**
150      * <p>
151      * This implementation will parse the <tt>Allow</tt> header to obtain
152      * the set of methods supported by the resource identified by the Request-URI.
153      * </p>
154      *
155      * @param state the {@link HttpState state} information associated with this method
156      * @param conn the {@link HttpConnection connection} used to execute
157      * this HTTP method
158      *
159      * @see #readResponse
160      * @see #readResponseHeaders
161      * @since 2.0
162      */

163     protected void processResponseHeaders(HttpState state, HttpConnection conn) {
164         LOG.trace("enter OptionsMethod.processResponseHeaders(HttpState, HttpConnection)");
165
166         Header allowHeader = getResponseHeader("allow");
167         if (allowHeader != null) {
168             String JavaDoc allowHeaderValue = allowHeader.getValue();
169             StringTokenizer JavaDoc tokenizer =
170                 new StringTokenizer JavaDoc(allowHeaderValue, ",");
171             while (tokenizer.hasMoreElements()) {
172                 String JavaDoc methodAllowed =
173                     tokenizer.nextToken().trim().toUpperCase();
174                 methodsAllowed.addElement(methodAllowed);
175             }
176         }
177     }
178
179     /**
180      * Return true if the method needs a content-length header in the request.
181      *
182      * @return true if a content-length header will be expected by the server
183      *
184      * @since 1.0
185      */

186     public boolean needContentLength() {
187         return false;
188     }
189
190
191 }
192
Popular Tags