001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.methods;
032:
033: import org.apache.commons.httpclient.Header;
034: import org.apache.commons.httpclient.HttpConnection;
035: import org.apache.commons.httpclient.HttpMethodBase;
036: import org.apache.commons.httpclient.HttpState;
037:
038: import org.apache.commons.logging.LogFactory;
039: import org.apache.commons.logging.Log;
040: import java.util.Enumeration;
041: import java.util.StringTokenizer;
042: import java.util.Vector;
043:
044: /**
045: * Implements the HTTP OPTIONS method.
046: * <p>
047: * The HTTP OPTIONS method is defined in section 9.2 of
048: * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
049: * <blockquote>
050: * The OPTIONS method represents a request for information about the
051: * communication options available on the request/response chain
052: * identified by the Request-URI. This method allows the client to
053: * determine the options and/or requirements associated with a resource,
054: * or the capabilities of a server, without implying a resource action
055: * or initiating a resource retrieval.
056: * </blockquote>
057: * </p>
058: *
059: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
060: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
061: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
062: *
063: * @version $Revision: 480424 $
064: * @since 1.0
065: */
066: public class OptionsMethod extends HttpMethodBase {
067:
068: // --------------------------------------------------------- Class Variables
069:
070: /** Log object for this class. */
071: private static final Log LOG = LogFactory
072: .getLog(OptionsMethod.class);
073:
074: // ----------------------------------------------------------- Constructors
075:
076: /**
077: * Method constructor.
078: *
079: * @since 1.0
080: */
081: public OptionsMethod() {
082: }
083:
084: /**
085: * Constructor specifying a URI.
086: *
087: * @param uri either an absolute or relative URI
088: *
089: * @since 1.0
090: */
091: public OptionsMethod(String uri) {
092: super (uri);
093: }
094:
095: // ----------------------------------------------------- Instance Variables
096:
097: /**
098: * Methods allowed.
099: */
100: private Vector methodsAllowed = new Vector();
101:
102: // --------------------------------------------------------- Public Methods
103:
104: /**
105: * Get the name.
106: * @return "OPTIONS"
107: * @since 2.0
108: */
109: public String getName() {
110: return "OPTIONS";
111: }
112:
113: /**
114: * Is the specified method allowed ?
115: *
116: * @param method The method to check.
117: * @return true if the specified method is allowed.
118: * @since 1.0
119: */
120: public boolean isAllowed(String method) {
121: checkUsed();
122: return methodsAllowed.contains(method);
123: }
124:
125: /**
126: * Get a list of allowed methods.
127: * @return An enumeration of all the allowed methods.
128: *
129: * @since 1.0
130: */
131: public Enumeration getAllowedMethods() {
132: checkUsed();
133: return methodsAllowed.elements();
134: }
135:
136: // ----------------------------------------------------- HttpMethod Methods
137:
138: /**
139: * <p>
140: * This implementation will parse the <tt>Allow</tt> header to obtain
141: * the set of methods supported by the resource identified by the Request-URI.
142: * </p>
143: *
144: * @param state the {@link HttpState state} information associated with this method
145: * @param conn the {@link HttpConnection connection} used to execute
146: * this HTTP method
147: *
148: * @see #readResponse
149: * @see #readResponseHeaders
150: * @since 2.0
151: */
152: protected void processResponseHeaders(HttpState state,
153: HttpConnection conn) {
154: LOG
155: .trace("enter OptionsMethod.processResponseHeaders(HttpState, HttpConnection)");
156:
157: Header allowHeader = getResponseHeader("allow");
158: if (allowHeader != null) {
159: String allowHeaderValue = allowHeader.getValue();
160: StringTokenizer tokenizer = new StringTokenizer(
161: allowHeaderValue, ",");
162: while (tokenizer.hasMoreElements()) {
163: String methodAllowed = tokenizer.nextToken().trim()
164: .toUpperCase();
165: methodsAllowed.addElement(methodAllowed);
166: }
167: }
168: }
169:
170: /**
171: * Return true if the method needs a content-length header in the request.
172: *
173: * @return true if a content-length header will be expected by the server
174: *
175: * @since 1.0
176: *
177: * @deprecated only entity enclosing methods set content length header
178: */
179: public boolean needContentLength() {
180: return false;
181: }
182:
183: }
|