Browse files

minor fixes and added REST client module

  • Loading branch information...
1 parent 6706f6c commit 8f4041bf2c58ee3cf0e90d3f82e324b5afcb1087 @hofmeister committed Dec 29, 2012
View
30 modules/webi-client/pom.xml
@@ -0,0 +1,30 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.vonhof</groupId>
+ <artifactId>webi-client</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>webi-client</name>
+ <url>http://maven.apache.org</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>vonhof</groupId>
+ <artifactId>babelshark</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+</project>
View
163 modules/webi-client/src/main/java/com/vonhof/webi/client/RESTClient.java
@@ -0,0 +1,163 @@
+package com.vonhof.webi.client;
+
+import com.vonhof.babelshark.BabelShark;
+import com.vonhof.babelshark.BabelSharkInstance;
+import com.vonhof.babelshark.Input;
+import com.vonhof.babelshark.Output;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ *
+ * @author Henrik Hofmeister <@vonhofdk>
+ */
+public class RESTClient {
+
+ private BabelSharkInstance bs = BabelShark.getDefaultInstance();
+ private String contentType = "application/json";
+
+ private final String baseUrl;
+
+ public RESTClient(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+ public Request req() {
+ return new Request();
+ }
+
+ protected <T> T send(Request req,String method,Object body,Class<T> responseClass) throws MalformedURLException, IOException {
+ URL url = new URL(req.toURL());
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+
+ conn.setRequestMethod(method.toUpperCase());
+ conn.setInstanceFollowRedirects(true);
+ if (!req.headers.isEmpty()) {
+ for(Entry<String,String> header:req.headers.entrySet()) {
+ conn.addRequestProperty(header.getKey(), header.getValue());
+ }
+ }
+
+ if (body != null) {
+ conn.setDoOutput(true);
+ bs.write(new Output(conn.getOutputStream(),contentType), body);
+ }
+
+ if (responseClass != null) {
+ String responseType = conn.getHeaderField("Content-type");
+ T out = bs.read(new Input(conn.getInputStream(), responseType), responseClass);
+ conn.disconnect();
+ return out;
+ }
+
+ return null;
+ }
+
+ public static Request from(String baseUrl) {
+ return new RESTClient(baseUrl).req();
+ }
+
+ public final class Request {
+ private List<String> paths = new LinkedList<String>();
+ private Map<String,String> queryParams = new HashMap<String, String>();
+ private Map<String,String> headers = new HashMap<String, String>();
+
+ private Request() {
+
+ }
+
+ public Request p(String path) {
+ paths.add(path);
+ return this;
+ }
+
+ public Request q(String name,String value) {
+ queryParams.put(name, value);
+ return this;
+ }
+
+ public Request h(String name,String value) {
+ headers.put(name, value);
+ return this;
+ }
+
+ private String toURL() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(baseUrl);
+ sb.append("/");
+
+ for(String path:paths) {
+ sb.append(path);
+ sb.append("/");
+ }
+
+ if (!queryParams.isEmpty()) {
+ sb.append("?");
+ for(Entry<String,String> parm:queryParams.entrySet()) {
+ sb.append(parm.getKey())
+ .append("=")
+ .append(parm.getValue());
+ }
+ }
+
+ return sb.toString();
+ }
+
+ public <T> T get(Class<T> responseType) throws IOException {
+ return send(this, "GET", null, responseType);
+ }
+
+ public void get() throws IOException {
+ get(null);
+ }
+
+ public <T> T head(Class<T> responseType) throws IOException {
+ return send(this, "HEAD", null, responseType);
+ }
+
+ public void head() throws IOException {
+ head(null);
+ }
+
+ public <T> T delete(Class<T> responseType) throws IOException {
+ return send(this, "DELETE", null, responseType);
+ }
+
+ public void delete() throws IOException {
+ delete(null);
+ }
+
+ public <T> T put(Object body,Class<T> responseType) throws IOException {
+ return send(this, "PUT", body, responseType);
+ }
+
+ public void put(Object body) throws IOException {
+ put(body, null);
+ }
+
+ public void put() throws IOException {
+ put(null);
+ }
+
+ public <T> T post(Object body,Class<T> responseType) throws IOException {
+ return send(this, "POST", body, responseType);
+ }
+
+ public void post(Object body) throws IOException {
+ post(body, null);
+ }
+
+ public void post() throws IOException {
+ post(null);
+ }
+
+
+ }
+}
View
7 modules/webi-mongo/src/main/java/com/vonhof/webi/mongo/dto/BasicDTO.java
@@ -22,4 +22,11 @@ public String getId() {
public void setId(String id) {
this.id = id;
}
+
+ @Override
+ public String toString() {
+ return String.format("%s[%s]",this.getClass().getSimpleName(),this.getId());
+ }
+
+
}

0 comments on commit 8f4041b

Please sign in to comment.