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.annotation.NotThreadSafe;
39  import org.apache.http.util.Args;
40  
41  /**
42   * HTTP OPTIONS method.
43   * <p>
44   * The HTTP OPTIONS method is defined in section 9.2 of
45   * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
46   * </p>
47   * <blockquote>
48   *  The OPTIONS method represents a request for information about the
49   *  communication options available on the request/response chain
50   *  identified by the Request-URI. This method allows the client to
51   *  determine the options and/or requirements associated with a resource,
52   *  or the capabilities of a server, without implying a resource action
53   *  or initiating a resource retrieval.
54   * </blockquote>
55   *
56   * @since 4.0
57   */
58  @NotThreadSafe
59  public class HttpOptions extends HttpRequestBase {
60  
61      public final static String METHOD_NAME = "OPTIONS";
62  
63      public HttpOptions() {
64          super();
65      }
66  
67      public HttpOptions(final URI uri) {
68          super();
69          setURI(uri);
70      }
71  
72      /**
73       * @throws IllegalArgumentException if the uri is invalid.
74       */
75      public HttpOptions(final String uri) {
76          super();
77          setURI(URI.create(uri));
78      }
79  
80      @Override
81      public String getMethod() {
82          return METHOD_NAME;
83      }
84  
85      public Set<String> getAllowedMethods(final HttpResponse response) {
86          Args.notNull(response, "HTTP response");
87  
88          final HeaderIterator it = response.headerIterator("Allow");
89          final Set<String> methods = new HashSet<String>();
90          while (it.hasNext()) {
91              final Header header = it.nextHeader();
92              final HeaderElement[] elements = header.getElements();
93              for (final HeaderElement element : elements) {
94                  methods.add(element.getName());
95              }
96          }
97          return methods;
98      }
99  
100 }