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