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