Skip to content

Commit ce39525

Browse files
committed
Added init sketch code for the library, with some use cases described in the readme file.
1 parent 1be801d commit ce39525

File tree

12 files changed

+517
-45
lines changed

12 files changed

+517
-45
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,123 @@
33
# java-ormen
44
Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
55

6+
The Async java-ormen library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses, without the developer needing to think about the http requests.
7+
8+
The library creates an abstraction layer on top of REST. The user of this library will in fact know very little about what protocol is used.
9+
10+
The library has been designed using the Future API. If you you don't need a synchronous library you can tell the library to execute query's synchronous.
11+
12+
13+
14+
15+
## Usage
16+
17+
Lets say that we have a REST API that exposes CRUD Operations for fetching Dog´s. Well dogs can bark, so we will use dogs in this example.
18+
19+
As an developer you will probably create some POJOS for a java representation of the Dogs. Then you will probably write some kind of handler that takes these objects and sends them
20+
21+
back and forth the REST API. Well, you will not need to do that any longer.
22+
23+
24+
One just simply do the following:
25+
26+
Start of by creating a normal POJO, but we will make sure it extends AbstractRestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
27+
28+
The AbstractRest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods takes an ActionCompletedinterface as parameter, so you can work with async request. More about that later. here is the code.
29+
30+
31+
```java
32+
33+
public class DogModel extends AbstractRESTModel {
34+
35+
// private vars that shall map against the json data fetched
36+
37+
public final String name;
38+
39+
public final int age;
40+
41+
public DogModel() {
42+
43+
name = "pluto";
44+
age = 5;
45+
}
46+
47+
public DogModel(String name, int age) {
48+
this.name = name;
49+
this.age = age;
50+
}
51+
52+
@Override
53+
protected String resourceUrl() {
54+
55+
return "/rest/dogs/";
56+
57+
}
58+
59+
@Override
60+
protected String identifierValue() {
61+
62+
return this.name;
63+
}
64+
65+
@Override
66+
public final AbstractRESTModel parse(JSONObject responseJson) {
67+
String name = (String) responseJson.get("name");
68+
69+
return new DogModel(name,123);
70+
71+
}
72+
// dedicated method
73+
public String bark() {
74+
return "VOFF says" + this.name;
75+
}
76+
77+
78+
79+
}
80+
```
81+
82+
83+
The Dog model has a method called ```resourceUrl```. This url will point on to the url location where we can work on dogs. The Dog model has a method called ```identifierValue```. It will be appended on the url on RUD operations. The ```parse``` method is there so you can convert raw json data to DogModel objects. This method will be called every time data is fetched from the server. ( the method will be a bit automagic in the future and populate the model with values without you needing to writing a parser ).
84+
85+
86+
So now we have created a model. Its now time to use it to fetch data. Simply write the following in your code. Remember, these operations are async so we will add a sleep in the end ( only for educational purpose ).
87+
88+
89+
```java
90+
public class App
91+
{
92+
public static void main( String[] args ) throws ExecutionException, InterruptedException, IOException {
93+
// create a new client
94+
95+
96+
System.out.println("fetching the dog");
97+
DogModel pluto = new DogModel();
98+
pluto.fetch(new ActionCompletedInterface() {
99+
public void onDone(AbstractRESTModel model) {
100+
101+
DogModel fetchedModel = (DogModel)model;
102+
103+
System.out.println("it went good " + fetchedModel.bark());
104+
105+
}
106+
107+
public void onError(Throwable e) {
108+
109+
System.out.println("it went bad" + e.getMessage());
110+
}
111+
});
112+
113+
114+
Thread.sleep(4000);
115+
System.out.println("dying");
116+
117+
}
118+
}
119+
```
120+
121+
In the code above, the dog model will call the rest api when the fetch method is invoked. When the server responds the onDone or onError will be fired.
122+
123+
6124

7125
# LICENSE

java-ormen.iml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
44
<output url="file://$MODULE_DIR$/target/classes" />
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
66
<content url="file://$MODULE_DIR$">
@@ -10,6 +10,11 @@
1010
</content>
1111
<orderEntry type="inheritedJdk" />
1212
<orderEntry type="sourceFolder" forTests="false" />
13-
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
13+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.10" level="project" />
14+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
15+
<orderEntry type="library" name="Maven: com.ning:async-http-client:1.9.31" level="project" />
16+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.12" level="project" />
17+
<orderEntry type="library" name="Maven: io.netty:netty:4.0.0.Alpha8" level="project" />
18+
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
1419
</component>
1520
</module>

pom.xml

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,43 @@
88
<packaging>jar</packaging>
99

1010
<name>java-ormen</name>
11-
<url>http://maven.apache.org</url>
11+
<url>http://github.com/code-brewery/java-ormen</url>
12+
13+
14+
<issueManagement>
15+
<system>teamforge tracker</system>
16+
<url>https://github.com/code-brewery/java-ormen/issues</url>
17+
</issueManagement>
1218

1319
<properties>
1420
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1521
</properties>
1622

23+
<distributionManagement>
24+
<snapshotRepository>
25+
<id>ossrh</id>
26+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
27+
</snapshotRepository>
28+
<repository>
29+
<id>ossrh</id>
30+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
31+
</repository>
32+
</distributionManagement>
33+
34+
<licenses>
35+
<license>
36+
<name>The Apache License, Version 2.0</name>
37+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
38+
</license>
39+
</licenses>
40+
41+
<scm>
42+
<connection>scm:git:git@github.com:code-brewery/java-ormen.git</connection>
43+
<developerConnection>scm:scm:git:git@github.com:code-brewery/java-ormen.git</developerConnection>
44+
<url>git@github.com/code-brewery/java-ormen.git</url>
45+
<tag>HEAD</tag>
46+
</scm>
47+
1748

1849
<dependencies>
1950
<dependency>
@@ -22,11 +53,38 @@
2253
<version>4.10</version>
2354
<scope>test</scope>
2455
</dependency>
25-
</dependencies>
56+
<dependency>
57+
<groupId>com.ning</groupId>
58+
<artifactId>async-http-client</artifactId>
59+
<version>1.9.31</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.netty</groupId>
63+
<artifactId>netty</artifactId>
64+
<version>4.0.0.Alpha8</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>com.googlecode.json-simple</groupId>
69+
<artifactId>json-simple</artifactId>
70+
<version>1.1.1</version>
71+
</dependency>
72+
73+
</dependencies>
2674

2775
<build>
2876
<sourceDirectory>src/main/java</sourceDirectory>
2977
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-compiler-plugin</artifactId>
81+
<version>3.3</version>
82+
<configuration>
83+
84+
<source>1.8</source>
85+
<target>1.8</target>
86+
</configuration>
87+
</plugin>
3088
<plugin>
3189
<groupId>org.jacoco</groupId>
3290
<artifactId>jacoco-maven-plugin</artifactId>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.codebrewery;
2+
3+
import com.ning.http.client.AsyncCompletionHandler;
4+
import com.ning.http.client.AsyncHttpClient;
5+
import com.ning.http.client.ListenableFuture;
6+
import com.ning.http.client.Response;
7+
import org.json.simple.JSONObject;
8+
import org.json.simple.parser.JSONParser;
9+
import org.json.simple.parser.ParseException;
10+
11+
import java.io.IOException;
12+
import java.util.concurrent.ExecutionException;
13+
14+
15+
/**
16+
* Created by jepp3 on 2015-09-04.
17+
*
18+
* This class implements functionality to communicate crud operations through an REST API.
19+
*
20+
*
21+
* The resource should have the body definition for POST,GET,SET operations. Etc its not allowed to have different
22+
* schemas for different operations. The payload stays the same
23+
*
24+
*
25+
*
26+
*/
27+
public abstract class AbstractRESTModel implements CrudModelInterface {
28+
29+
/**
30+
* This method returns the base url for a resource.
31+
*
32+
*
33+
* /rest/dogs/
34+
*
35+
* @return resource url common for all instances of this resource
36+
*/
37+
abstract String resourceUrl();
38+
39+
/**
40+
* Returns the uniq identifierValue value of the resource. This might in many cases be
41+
* the value of the id private variable.
42+
*
43+
*
44+
* "123"
45+
*
46+
* @return
47+
*/
48+
abstract String identifierValue();
49+
50+
/**
51+
* Override this method when you want to convert the json data to JAVA Objects
52+
*
53+
* @param response
54+
* @return
55+
*/
56+
public abstract AbstractRESTModel parse(JSONObject response) throws IOException;
57+
58+
59+
private String generateFullHttpUrl() {
60+
61+
return "http://localhost:8081"+resourceUrl();
62+
63+
}
64+
65+
private JSONObject convertResponseToJSONObject(Response response) throws IOException, ParseException {
66+
67+
return (JSONObject)new JSONParser().parse(response.getResponseBody());
68+
69+
}
70+
71+
public void fetch(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
72+
73+
74+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
75+
String urlToUse = generateFullHttpUrl() + identifierValue();
76+
asyncHttpClient.prepareGet(urlToUse).execute(new AsyncCompletionHandler() {
77+
78+
@Override
79+
public Object onCompleted(Response response) throws Exception {
80+
81+
82+
actions.onDone(parse(convertResponseToJSONObject(response)));
83+
84+
// is it possible to not return void?
85+
return null;
86+
}
87+
88+
@Override
89+
public void onThrowable(Throwable t) {
90+
91+
actions.onError(t);
92+
93+
}
94+
});
95+
96+
}
97+
98+
public void destroy(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
99+
100+
101+
102+
103+
}
104+
105+
public void update(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
106+
107+
108+
109+
}
110+
111+
public void create(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
112+
113+
114+
115+
}
116+
117+
118+
119+
120+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.codebrewery;
2+
3+
/**
4+
* Created by jepp3 on 2015-09-05.
5+
*/
6+
public interface ActionCompletedInterface {
7+
8+
9+
void onDone(AbstractRESTModel model);
10+
11+
void onError(Throwable e);
12+
13+
14+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.codebrewery;
22

3+
4+
import java.io.IOException;
5+
import java.util.concurrent.ExecutionException;
36
/**
47
* Hello world!
58
*
69
*/
710
public class App
811
{
9-
public static void main( String[] args )
10-
{
11-
System.out.println( "Hello World!" );
12+
public static void main( String[] args ) throws ExecutionException, InterruptedException, IOException {
13+
1214
}
1315
}

0 commit comments

Comments
 (0)