001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.client.methods;
029
030import java.net.URI;
031import java.util.HashSet;
032import java.util.Set;
033
034import org.apache.http.Header;
035import org.apache.http.HeaderElement;
036import org.apache.http.HeaderIterator;
037import org.apache.http.HttpResponse;
038import org.apache.http.util.Args;
039
040/**
041 * HTTP OPTIONS method.
042 * <p>
043 * The HTTP OPTIONS method is defined in section 9.2 of
044 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
045 * </p>
046 * <blockquote>
047 *  The OPTIONS method represents a request for information about the
048 *  communication options available on the request/response chain
049 *  identified by the Request-URI. This method allows the client to
050 *  determine the options and/or requirements associated with a resource,
051 *  or the capabilities of a server, without implying a resource action
052 *  or initiating a resource retrieval.
053 * </blockquote>
054 *
055 * @since 4.0
056 */
057public class HttpOptions extends HttpRequestBase {
058
059    public final static String METHOD_NAME = "OPTIONS";
060
061    public HttpOptions() {
062        super();
063    }
064
065    public HttpOptions(final URI uri) {
066        super();
067        setURI(uri);
068    }
069
070    /**
071     * @throws IllegalArgumentException if the uri is invalid.
072     */
073    public HttpOptions(final String uri) {
074        super();
075        setURI(URI.create(uri));
076    }
077
078    @Override
079    public String getMethod() {
080        return METHOD_NAME;
081    }
082
083    public Set<String> getAllowedMethods(final HttpResponse response) {
084        Args.notNull(response, "HTTP response");
085
086        final HeaderIterator it = response.headerIterator("Allow");
087        final Set<String> methods = new HashSet<String>();
088        while (it.hasNext()) {
089            final Header header = it.nextHeader();
090            final HeaderElement[] elements = header.getElements();
091            for (final HeaderElement element : elements) {
092                methods.add(element.getName());
093            }
094        }
095        return methods;
096    }
097
098}