|
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 ch.boye.httpclientandroidlib.client.methods; |
|
29 |
|
30 import java.net.URI; |
|
31 import java.util.HashSet; |
|
32 import java.util.Set; |
|
33 |
|
34 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
|
35 |
|
36 import ch.boye.httpclientandroidlib.Header; |
|
37 import ch.boye.httpclientandroidlib.HeaderElement; |
|
38 import ch.boye.httpclientandroidlib.HeaderIterator; |
|
39 import ch.boye.httpclientandroidlib.HttpResponse; |
|
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 * <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 * </p> |
|
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 if (response == null) { |
|
87 throw new IllegalArgumentException("HTTP response may not be null"); |
|
88 } |
|
89 |
|
90 HeaderIterator it = response.headerIterator("Allow"); |
|
91 Set<String> methods = new HashSet<String>(); |
|
92 while (it.hasNext()) { |
|
93 Header header = it.nextHeader(); |
|
94 HeaderElement[] elements = header.getElements(); |
|
95 for (HeaderElement element : elements) { |
|
96 methods.add(element.getName()); |
|
97 } |
|
98 } |
|
99 return methods; |
|
100 } |
|
101 |
|
102 } |