Модуль http использует OkHttp вместо Apache Http

This commit is contained in:
Victor 2016-02-15 15:32:01 +02:00
parent 8518c8b803
commit 1173c21dfe
8 changed files with 79 additions and 87 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
libs/okhttp-3.1.2.jar Normal file

Binary file not shown.

BIN
libs/okio-1.6.0.jar Normal file

Binary file not shown.

View File

@ -29,19 +29,15 @@ dist.jar=${dist.dir}/OwnLang.jar
dist.javadoc.dir=${dist.dir}/javadoc dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath= endorsed.classpath=
excludes= excludes=
file.reference.commons-codec-1.6.jar=libs/commons-codec-1.6.jar
file.reference.commons-logging-1.1.3.jar=libs/commons-logging-1.1.3.jar
file.reference.httpclient-4.3.5.jar=libs/httpclient-4.3.5.jar
file.reference.httpcore-4.3.2.jar=libs/httpcore-4.3.2.jar
file.reference.json-20151123.jar=libs/json-20151123.jar file.reference.json-20151123.jar=libs/json-20151123.jar
file.reference.okhttp-3.1.2.jar=libs\\okhttp-3.1.2.jar
file.reference.okio-1.6.0.jar=libs/okio-1.6.0.jar
includes=** includes=**
jar.compress=false jar.compress=false
javac.classpath=\ javac.classpath=\
${file.reference.commons-codec-1.6.jar}:\ ${file.reference.json-20151123.jar}:\
${file.reference.commons-logging-1.1.3.jar}:\ ${file.reference.okhttp-3.1.2.jar}:\
${file.reference.httpclient-4.3.5.jar}:\ ${file.reference.okio-1.6.0.jar}
${file.reference.httpcore-4.3.2.jar}:\
${file.reference.json-20151123.jar}
# Space-separated list of extra javac options # Space-separated list of extra javac options
javac.compilerargs= javac.compilerargs=
javac.deprecation=false javac.deprecation=false

View File

@ -5,26 +5,23 @@ import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.*; import com.annimon.ownlang.lib.*;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.http.HttpEntity; import okhttp3.*;
import org.apache.http.HttpResponse; import okhttp3.internal.http.HttpMethod;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
public final class http_http implements Function { public final class http_http implements Function {
private static final Value private static final Value
HEADER_KEY = new StringValue("header"), HEADER_KEY = new StringValue("header"),
CHARSET_KEY = new StringValue("charset"); CHARSET_KEY = new StringValue("charset"),
ENCODED_KEY = new StringValue("encoded"),
CONTENT_TYPE = new StringValue("content_type"),
EXTENDED_RESULT = new StringValue("extended_result");
private static final MediaType URLENCODED_MEDIA_TYPE = MediaType.parse("application/x-www-form-urlencoded");
private final OkHttpClient client = new OkHttpClient();
@Override @Override
public Value execute(Value... args) { public Value execute(Value... args) {
@ -85,91 +82,90 @@ public final class http_http implements Function {
return process(url, method, params, MapValue.EMPTY, function); return process(url, method, params, MapValue.EMPTY, function);
} }
private Value process(String url, String method, Value requestParams, MapValue options, FunctionValue function) { private Value process(String url, String methodStr, Value requestParams, MapValue options, FunctionValue function) {
final String method = methodStr.toUpperCase();
final Function callback = function.getValue(); final Function callback = function.getValue();
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try {
HttpRequestBase httpMethod; final Request.Builder builder = new Request.Builder()
switch (method.toUpperCase()) { .url(url)
case "POST": .method(method, getRequestBody(method, requestParams, options));
httpMethod = new HttpPost(url);
break;
case "PUT":
httpMethod = new HttpPut(url);
break;
case "DELETE":
httpMethod = new HttpDelete(url);
break;
case "PATCH":
httpMethod = new HttpPatch(url);
break;
case "HEAD":
httpMethod = new HttpHead(url);
break;
case "OPTIONS":
httpMethod = new HttpOptions(url);
break;
case "TRACE":
httpMethod = new HttpTrace(url);
break;
case "GET":
default:
httpMethod = new HttpGet(url);
break;
}
if (options.containsKey(HEADER_KEY)) { if (options.containsKey(HEADER_KEY)) {
applyHeaderParams((MapValue) options.get(HEADER_KEY), httpMethod); applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
} }
if (httpMethod instanceof HttpEntityEnclosingRequestBase) { final Response response = client.newCall(builder.build()).execute();
final HttpEntityEnclosingRequestBase heerb = (HttpEntityEnclosingRequestBase) httpMethod; callback.execute(getResult(response, options));
if (requestParams.type() == Types.MAP) { return NumberValue.fromBoolean(response.isSuccessful());
applyMapRequestParams(heerb, (MapValue) requestParams, options);
} else {
applyStringRequestParams(heerb, requestParams, options);
}
}
final HttpResponse httpResponse = httpClient.execute(httpMethod);
final String response = new BasicResponseHandler().handleResponse(httpResponse);
callback.execute(new StringValue(response));
return NumberValue.fromBoolean(true);
} catch (IOException ex) { } catch (IOException ex) {
return NumberValue.fromBoolean(false); return NumberValue.fromBoolean(false);
} }
} }
private Value getResult(Response response, MapValue options) throws IOException {
if (options.containsKey(EXTENDED_RESULT)) {
final MapValue map = new MapValue(10);
map.set(new StringValue("text"), new StringValue(response.body().string()));
map.set(new StringValue("message"), new StringValue(response.message()));
map.set(new StringValue("code"), new NumberValue(response.code()));
final MapValue headers = new MapValue(response.headers().size());
for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
final int valuesSize = entry.getValue().size();
final ArrayValue values = new ArrayValue(valuesSize);
for (int i = 0; i < valuesSize; i++) {
values.set(i, new StringValue(entry.getValue().get(i)));
}
headers.set(new StringValue(entry.getKey()), values);
}
map.set(new StringValue("headers"), headers);
map.set(new StringValue("content_length"), new NumberValue(response.body().contentLength()));
map.set(CONTENT_TYPE, new StringValue(response.body().contentType().toString()));
return map;
}
return new StringValue(response.body().string());
}
private void applyHeaderParams(MapValue headerParams, HttpRequestBase httpMethod) { private void applyHeaderParams(MapValue headerParams, Request.Builder builder) {
for (Map.Entry<Value, Value> p : headerParams) { for (Map.Entry<Value, Value> p : headerParams) {
httpMethod.addHeader(p.getKey().asString(), p.getValue().asString()); builder.header(p.getKey().asString(), p.getValue().asString());
} }
} }
private void applyMapRequestParams(HttpEntityEnclosingRequestBase h, MapValue params, MapValue options) private RequestBody getRequestBody(String method, Value params, MapValue options) throws UnsupportedEncodingException {
throws UnsupportedEncodingException { if (!HttpMethod.permitsRequestBody(method)) return null;
final List<NameValuePair> entityParams = new ArrayList<>(params.size());
if (params.type() == Types.MAP) {
return getMapRequestBody((MapValue) params, options);
}
return getStringRequestBody(params, options);
}
private RequestBody getMapRequestBody(MapValue params, MapValue options) throws UnsupportedEncodingException {
final FormBody.Builder form = new FormBody.Builder();
final boolean alreadyEncoded = (options.containsKey(ENCODED_KEY) && options.get(ENCODED_KEY).asNumber() != 0);
for (Map.Entry<Value, Value> param : params) { for (Map.Entry<Value, Value> param : params) {
final String name = param.getKey().asString(); final String name = param.getKey().asString();
final String value = param.getValue().asString(); final String value = param.getValue().asString();
entityParams.add(new BasicNameValuePair(name, value)); if (alreadyEncoded)
form.addEncoded(name, value);
else
form.add(name, value);
} }
HttpEntity entity; return form.build();
if (options.containsKey(CHARSET_KEY)) {
entity = new UrlEncodedFormEntity(entityParams, options.get(CHARSET_KEY).asString());
} else {
entity = new UrlEncodedFormEntity(entityParams);
}
h.setEntity(entity);
} }
private void applyStringRequestParams(final HttpEntityEnclosingRequestBase heerb, Value requestParams, MapValue options) throws UnsupportedEncodingException, UnsupportedCharsetException { private RequestBody getStringRequestBody(Value params, MapValue options) throws UnsupportedEncodingException {
HttpEntity entity; final MediaType type;
if (options.containsKey(CHARSET_KEY)) { if (options.containsKey(CONTENT_TYPE)) {
entity = new StringEntity(requestParams.asString(), options.get(CHARSET_KEY).asString()); type = MediaType.parse(options.get(CONTENT_TYPE).asString());
} else { } else {
entity = new StringEntity(requestParams.asString()); type = URLENCODED_MEDIA_TYPE;
} }
heerb.setEntity(entity);
if (options.containsKey(CHARSET_KEY)) {
final String charset = options.get(CHARSET_KEY).asString();
return RequestBody.create(type, params.asString().getBytes(charset));
}
return RequestBody.create(type, params.asString());
} }
} }