Skip to content

Commit

Permalink
Merge pull request #22 from rchodava/rc/remove-vertx-add-netty
Browse files Browse the repository at this point in the history
Remove vert.x, and replace it with direct usage of Netty, and org.json
  • Loading branch information
rchodava committed Apr 10, 2016
2 parents 3779152 + acd78c4 commit de42242
Show file tree
Hide file tree
Showing 28 changed files with 964 additions and 335 deletions.
62 changes: 44 additions & 18 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,34 @@
<version>19.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.2.1</version>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java</artifactId>
<version>3.2.1</version>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>4.1.0.CR6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.1.0.CR6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.0.CR6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.0.CR6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<version>4.1.0.CR6</version>
</dependency>
<dependency>
<groupId>com.github.davidmoten</groupId>
Expand Down Expand Up @@ -68,18 +88,6 @@
<artifactId>objenesis</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
Expand All @@ -95,5 +103,23 @@
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
46 changes: 41 additions & 5 deletions core/src/main/java/org/chodavarapu/datamill/http/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.chodavarapu.datamill.http;

import com.google.common.base.Joiner;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import org.chodavarapu.datamill.http.impl.*;
import org.chodavarapu.datamill.values.Value;
import org.slf4j.Logger;
Expand All @@ -12,10 +14,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -29,7 +34,8 @@ public class Client {

public Observable<Response> request(Function<RequestBuilder, Request> builder) {
Request request = builder.apply(new RequestBuilderImpl());
return request(request.method(), request.headers(), request.uri(), request.uriParameters(), request.options(), request.entity());
return request(request.method(), request.headers(), request.uri(), request.uriParameters(),
request.queryParameters(), request.options(), request.entity());
}

public Observable<Response> request(Method method, Map<String, String> headers, String uri, Value entity) {
Expand All @@ -41,20 +47,23 @@ protected URLConnection createConnection(String uri) throws IOException {
}

public Observable<Response> request(Method method, Map<String, String> headers, String uri, Entity entity) {
return request(method, headers, uri, null, null, entity);
return request(method, headers != null ? Multimaps.forMap(headers) : null, uri, null, null, null, entity);
}

public Observable<Response> request(
Method method,
Map<String, String> headers,
Multimap<String, String> headers,
String uri,
Map<String, String> uriParameters,
Multimap<String, String> queryParameters,
Map<String, ?> options,
Entity entity) {
if (uriParameters != null && uriParameters.size() > 0) {
uri = uriBuilder.build(uri, uriParameters);
}

uri = appendQueryParameters(uri, queryParameters);

final String composedUri = uri;

return Async.fromCallable(() -> {
Expand All @@ -71,7 +80,7 @@ public Observable<Response> request(
}

if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
for (Map.Entry<String, String> header : headers.entries()) {
httpConnection.addRequestProperty(header.getKey(), header.getValue());
}
}
Expand All @@ -83,7 +92,7 @@ public Observable<Response> request(
logger.debug("Making HTTP request {} {}", method.name(), composedUri);
if (headers != null && logger.isDebugEnabled()) {
logger.debug(" HTTP request headers:");
for (Map.Entry<String, String> header : headers.entrySet()) {
for (Map.Entry<String, String> header : headers.entries()) {
logger.debug(" {}: {}", header.getKey(), header.getValue());
}
}
Expand All @@ -105,6 +114,33 @@ public Observable<Response> request(
}, Schedulers.io());
}

private String appendQueryParameters(String uri, Multimap<String, String> queryParameters) {
if (queryParameters != null && queryParameters.size() > 0) {
try {
StringBuilder queryBuilder = new StringBuilder("?");
Iterator<Map.Entry<String, String>> iterator = queryParameters.entries().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> parameter = iterator.next();

queryBuilder.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
queryBuilder.append('=');

if (parameter.getValue() != null) {
queryBuilder.append(URLEncoder.encode(parameter.getValue()));
}

if (iterator.hasNext()) {
queryBuilder.append('&');
}
}

uri = uri + queryBuilder.toString();
} catch (UnsupportedEncodingException e) {
}
}
return uri;
}

private void writeEntityOutOverConnection(Entity entity, HttpURLConnection httpConnection) throws IOException {
httpConnection.setDoOutput(true);
OutputStream outputStream = httpConnection.getOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public enum Method {
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PATCH;
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PATCH, UNKNOWN;

private static final Set<Method> methods =
EnumSet.allOf(Method.class);
Expand Down
14 changes: 10 additions & 4 deletions core/src/main/java/org/chodavarapu/datamill/http/Request.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.chodavarapu.datamill.http;

import com.google.common.collect.Multimap;
import org.chodavarapu.datamill.values.Value;

import java.util.Map;
import java.util.Optional;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
Expand All @@ -13,16 +13,22 @@ public interface Request {

Entity entity();

Map<String, String> headers();
Multimap<String, String> headers();

Optional<Value> header(String header);
Value firstHeader(String header);

Optional<Value> header(RequestHeader header);
Value firstHeader(RequestHeader header);

Value firstQueryParameter(String name);

Method method();

Map<String, Object> options();

Multimap<String, String> queryParameters();

String rawMethod();

String uri();

Value uriParameter(String parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface RequestBuilder {
RequestBuilder header(RequestHeader header, String value);
RequestBuilder method(Method method);
RequestBuilder method(String method);
RequestBuilder queryParameter(String name, String value);
RequestBuilder uri(String uri);
<T> RequestBuilder uriParameter(String name, T value);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.chodavarapu.datamill.http;

import java.util.Map;
import com.google.common.collect.Multimap;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public interface Response {
Entity entity();
Map<String, String> headers();
Multimap<String, String> headers();
Status status();
}

0 comments on commit de42242

Please sign in to comment.