mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-01 22:55:23 +00:00
107 lines
3.7 KiB
Java
107 lines
3.7 KiB
Java
/*
|
|
* ====================================================================
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
* or more contributor license agreements. See the NOTICE file
|
|
* distributed with this work for additional information
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
* to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
* ====================================================================
|
|
*
|
|
* This software consists of voluntary contributions made by many
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
* information on the Apache Software Foundation, please see
|
|
* <http://www.apache.org/>.
|
|
*
|
|
*/
|
|
|
|
package ch.boye.httpclientandroidlib.impl;
|
|
|
|
import ch.boye.httpclientandroidlib.HttpRequest;
|
|
import ch.boye.httpclientandroidlib.HttpRequestFactory;
|
|
import ch.boye.httpclientandroidlib.MethodNotSupportedException;
|
|
import ch.boye.httpclientandroidlib.RequestLine;
|
|
import ch.boye.httpclientandroidlib.message.BasicHttpEntityEnclosingRequest;
|
|
import ch.boye.httpclientandroidlib.message.BasicHttpRequest;
|
|
|
|
/**
|
|
* Default factory for creating {@link HttpRequest} objects.
|
|
*
|
|
* @since 4.0
|
|
*/
|
|
public class DefaultHttpRequestFactory implements HttpRequestFactory {
|
|
|
|
private static final String[] RFC2616_COMMON_METHODS = {
|
|
"GET"
|
|
};
|
|
|
|
private static final String[] RFC2616_ENTITY_ENC_METHODS = {
|
|
"POST",
|
|
"PUT"
|
|
};
|
|
|
|
private static final String[] RFC2616_SPECIAL_METHODS = {
|
|
"HEAD",
|
|
"OPTIONS",
|
|
"DELETE",
|
|
"TRACE",
|
|
"CONNECT"
|
|
};
|
|
|
|
|
|
public DefaultHttpRequestFactory() {
|
|
super();
|
|
}
|
|
|
|
private static boolean isOneOf(final String[] methods, final String method) {
|
|
for (int i = 0; i < methods.length; i++) {
|
|
if (methods[i].equalsIgnoreCase(method)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public HttpRequest newHttpRequest(final RequestLine requestline)
|
|
throws MethodNotSupportedException {
|
|
if (requestline == null) {
|
|
throw new IllegalArgumentException("Request line may not be null");
|
|
}
|
|
String method = requestline.getMethod();
|
|
if (isOneOf(RFC2616_COMMON_METHODS, method)) {
|
|
return new BasicHttpRequest(requestline);
|
|
} else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
|
|
return new BasicHttpEntityEnclosingRequest(requestline);
|
|
} else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
|
|
return new BasicHttpRequest(requestline);
|
|
} else {
|
|
throw new MethodNotSupportedException(method + " method not supported");
|
|
}
|
|
}
|
|
|
|
public HttpRequest newHttpRequest(final String method, final String uri)
|
|
throws MethodNotSupportedException {
|
|
if (isOneOf(RFC2616_COMMON_METHODS, method)) {
|
|
return new BasicHttpRequest(method, uri);
|
|
} else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
|
|
return new BasicHttpEntityEnclosingRequest(method, uri);
|
|
} else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
|
|
return new BasicHttpRequest(method, uri);
|
|
} else {
|
|
throw new MethodNotSupportedException(method
|
|
+ " method not supported");
|
|
}
|
|
}
|
|
|
|
}
|