Home | History | Annotate | Download | only in impl
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultHttpRequestFactory.java $
      3  * $Revision: 618367 $
      4  * $Date: 2008-02-04 10:26:06 -0800 (Mon, 04 Feb 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl;
     33 
     34 import org.apache.http.HttpRequest;
     35 import org.apache.http.HttpRequestFactory;
     36 import org.apache.http.MethodNotSupportedException;
     37 import org.apache.http.RequestLine;
     38 import org.apache.http.message.BasicHttpEntityEnclosingRequest;
     39 import org.apache.http.message.BasicHttpRequest;
     40 
     41 /**
     42  * Default implementation of a factory for creating request objects.
     43  *
     44  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     45  *
     46  * @version $Revision: 618367 $
     47  *
     48  * @since 4.0
     49  */
     50 public class DefaultHttpRequestFactory implements HttpRequestFactory {
     51 
     52     private static final String[] RFC2616_COMMON_METHODS = {
     53         "GET"
     54     };
     55 
     56     private static final String[] RFC2616_ENTITY_ENC_METHODS = {
     57         "POST",
     58         "PUT"
     59     };
     60 
     61     private static final String[] RFC2616_SPECIAL_METHODS = {
     62         "HEAD",
     63         "OPTIONS",
     64         "DELETE",
     65         "TRACE"
     66     };
     67 
     68 
     69     public DefaultHttpRequestFactory() {
     70         super();
     71     }
     72 
     73     private static boolean isOneOf(final String[] methods, final String method) {
     74         for (int i = 0; i < methods.length; i++) {
     75             if (methods[i].equalsIgnoreCase(method)) {
     76                 return true;
     77             }
     78         }
     79         return false;
     80     }
     81 
     82     public HttpRequest newHttpRequest(final RequestLine requestline)
     83             throws MethodNotSupportedException {
     84         if (requestline == null) {
     85             throw new IllegalArgumentException("Request line may not be null");
     86         }
     87         String method = requestline.getMethod();
     88         if (isOneOf(RFC2616_COMMON_METHODS, method)) {
     89             return new BasicHttpRequest(requestline);
     90         } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
     91             return new BasicHttpEntityEnclosingRequest(requestline);
     92         } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
     93             return new BasicHttpRequest(requestline);
     94         } else {
     95             throw new MethodNotSupportedException(method +  " method not supported");
     96         }
     97     }
     98 
     99     public HttpRequest newHttpRequest(final String method, final String uri)
    100             throws MethodNotSupportedException {
    101         if (isOneOf(RFC2616_COMMON_METHODS, method)) {
    102             return new BasicHttpRequest(method, uri);
    103         } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
    104             return new BasicHttpEntityEnclosingRequest(method, uri);
    105         } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
    106             return new BasicHttpRequest(method, uri);
    107         } else {
    108             throw new MethodNotSupportedException(method
    109                     + " method not supported");
    110         }
    111     }
    112 
    113 }
    114