|
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.impl; |
|
29 |
|
30 import ch.boye.httpclientandroidlib.HttpRequest; |
|
31 import ch.boye.httpclientandroidlib.HttpRequestFactory; |
|
32 import ch.boye.httpclientandroidlib.MethodNotSupportedException; |
|
33 import ch.boye.httpclientandroidlib.RequestLine; |
|
34 import ch.boye.httpclientandroidlib.message.BasicHttpEntityEnclosingRequest; |
|
35 import ch.boye.httpclientandroidlib.message.BasicHttpRequest; |
|
36 |
|
37 /** |
|
38 * Default factory for creating {@link HttpRequest} objects. |
|
39 * |
|
40 * @since 4.0 |
|
41 */ |
|
42 public class DefaultHttpRequestFactory implements HttpRequestFactory { |
|
43 |
|
44 private static final String[] RFC2616_COMMON_METHODS = { |
|
45 "GET" |
|
46 }; |
|
47 |
|
48 private static final String[] RFC2616_ENTITY_ENC_METHODS = { |
|
49 "POST", |
|
50 "PUT" |
|
51 }; |
|
52 |
|
53 private static final String[] RFC2616_SPECIAL_METHODS = { |
|
54 "HEAD", |
|
55 "OPTIONS", |
|
56 "DELETE", |
|
57 "TRACE", |
|
58 "CONNECT" |
|
59 }; |
|
60 |
|
61 |
|
62 public DefaultHttpRequestFactory() { |
|
63 super(); |
|
64 } |
|
65 |
|
66 private static boolean isOneOf(final String[] methods, final String method) { |
|
67 for (int i = 0; i < methods.length; i++) { |
|
68 if (methods[i].equalsIgnoreCase(method)) { |
|
69 return true; |
|
70 } |
|
71 } |
|
72 return false; |
|
73 } |
|
74 |
|
75 public HttpRequest newHttpRequest(final RequestLine requestline) |
|
76 throws MethodNotSupportedException { |
|
77 if (requestline == null) { |
|
78 throw new IllegalArgumentException("Request line may not be null"); |
|
79 } |
|
80 String method = requestline.getMethod(); |
|
81 if (isOneOf(RFC2616_COMMON_METHODS, method)) { |
|
82 return new BasicHttpRequest(requestline); |
|
83 } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) { |
|
84 return new BasicHttpEntityEnclosingRequest(requestline); |
|
85 } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) { |
|
86 return new BasicHttpRequest(requestline); |
|
87 } else { |
|
88 throw new MethodNotSupportedException(method + " method not supported"); |
|
89 } |
|
90 } |
|
91 |
|
92 public HttpRequest newHttpRequest(final String method, final String uri) |
|
93 throws MethodNotSupportedException { |
|
94 if (isOneOf(RFC2616_COMMON_METHODS, method)) { |
|
95 return new BasicHttpRequest(method, uri); |
|
96 } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) { |
|
97 return new BasicHttpEntityEnclosingRequest(method, uri); |
|
98 } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) { |
|
99 return new BasicHttpRequest(method, uri); |
|
100 } else { |
|
101 throw new MethodNotSupportedException(method |
|
102 + " method not supported"); |
|
103 } |
|
104 } |
|
105 |
|
106 } |