Skip to content

Commit 1173c21

Browse files
committed
Модуль http использует OkHttp вместо Apache Http
1 parent 8518c8b commit 1173c21

File tree

8 files changed

+79
-87
lines changed

8 files changed

+79
-87
lines changed

libs/commons-codec-1.6.jar

-227 KB
Binary file not shown.

libs/commons-logging-1.1.3.jar

-60.6 KB
Binary file not shown.

libs/httpclient-4.3.5.jar

-577 KB
Binary file not shown.

libs/httpcore-4.3.2.jar

-276 KB
Binary file not shown.

libs/okhttp-3.1.2.jar

326 KB
Binary file not shown.

libs/okio-1.6.0.jar

64.4 KB
Binary file not shown.

nbproject/project.properties

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,15 @@ dist.jar=${dist.dir}/OwnLang.jar
2929
dist.javadoc.dir=${dist.dir}/javadoc
3030
endorsed.classpath=
3131
excludes=
32-
file.reference.commons-codec-1.6.jar=libs/commons-codec-1.6.jar
33-
file.reference.commons-logging-1.1.3.jar=libs/commons-logging-1.1.3.jar
34-
file.reference.httpclient-4.3.5.jar=libs/httpclient-4.3.5.jar
35-
file.reference.httpcore-4.3.2.jar=libs/httpcore-4.3.2.jar
3632
file.reference.json-20151123.jar=libs/json-20151123.jar
33+
file.reference.okhttp-3.1.2.jar=libs\\okhttp-3.1.2.jar
34+
file.reference.okio-1.6.0.jar=libs/okio-1.6.0.jar
3735
includes=**
3836
jar.compress=false
3937
javac.classpath=\
40-
${file.reference.commons-codec-1.6.jar}:\
41-
${file.reference.commons-logging-1.1.3.jar}:\
42-
${file.reference.httpclient-4.3.5.jar}:\
43-
${file.reference.httpcore-4.3.2.jar}:\
44-
${file.reference.json-20151123.jar}
38+
${file.reference.json-20151123.jar}:\
39+
${file.reference.okhttp-3.1.2.jar}:\
40+
${file.reference.okio-1.6.0.jar}
4541
# Space-separated list of extra javac options
4642
javac.compilerargs=
4743
javac.deprecation=false

src/com/annimon/ownlang/lib/modules/functions/http_http.java

Lines changed: 74 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55
import com.annimon.ownlang.lib.*;
66
import java.io.IOException;
77
import java.io.UnsupportedEncodingException;
8-
import java.nio.charset.UnsupportedCharsetException;
9-
import java.util.ArrayList;
108
import java.util.List;
119
import java.util.Map;
12-
import org.apache.http.HttpEntity;
13-
import org.apache.http.HttpResponse;
14-
import org.apache.http.NameValuePair;
15-
import org.apache.http.client.entity.UrlEncodedFormEntity;
16-
import org.apache.http.client.methods.*;
17-
import org.apache.http.entity.StringEntity;
18-
import org.apache.http.impl.client.BasicResponseHandler;
19-
import org.apache.http.impl.client.CloseableHttpClient;
20-
import org.apache.http.impl.client.HttpClientBuilder;
21-
import org.apache.http.message.BasicNameValuePair;
10+
import okhttp3.*;
11+
import okhttp3.internal.http.HttpMethod;
2212

2313
public final class http_http implements Function {
2414

2515
private static final Value
2616
HEADER_KEY = new StringValue("header"),
27-
CHARSET_KEY = new StringValue("charset");
17+
CHARSET_KEY = new StringValue("charset"),
18+
ENCODED_KEY = new StringValue("encoded"),
19+
CONTENT_TYPE = new StringValue("content_type"),
20+
EXTENDED_RESULT = new StringValue("extended_result");
21+
22+
private static final MediaType URLENCODED_MEDIA_TYPE = MediaType.parse("application/x-www-form-urlencoded");
23+
24+
private final OkHttpClient client = new OkHttpClient();
2825

2926
@Override
3027
public Value execute(Value... args) {
@@ -85,91 +82,90 @@ private Value process(String url, String method, Value params, FunctionValue fun
8582
return process(url, method, params, MapValue.EMPTY, function);
8683
}
8784

88-
private Value process(String url, String method, Value requestParams, MapValue options, FunctionValue function) {
85+
private Value process(String url, String methodStr, Value requestParams, MapValue options, FunctionValue function) {
86+
final String method = methodStr.toUpperCase();
8987
final Function callback = function.getValue();
90-
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
91-
HttpRequestBase httpMethod;
92-
switch (method.toUpperCase()) {
93-
case "POST":
94-
httpMethod = new HttpPost(url);
95-
break;
96-
case "PUT":
97-
httpMethod = new HttpPut(url);
98-
break;
99-
case "DELETE":
100-
httpMethod = new HttpDelete(url);
101-
break;
102-
case "PATCH":
103-
httpMethod = new HttpPatch(url);
104-
break;
105-
case "HEAD":
106-
httpMethod = new HttpHead(url);
107-
break;
108-
case "OPTIONS":
109-
httpMethod = new HttpOptions(url);
110-
break;
111-
case "TRACE":
112-
httpMethod = new HttpTrace(url);
113-
break;
114-
case "GET":
115-
default:
116-
httpMethod = new HttpGet(url);
117-
break;
118-
}
119-
88+
try {
89+
final Request.Builder builder = new Request.Builder()
90+
.url(url)
91+
.method(method, getRequestBody(method, requestParams, options));
12092
if (options.containsKey(HEADER_KEY)) {
121-
applyHeaderParams((MapValue) options.get(HEADER_KEY), httpMethod);
93+
applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
12294
}
12395

124-
if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
125-
final HttpEntityEnclosingRequestBase heerb = (HttpEntityEnclosingRequestBase) httpMethod;
126-
if (requestParams.type() == Types.MAP) {
127-
applyMapRequestParams(heerb, (MapValue) requestParams, options);
128-
} else {
129-
applyStringRequestParams(heerb, requestParams, options);
130-
}
131-
}
132-
133-
final HttpResponse httpResponse = httpClient.execute(httpMethod);
134-
final String response = new BasicResponseHandler().handleResponse(httpResponse);
135-
callback.execute(new StringValue(response));
136-
return NumberValue.fromBoolean(true);
96+
final Response response = client.newCall(builder.build()).execute();
97+
callback.execute(getResult(response, options));
98+
return NumberValue.fromBoolean(response.isSuccessful());
13799
} catch (IOException ex) {
138100
return NumberValue.fromBoolean(false);
139101
}
140102
}
103+
104+
private Value getResult(Response response, MapValue options) throws IOException {
105+
if (options.containsKey(EXTENDED_RESULT)) {
106+
final MapValue map = new MapValue(10);
107+
map.set(new StringValue("text"), new StringValue(response.body().string()));
108+
map.set(new StringValue("message"), new StringValue(response.message()));
109+
map.set(new StringValue("code"), new NumberValue(response.code()));
110+
final MapValue headers = new MapValue(response.headers().size());
111+
for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
112+
final int valuesSize = entry.getValue().size();
113+
final ArrayValue values = new ArrayValue(valuesSize);
114+
for (int i = 0; i < valuesSize; i++) {
115+
values.set(i, new StringValue(entry.getValue().get(i)));
116+
}
117+
headers.set(new StringValue(entry.getKey()), values);
118+
}
119+
map.set(new StringValue("headers"), headers);
120+
map.set(new StringValue("content_length"), new NumberValue(response.body().contentLength()));
121+
map.set(CONTENT_TYPE, new StringValue(response.body().contentType().toString()));
122+
return map;
123+
}
124+
return new StringValue(response.body().string());
125+
}
141126

142-
private void applyHeaderParams(MapValue headerParams, HttpRequestBase httpMethod) {
127+
private void applyHeaderParams(MapValue headerParams, Request.Builder builder) {
143128
for (Map.Entry<Value, Value> p : headerParams) {
144-
httpMethod.addHeader(p.getKey().asString(), p.getValue().asString());
129+
builder.header(p.getKey().asString(), p.getValue().asString());
130+
}
131+
}
132+
133+
private RequestBody getRequestBody(String method, Value params, MapValue options) throws UnsupportedEncodingException {
134+
if (!HttpMethod.permitsRequestBody(method)) return null;
135+
136+
if (params.type() == Types.MAP) {
137+
return getMapRequestBody((MapValue) params, options);
145138
}
139+
return getStringRequestBody(params, options);
146140
}
147141

148-
private void applyMapRequestParams(HttpEntityEnclosingRequestBase h, MapValue params, MapValue options)
149-
throws UnsupportedEncodingException {
150-
final List<NameValuePair> entityParams = new ArrayList<>(params.size());
142+
private RequestBody getMapRequestBody(MapValue params, MapValue options) throws UnsupportedEncodingException {
143+
final FormBody.Builder form = new FormBody.Builder();
144+
final boolean alreadyEncoded = (options.containsKey(ENCODED_KEY) && options.get(ENCODED_KEY).asNumber() != 0);
151145
for (Map.Entry<Value, Value> param : params) {
152146
final String name = param.getKey().asString();
153147
final String value = param.getValue().asString();
154-
entityParams.add(new BasicNameValuePair(name, value));
148+
if (alreadyEncoded)
149+
form.addEncoded(name, value);
150+
else
151+
form.add(name, value);
155152
}
156-
HttpEntity entity;
157-
if (options.containsKey(CHARSET_KEY)) {
158-
entity = new UrlEncodedFormEntity(entityParams, options.get(CHARSET_KEY).asString());
159-
} else {
160-
entity = new UrlEncodedFormEntity(entityParams);
161-
}
162-
h.setEntity(entity);
153+
return form.build();
163154
}
164155

165-
private void applyStringRequestParams(final HttpEntityEnclosingRequestBase heerb, Value requestParams, MapValue options) throws UnsupportedEncodingException, UnsupportedCharsetException {
166-
HttpEntity entity;
167-
if (options.containsKey(CHARSET_KEY)) {
168-
entity = new StringEntity(requestParams.asString(), options.get(CHARSET_KEY).asString());
156+
private RequestBody getStringRequestBody(Value params, MapValue options) throws UnsupportedEncodingException {
157+
final MediaType type;
158+
if (options.containsKey(CONTENT_TYPE)) {
159+
type = MediaType.parse(options.get(CONTENT_TYPE).asString());
169160
} else {
170-
entity = new StringEntity(requestParams.asString());
161+
type = URLENCODED_MEDIA_TYPE;
171162
}
172-
heerb.setEntity(entity);
163+
164+
if (options.containsKey(CHARSET_KEY)) {
165+
final String charset = options.get(CHARSET_KEY).asString();
166+
return RequestBody.create(type, params.asString().getBytes(charset));
167+
}
168+
169+
return RequestBody.create(type, params.asString());
173170
}
174-
175171
}

0 commit comments

Comments
 (0)