View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.client.methods;
29  
30  import java.net.URI;
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.HeaderIterator;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.util.Args;
39  
40  /**
41   * HTTP OPTIONS method.
42   * <p>
43   * The HTTP OPTIONS method is defined in section 9.2 of
44   * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
45   * </p>
46   * <blockquote>
47   *  The OPTIONS method represents a request for information about the
48   *  communication options available on the request/response chain
49   *  identified by the Request-URI. This method allows the client to
50   *  determine the options and/or requirements associated with a resource,
51   *  or the capabilities of a server, without implying a resource action
52   *  or initiating a resource retrieval.
53   * </blockquote>
54   *
55   * @since 4.0
56   */
57  public class HttpOptions extends HttpRequestBase {
58  
59      public final static String METHOD_NAME = "OPTIONS";
60  
61      public HttpOptions() {
62          super();
63      }
64  
65      public HttpOptions(final URI uri) {
66          super();
67          setURI(uri);
68      }
69  
70      /**
71       * @throws IllegalArgumentException if the uri is invalid.
72       */
73      public HttpOptions(final String uri) {
74          super();
75          setURI(URI.create(uri));
76      }
77  
78      @Override
79      public String getMethod() {
80          return METHOD_NAME;
81      }
82  
83      public Set<String> getAllowedMethods(final HttpResponse response) {
84          Args.notNull(response, "HTTP response");
85  
86          final HeaderIterator it = response.headerIterator("Allow");
87          final Set<String> methods = new HashSet<String>();
88          while (it.hasNext()) {
89              final Header header = it.nextHeader();
90              final HeaderElement[] elements = header.getElements();
91              for (final HeaderElement element : elements) {
92                  methods.add(element.getName());
93              }
94          }
95          return methods;
96      }
97  
98  }