Skip to content

Commit 756cc96

Browse files
committed
Endpoint service and web app
1 parent 0a996b0 commit 756cc96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+9849
-0
lines changed

Diff for: .classpath

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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/java-json.jar"/>
7+
<classpathentry kind="output" path="war/WEB-INF/classes"/>
8+
</classpath>

Diff for: .project

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>SimpleEndpoints</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>com.google.gdt.eclipse.core.webAppProjectValidator</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>com.google.appengine.eclipse.core.gaeProjectChangeNotifier</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>com.google.appengine.eclipse.core.projectValidator</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>com.google.appengine.eclipse.core.enhancerbuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
</buildSpec>
39+
<natures>
40+
<nature>org.eclipse.jdt.core.javanature</nature>
41+
<nature>com.google.appengine.eclipse.core.gaeNature</nature>
42+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
43+
</natures>
44+
</projectDescription>

Diff for: .settings/com.google.appengine.eclipse.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
filesCopiedToWebInfLib=appengine-api-1.0-sdk-1.9.5.jar|appengine-api-labs.jar|appengine-endpoints-deps.jar|appengine-endpoints.jar|appengine-jsr107cache-1.9.5.jar|asm-4.0.jar|datanucleus-api-jdo-3.1.3.jar|datanucleus-api-jpa-3.1.3.jar|datanucleus-appengine-2.1.2.jar|datanucleus-core-3.1.3.jar|geronimo-jpa_2.0_spec-1.0.jar|jdo-api-3.0.1.jar|jsr107cache-1.1.jar|jta-1.1.jar
3+
gaeDatanucleusVersion=v2
4+
gaeHrdEnabled=true

Diff for: .settings/com.google.gdt.eclipse.core.prefs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
warSrcDir=war
3+
warSrcDirIsOutput=true

Diff for: .settings/org.eclipse.jdt.core.prefs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.compliance=1.7
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.source=1.7
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<installed facet="java" version="1.7"/>
4+
</faceted-project>

Diff for: src/META-INF/jdoconfig.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.api.jdo.JDOPersistenceManagerFactory"/>
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+
<property name="datanucleus.appengine.singletonPMFForName" value="true"/>
15+
</persistence-manager-factory>
16+
</jdoconfig>

Diff for: src/META-INF/persistence.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5+
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
6+
7+
<persistence-unit name="transactions-optional">
8+
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
9+
<properties>
10+
<property name="datanucleus.NontransactionalRead" value="true"/>
11+
<property name="datanucleus.NontransactionalWrite" value="true"/>
12+
<property name="datanucleus.ConnectionURL" value="appengine"/>
13+
</properties>
14+
</persistence-unit>
15+
</persistence>
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.guesswork.community.endpoint;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.HashSet;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
import org.json.JSONTokener;
14+
15+
import com.google.appengine.api.datastore.Blob;
16+
import com.google.appengine.api.datastore.Entity;
17+
import com.google.appengine.api.datastore.Text;
18+
import com.google.appengine.api.utils.SystemProperty;
19+
20+
public class DatastoreUtil
21+
{
22+
23+
private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes();
24+
25+
public static boolean isWrapperType(Class<?> clazz)
26+
{
27+
return WRAPPER_TYPES.contains(clazz);
28+
}
29+
30+
private static Set<Class<?>> getWrapperTypes()
31+
{
32+
Set<Class<?>> ret = new HashSet<Class<?>>();
33+
ret.add(String.class);
34+
ret.add(Boolean.class);
35+
ret.add(Character.class);
36+
ret.add(Byte.class);
37+
ret.add(Short.class);
38+
ret.add(Integer.class);
39+
ret.add(Long.class);
40+
ret.add(Float.class);
41+
ret.add(Double.class);
42+
ret.add(Void.class);
43+
ret.add(Date.class);
44+
return ret;
45+
}
46+
47+
public static Entity update(JSONObject source, Entity target) throws Exception
48+
{
49+
Iterator it = source.keys();
50+
while(it.hasNext())
51+
{
52+
String key = (String)it.next();
53+
Object val = source.get(key);
54+
if(val instanceof String)
55+
{
56+
String str = (String)val;
57+
if(str.length() < 500)
58+
target.setProperty(key, str);
59+
else
60+
target.setProperty(key, new Text(str));
61+
}
62+
else if(isWrapperType(val.getClass()))
63+
{
64+
target.setProperty(key, val);
65+
}
66+
else if(val instanceof JSONArray)
67+
{
68+
JSONArray l = (JSONArray)val;
69+
if(l.length() > 0)
70+
{
71+
Object first = l.get(0);
72+
if(isWrapperType(first.getClass()))
73+
{
74+
List uplst = new ArrayList();
75+
for(int i = 0; i < l.length(); i++)
76+
uplst.add(l.get(i));
77+
target.setProperty(key, uplst);
78+
}
79+
else
80+
{
81+
String js = l.toString();
82+
target.setProperty(key, new Blob(js.getBytes()));
83+
}
84+
}
85+
}
86+
else if(val instanceof JSONObject)
87+
{
88+
JSONObject m = (JSONObject)val;
89+
String js = m.toString();
90+
target.setProperty(key, new Blob(js.getBytes()));
91+
}
92+
}
93+
return target;
94+
}
95+
96+
public static JSONObject update(Entity source, JSONObject target) throws Exception
97+
{
98+
String sid = source.getKey().getName();
99+
if(sid == null)
100+
target.put("id", Long.toString(source.getKey().getId()));
101+
else
102+
target.put("id", sid);
103+
target.put("kind", source.getKind());
104+
Map m = source.getProperties();
105+
for(Object ky : m.keySet())
106+
{
107+
String key = (String)ky;
108+
Object val = m.get(key);
109+
if(val == null)
110+
target.put(key, val);
111+
else if(val instanceof String)
112+
target.put(key, val);
113+
else if(isWrapperType(val.getClass()))
114+
target.put(key, val);
115+
else if(val instanceof List)
116+
{
117+
List vlst = (List)val;
118+
JSONArray arr = new JSONArray();
119+
for(int i = 0; i < vlst.size(); i++)
120+
arr.put(vlst.get(i));
121+
target.put(key, arr);
122+
}
123+
else if(val instanceof Text)
124+
{
125+
Text txt = (Text)val;
126+
target.put(key, txt.getValue());
127+
}
128+
else if(val instanceof Blob)
129+
{
130+
Blob blb = (Blob)val;
131+
target.put(key, convert(blb));
132+
}
133+
}
134+
return target;
135+
}
136+
137+
public static Object convert(Blob blb) throws Exception
138+
{
139+
if(blb == null)
140+
return null;
141+
else
142+
{
143+
String str = new String(blb.getBytes());
144+
JSONTokener tok = new JSONTokener(str);
145+
if(str.startsWith("["))
146+
return new JSONArray(tok);
147+
else if(str.startsWith("{"))
148+
return new JSONObject(tok);
149+
else
150+
return str;
151+
}
152+
}
153+
154+
public String getString(Entity en, String name)
155+
{
156+
Object val = en.getProperty(name);
157+
if(val == null)
158+
return null;
159+
else if(val instanceof String)
160+
return (String)val;
161+
else if(val instanceof Text)
162+
return ((Text)val).getValue();
163+
else
164+
return val.toString();
165+
166+
}
167+
168+
public static boolean isLive()
169+
{
170+
return SystemProperty.environment.value()==SystemProperty.Environment.Value.Production;
171+
}
172+
173+
}

0 commit comments

Comments
 (0)