Skip to content

Commit f5e11c5

Browse files
committed
Initial Commit.
0 parents  commit f5e11c5

39 files changed

+718
-0
lines changed

.classpath

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="lib" path="war/WEB-INF/lib/flexjson.jar"/>
7+
<classpathentry kind="lib" path="war/WEB-INF/lib/org.json.jar"/>
8+
<classpathentry kind="lib" path="war/WEB-INF/lib/org.restlet.ext.json.jar"/>
9+
<classpathentry kind="lib" path="war/WEB-INF/lib/org.restlet.ext.servlet.jar"/>
10+
<classpathentry kind="lib" path="war/WEB-INF/lib/org.restlet.jar"/>
11+
<classpathentry kind="output" path="war/WEB-INF/classes"/>
12+
</classpath>

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.rar
19+
*.tar
20+
*.zip
21+
22+
# Logs and databases #
23+
######################
24+
*.log
25+
*.sql
26+
*.sqlite
27+
28+
# OS generated files #
29+
######################
30+
.DS_Store
31+
ehthumbs.db
32+
Icon
33+
Thumbs.db
34+
35+
# Ignore temp and swp files #
36+
#############################
37+
*~
38+
*.swp

.project

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Restful-Java-GAE</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>com.google.gdt.eclipse.core.webAppProjectValidator</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>com.google.appengine.eclipse.core.projectValidator</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>com.google.appengine.eclipse.core.enhancerbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>org.eclipse.jdt.core.javanature</nature>
31+
<nature>com.google.appengine.eclipse.core.gaeNature</nature>
32+
</natures>
33+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Fri Apr 02 01:43:29 EEST 2010
2+
eclipse.preferences.version=1
3+
filesCopiedToWebInfLib=appengine-api-1.0-sdk-1.3.2.jar|appengine-api-labs-1.3.2.jar|appengine-jsr107cache-1.3.2.jar|jsr107cache-1.1.jar|datanucleus-appengine-1.0.6.final.jar|datanucleus-core-1.1.5.jar|datanucleus-jpa-1.1.5.jar|geronimo-jpa_3.0_spec-1.1.1.jar|geronimo-jta_1.1_spec-1.1.1.jar|jdo2-api-2.3-eb.jar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Fri Apr 02 01:43:28 EEST 2010
2+
eclipse.preferences.version=1
3+
warSrcDir=war
4+
warSrcDirIsOutput=true

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#Application base prototype for RESTful-Java-GAE
2+
3+
This is a base prototype demo for setting up an infrastructure for a RESTful Java application, with a Google App Engine backend API talking to DataStore.
4+
5+
## Why
6+
7+
When I initially work with Google App Engine with RESTful API, I walk through lot of resources.
8+
Unfortunately I could not find out any concrete code to explain RESTful in GAE with Java.
9+
10+
Finally, I found [resftul-webshop](https://github.com/oscarrenalias/restful-webshop) which provides me a sample code to work with RESTful APIs in GAE. Thanks to [Oscar Renalias](https://github.com/oscarrenalias).
11+
Then I made some changes to make my application to work with RESTful APIs. Here, I just put my learnings in template code.
12+
13+
## USAGE
14+
15+
To start,
16+
* Just clone this repository
17+
* From Eclipse, Import this repository as 'Existing Projects into Workspace'.
18+
19+
##Bookmark Application
20+
21+
This is simple Bookmarking application where you create, retrieve, update, delete bookmarks.
22+
23+
There are five RESTful APIs provided
24+
25+
1. POST /bookmark - Create new bookmark
26+
2. GET /bookmarks - Get all bookmarks
27+
3. GET /bookmarks/:id - Get particular bookmark
28+
4. PUT /bookmarks/:id - Update particular bookmark
29+
5. DELETE /bookmarks:id - Delete particular bookmark
30+
31+
I haven't done any pages to test above APIs. You have to use `curl` tool or your browser to test the APIs.
32+
While testing with JSON data, I highly recommend you to use PrettyJSON tool. Simply you can pipe the output to Python JSON tool( | python -mjson.tool).
33+
In below sample, I use default GAE `url` as http://localhost:8888.
34+
35+
###POST /bookmark
36+
37+
$ curl -H 'Content-Type: application/json' -d '{"url":"http://github.com", "name":"GitHub", "description" : "Long Live Opensource" }' http://localhost:8888/api/v1/bookmarks
38+
$ curl -H 'Content-Type: application/json' -d '{"url":"http://google.com", "name":"Google", "description" : "Do not be Evil" }' http://localhost:8888/api/v1/bookmarks
39+
40+
###GET /bookmarks
41+
42+
$ curl http://localhost:8888/api/v1/bookmarks
43+
44+
###GET /bookmarks/:id
45+
$ curl http://localhost:8888/api/v1/bookmarks/1590
46+
47+
Note: I assume, the id is 1590. You have to place the ID which you got from GET all bookmarks.
48+
49+
###PUT /bookmarks/:id
50+
51+
$ curl -X PUT -H 'Content-Type: application/json' -d '{"url":"http://github.com", "name":"GitHub(Public)", "description" : "Long Live GitHub Public Repositories" }' http://localhost:8888/api/v1/bookmarks/1590
52+
53+
###DELETE /bookmarks:id
54+
55+
$ curl -X DELETE http://localhost:8888/api/v1/bookmarks/1590
56+
57+
58+
##Appendix
59+
It is under MIT License.
60+
Feel free to fork it and give pull requests.
61+
62+
Long Live Open Source

src/META-INF/jdoconfig.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
5+
6+
<persistence-manager-factory name="transactions-optional">
7+
<property name="javax.jdo.PersistenceManagerFactoryClass"
8+
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
9+
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
10+
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
11+
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
12+
<property name="javax.jdo.option.RetainValues" value="true"/>
13+
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
14+
</persistence-manager-factory>
15+
</jdoconfig>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.restfuljavagae;
2+
3+
import java.io.IOException;
4+
import javax.servlet.http.*;
5+
6+
@SuppressWarnings("serial")
7+
public class AdminServlet extends HttpServlet {
8+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
9+
throws IOException {
10+
11+
resp.setContentType("text/plain");
12+
resp.getWriter().println("Hello Admin!! " +
13+
"Sorry, We are working on Admin page.");
14+
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.restfuljavagae;
2+
3+
import org.restlet.Application;
4+
import org.restlet.Restlet;
5+
import org.restlet.routing.Router;
6+
7+
import com.github.restfuljavagae.resources.BookmarkResource;
8+
9+
public class RestfulApplication extends Application {
10+
11+
/**
12+
* Creates a root Restlet that will receive all incoming calls.
13+
*/
14+
@Override
15+
public Restlet createInboundRoot() {
16+
Router router = new Router(getContext());
17+
18+
// Defines sample routes
19+
// Route for GET all bookmarks and POST new bookmark
20+
// It handles following request
21+
// GET /bookmarks - Return all available bookmarks
22+
// POST /bookmarks - new bookmark is passed as JSON data and created.
23+
router.attach("/bookmarks", BookmarkResource.class);
24+
25+
// Route for GET/PUT/DELETE specific bookmark(by id)
26+
// It handles following request
27+
// GET /bookmarks/1 - Get bookmark 1
28+
// PUT /bookmarks/1 - Update bookmark 1 with the passed JSON data.
29+
// DELETE /bookmarks/1 - Delete bookmark 1
30+
router.attach("/bookmarks/{id}", BookmarkResource.class);
31+
32+
// You have to create your own ServerResource and attach it in the router as
33+
// above.
34+
35+
return router;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.restfuljavagae.models;
2+
3+
import javax.jdo.annotations.IdGeneratorStrategy;
4+
import javax.jdo.annotations.PersistenceCapable;
5+
import javax.jdo.annotations.Persistent;
6+
import javax.jdo.annotations.PrimaryKey;
7+
8+
@PersistenceCapable
9+
public class Bookmark {
10+
11+
@PrimaryKey
12+
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
13+
private Long id;
14+
15+
@Persistent
16+
private String url;
17+
18+
@Persistent
19+
private String name;
20+
21+
@Persistent
22+
private String description;
23+
24+
public Bookmark(String url) {
25+
this.url = url;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getUrl() {
37+
return url;
38+
}
39+
40+
public void setUrl(String url) {
41+
this.url = url;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
public String getDescription() {
53+
return description;
54+
}
55+
56+
public void setDescription(String description) {
57+
this.description = description;
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.restfuljavagae.models;
2+
3+
import javax.jdo.JDOHelper;
4+
import javax.jdo.PersistenceManagerFactory;
5+
6+
public final class ModelManager {
7+
8+
private static final PersistenceManagerFactory pmfInstance =
9+
JDOHelper.getPersistenceManagerFactory("transactions-optional");
10+
11+
private ModelManager() {}
12+
13+
public static PersistenceManagerFactory get() {
14+
return pmfInstance;
15+
}
16+
}

0 commit comments

Comments
 (0)