001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/DefaultHttpRequestFactory.java $
003: * $Revision: 610772 $
004: * $Date: 2008-01-10 13:26:57 +0100 (Thu, 10 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl;
033:
034: import org.apache.http.HttpRequest;
035: import org.apache.http.HttpRequestFactory;
036: import org.apache.http.MethodNotSupportedException;
037: import org.apache.http.RequestLine;
038: import org.apache.http.message.BasicHttpEntityEnclosingRequest;
039: import org.apache.http.message.BasicHttpRequest;
040:
041: /**
042: * Default implementation of a factory for creating request objects.
043: *
044: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
045: *
046: * @version $Revision: 610772 $
047: *
048: * @since 4.0
049: */
050: public class DefaultHttpRequestFactory implements HttpRequestFactory {
051:
052: private static final String[] RFC2616_COMMON_METHODS = { "GET" };
053:
054: private static final String[] RFC2616_ENTITY_ENC_METHODS = {
055: "POST", "PUT" };
056:
057: private static final String[] RFC2616_SPECIAL_METHODS = { "HEAD",
058: "OPTIONS", "DELETE", "TRACE" };
059:
060: public DefaultHttpRequestFactory() {
061: super ();
062: }
063:
064: private static boolean isOneOf(final String[] methods,
065: final String method) {
066: for (int i = 0; i < methods.length; i++) {
067: if (methods[i].equalsIgnoreCase(method)) {
068: return true;
069: }
070: }
071: return false;
072: }
073:
074: public HttpRequest newHttpRequest(final RequestLine requestline)
075: throws MethodNotSupportedException {
076: if (requestline == null) {
077: throw new IllegalArgumentException(
078: "Request line may not be null");
079: }
080: String method = requestline.getMethod();
081: if (isOneOf(RFC2616_COMMON_METHODS, method)) {
082: return new BasicHttpRequest(requestline);
083: } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
084: return new BasicHttpEntityEnclosingRequest(requestline);
085: } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
086: return new BasicHttpRequest(requestline);
087: } else {
088: throw new MethodNotSupportedException(method
089: + " method not supported");
090: }
091: }
092:
093: public HttpRequest newHttpRequest(final String method,
094: final String uri) throws MethodNotSupportedException {
095: if (isOneOf(RFC2616_COMMON_METHODS, method)) {
096: return new BasicHttpRequest(method, uri);
097: } else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
098: return new BasicHttpEntityEnclosingRequest(method, uri);
099: } else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
100: return new BasicHttpRequest(method, uri);
101: } else {
102: throw new MethodNotSupportedException(method
103: + " method not supported");
104: }
105: }
106:
107: }
|