001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl; 029 030import org.apache.http.HttpRequest; 031import org.apache.http.HttpRequestFactory; 032import org.apache.http.MethodNotSupportedException; 033import org.apache.http.RequestLine; 034import org.apache.http.annotation.ThreadingBehavior; 035import org.apache.http.annotation.Contract; 036import org.apache.http.message.BasicHttpEntityEnclosingRequest; 037import org.apache.http.message.BasicHttpRequest; 038import org.apache.http.util.Args; 039 040/** 041 * Default factory for creating {@link HttpRequest} objects. 042 * 043 * @since 4.0 044 */ 045@Contract(threading = ThreadingBehavior.IMMUTABLE) 046public class DefaultHttpRequestFactory implements HttpRequestFactory { 047 048 public static final DefaultHttpRequestFactory INSTANCE = new DefaultHttpRequestFactory(); 049 050 private static final String[] RFC2616_COMMON_METHODS = { 051 "GET" 052 }; 053 054 private static final String[] RFC2616_ENTITY_ENC_METHODS = { 055 "POST", 056 "PUT" 057 }; 058 059 private static final String[] RFC2616_SPECIAL_METHODS = { 060 "HEAD", 061 "OPTIONS", 062 "DELETE", 063 "TRACE", 064 "CONNECT" 065 }; 066 067 private static final String[] RFC5789_ENTITY_ENC_METHODS = { 068 "PATCH" 069 }; 070 071 public DefaultHttpRequestFactory() { 072 super(); 073 } 074 075 private static boolean isOneOf(final String[] methods, final String method) { 076 for (final String method2 : methods) { 077 if (method2.equalsIgnoreCase(method)) { 078 return true; 079 } 080 } 081 return false; 082 } 083 084 @Override 085 public HttpRequest newHttpRequest(final RequestLine requestline) 086 throws MethodNotSupportedException { 087 Args.notNull(requestline, "Request line"); 088 final String method = requestline.getMethod(); 089 if (isOneOf(RFC2616_COMMON_METHODS, method)) { 090 return new BasicHttpRequest(requestline); 091 } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) { 092 return new BasicHttpEntityEnclosingRequest(requestline); 093 } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) { 094 return new BasicHttpRequest(requestline); 095 } else if (isOneOf(RFC5789_ENTITY_ENC_METHODS, method)) { 096 return new BasicHttpEntityEnclosingRequest(requestline); 097 } else { 098 throw new MethodNotSupportedException(method + " method not supported"); 099 } 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}