Skip to content

Commit

Permalink
Improved HTTP client API: configurable content type for raw data post.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 21, 2015
1 parent b1aacd0 commit c7d0493
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
Expand Up @@ -76,7 +76,7 @@ public void run() {
HTTPServer server = WebServer.create().applications(WebAppGroup.main()).build().start(); HTTPServer server = WebServer.create().applications(WebAppGroup.main()).build().start();


eq(new String(HTTP.get("http://localhost:8888/")), "OK"); eq(new String(HTTP.get("http://localhost:8888/")), "OK");
eq(new String(HTTP.post("http://localhost:8888/")), "OK"); eq(new String(HTTP.post("http://localhost:8888/", null, new byte[0], null)), "OK");


server.shutdown(); server.shutdown();
Ctxs.close(); Ctxs.close();
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void testHttpServer() {
HTTPServer server = WebServer.create().applications(WebAppGroup.main()).build().start(); HTTPServer server = WebServer.create().applications(WebAppGroup.main()).build().start();


eq(new String(HTTP.get("http://localhost:8888/")), "home"); eq(new String(HTTP.get("http://localhost:8888/")), "home");
eq(new String(HTTP.post("http://localhost:8888/")), "\"abc\""); eq(new String(HTTP.post("http://localhost:8888/", null, new byte[0], null)), "\"abc\"");


server.shutdown(); server.shutdown();
Ctxs.close(); Ctxs.close();
Expand Down
38 changes: 22 additions & 16 deletions rapidoid-rest/src/main/java/org/rapidoid/http/HTTP.java
Expand Up @@ -31,8 +31,26 @@
@Since("2.0.0") @Since("2.0.0")
public class HTTP { public class HTTP {


public static final String CONTENT_TYPE_MULTIPART = "multipart/form-data";
public static final String CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String CONTENT_TYPE_JSON = "application/json";
public static final String CONTENT_TYPE_XML = "application/xml";
public static final String CONTENT_TYPE_BINARY = "application/octet-stream";

public static final HttpClient DEFAULT_CLIENT = new HttpClient(); public static final HttpClient DEFAULT_CLIENT = new HttpClient();


/********************************** GET **********************************/

public static Future<byte[]> get(String uri, Callback<byte[]> callback) {
return DEFAULT_CLIENT.get(uri, callback);
}

public static byte[] get(String uri) {
return get(uri, null).get();
}

/********************************** POST **********************************/

public static Future<byte[]> post(String uri, Map<String, String> headers, Map<String, String> data, public static Future<byte[]> post(String uri, Map<String, String> headers, Map<String, String> data,
Map<String, String> files, Callback<byte[]> callback) { Map<String, String> files, Callback<byte[]> callback) {
return DEFAULT_CLIENT.post(uri, headers, data, files, callback); return DEFAULT_CLIENT.post(uri, headers, data, files, callback);
Expand All @@ -43,25 +61,13 @@ public static byte[] post(String uri, Map<String, String> headers, Map<String, S
return post(uri, headers, data, files, null).get(); return post(uri, headers, data, files, null).get();
} }


public static Future<byte[]> post(String uri, Map<String, String> headers, String postData, public static Future<byte[]> post(String uri, Map<String, String> headers, byte[] postData, String contentType,
Callback<byte[]> callback) { Callback<byte[]> callback) {
return DEFAULT_CLIENT.post(uri, headers, postData, callback); return DEFAULT_CLIENT.post(uri, headers, postData, contentType, callback);
} }


public static byte[] post(String uri, Map<String, String> headers, String postData) { public static byte[] post(String uri, Map<String, String> headers, byte[] postData, String contentType) {
return post(uri, headers, postData, null).get(); return post(uri, headers, postData, contentType, null).get();
}

public static byte[] post(String uri) {
return post(uri, null, null, null, null).get();
}

public static Future<byte[]> get(String uri, Callback<byte[]> callback) {
return DEFAULT_CLIENT.get(uri, callback);
}

public static byte[] get(String uri) {
return get(uri, null).get();
} }


} }
15 changes: 10 additions & 5 deletions rapidoid-rest/src/main/java/org/rapidoid/http/HttpClient.java
Expand Up @@ -72,21 +72,26 @@ public HttpClient(CloseableHttpAsyncClient client) {
client.start(); client.start();
} }


public Future<byte[]> post(String uri, Map<String, String> headers, String postData, Callback<byte[]> callback) { public Future<byte[]> post(String uri, Map<String, String> headers, byte[] postData, String contentType,
Callback<byte[]> callback) {


headers = U.safe(headers); headers = U.safe(headers);


HttpPost req = new HttpPost(uri); HttpPost req = new HttpPost(uri);


NByteArrayEntity entity = new NByteArrayEntity(postData.getBytes(), ContentType.APPLICATION_FORM_URLENCODED); NByteArrayEntity entity = new NByteArrayEntity(postData);

if (contentType != null) {
entity.setContentType(contentType);
}


for (Entry<String, String> e : headers.entrySet()) { for (Entry<String, String> e : headers.entrySet()) {
req.addHeader(e.getKey(), e.getValue()); req.addHeader(e.getKey(), e.getValue());
} }


req.setEntity(entity); req.setEntity(entity);


Log.info("Starting HTTP POST request", "request", req.getRequestLine()); Log.debug("Starting HTTP POST request", "request", req.getRequestLine());


return execute(client, req, callback); return execute(client, req, callback);
} }
Expand Down Expand Up @@ -128,15 +133,15 @@ public Future<byte[]> post(String uri, Map<String, String> headers, Map<String,


req.setEntity(entity); req.setEntity(entity);


Log.info("Starting HTTP POST request", "request", req.getRequestLine()); Log.debug("Starting HTTP POST request", "request", req.getRequestLine());


return execute(client, req, callback); return execute(client, req, callback);
} }


public Future<byte[]> get(String uri, Callback<byte[]> callback) { public Future<byte[]> get(String uri, Callback<byte[]> callback) {
HttpGet req = new HttpGet(uri); HttpGet req = new HttpGet(uri);


Log.info("Starting HTTP GET request", "request", req.getRequestLine()); Log.debug("Starting HTTP GET request", "request", req.getRequestLine());


return execute(client, req, callback); return execute(client, req, callback);
} }
Expand Down
Expand Up @@ -44,7 +44,7 @@ public <T> T get(String uri, Class<T> resultType) {
public <T> Future<T> post(String uri, Class<T> resultType, Callback<T> callback) { public <T> Future<T> post(String uri, Class<T> resultType, Callback<T> callback) {
RESTResultMapper<T> mapper = new RESTResultMapper<T>(resultType); RESTResultMapper<T> mapper = new RESTResultMapper<T>(resultType);
Callback<byte[]> cb = Callbacks.mapping(callback, mapper); Callback<byte[]> cb = Callbacks.mapping(callback, mapper);
return Futures.mapping(HTTP.post(uri, null, "", cb), mapper); return Futures.mapping(HTTP.post(uri, null, new byte[0], cb), mapper);
} }


public <T> T post(String uri, Class<T> resultType) { public <T> T post(String uri, Class<T> resultType) {
Expand Down

0 comments on commit c7d0493

Please sign in to comment.