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