Skip to content

Commit a1590d9

Browse files
committed
Server draft to deploy on Google Engine
1 parent 05c7fe8 commit a1590d9

File tree

8 files changed

+207
-14
lines changed

8 files changed

+207
-14
lines changed

build.gradle

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,51 @@ buildscript {
66

77
dependencies {
88
classpath 'com.stanfy.helium:gradle-plugin:0.8.0-SNAPSHOT'
9+
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.30'
910
}
1011
}
1112

12-
apply plugin: 'java'
1313
apply plugin: 'helium'
14-
15-
sourceCompatibility = JavaVersion.VERSION_1_8
16-
targetCompatibility = JavaVersion.VERSION_1_8
14+
apply plugin: 'war'
15+
apply plugin: 'appengine'
1716

1817
repositories {
1918
mavenCentral()
2019
}
2120

2221
dependencies {
22+
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.32'
2323
compile 'com.squareup.retrofit:retrofit:1.9.0'
24+
compile 'com.google.code.gson:gson:1.7.2'
25+
compile 'javax.servlet:javax.servlet-api:3.0.1'
2426
}
2527

28+
sourceCompatibility = JavaVersion.VERSION_1_7
29+
targetCompatibility = JavaVersion.VERSION_1_7
30+
31+
appengine {
32+
33+
downloadSdk = true
34+
35+
appcfg {
36+
oauth2 = true
37+
}
38+
39+
app {
40+
id = 'helium-1222'
41+
}
42+
43+
update {
44+
useJava7 = true
45+
}
46+
47+
logs {
48+
severity = 0
49+
outputFile = file('debugLogs.txt')
50+
}
51+
}
2652

53+
////// Hellium api and code generation scope. //////
2754
import com.stanfy.helium.handler.codegen.java.constants.ConstantNameConverter
2855

2956
import javax.lang.model.element.Modifier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.stanfy.helium.server;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Data {
6+
7+
@SerializedName("boolean_truth")
8+
private boolean boolData;
9+
10+
@SerializedName("string_data")
11+
private String text;
12+
13+
@SerializedName("numeric_data")
14+
private Integer numericValue;
15+
16+
public boolean getBoolValue() {
17+
return boolData;
18+
}
19+
20+
public String getText() {
21+
return text;
22+
}
23+
24+
public void setNumericValue(Integer newValue) {
25+
numericValue = newValue;
26+
}
27+
28+
public Integer getNumericValue() {
29+
return numericValue;
30+
}
31+
32+
public void setBoolData(boolean boolData) {
33+
this.boolData = boolData;
34+
}
35+
36+
public void setText(String text) {
37+
this.text = text;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "string = " + text + ", bool = " + boolData + ", number = " + numericValue.toString();
43+
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.stanfy.helium.server;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.io.PrintWriter;
12+
import java.util.HashMap;
13+
14+
public class DataRetrieveServlet extends HttpServlet {
15+
16+
private static final HashMap<String, Data> STORE = new HashMap<>(500);
17+
private static final Gson GSON = new GsonBuilder().serializeNulls().create();
18+
19+
@Override
20+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
21+
22+
String dataId = getDataId(req);
23+
24+
Data data = STORE.get(dataId);
25+
String jsonData = buildResponse(data);
26+
27+
putClientResponse(resp, jsonData);
28+
respondSuccess(resp);
29+
}
30+
31+
@Override
32+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
33+
Data requestData = GSON.fromJson(req.getReader(), Data.class);
34+
String dataId = getDataId(req);
35+
36+
STORE.put(dataId, requestData);
37+
String jsonData = GSON.toJson(requestData);
38+
39+
putClientResponse(resp, jsonData);
40+
respondSuccess(resp);
41+
}
42+
43+
@Override
44+
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45+
String dataId = getDataId(req);
46+
STORE.remove(dataId);
47+
48+
respondSuccess(resp);
49+
}
50+
51+
@Override
52+
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
53+
Data requestData = GSON.fromJson(req.getReader(), Data.class);
54+
55+
String dataId = getDataId(req);
56+
57+
STORE.put(dataId, requestData);
58+
respondSuccess(resp);
59+
}
60+
61+
private String buildResponse(final Data data) {
62+
ResponseData response = new ResponseData();
63+
response.setData(data);
64+
return GSON.toJson(response);
65+
}
66+
67+
private void putClientResponse(HttpServletResponse resp, String jsonData) throws IOException {
68+
PrintWriter respWriter = resp.getWriter();
69+
respWriter.print(jsonData);
70+
respWriter.flush();
71+
}
72+
73+
private String getDataId(HttpServletRequest req) {
74+
return req.getPathInfo().replaceFirst("/", "");
75+
}
76+
77+
private void respondSuccess(HttpServletResponse resp) {
78+
resp.setContentType("application/json");
79+
resp.setStatus(HttpServletResponse.SC_OK);
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.stanfy.helium.server;
2+
3+
public class ResponseData {
4+
5+
Data data;
6+
7+
public Data getData() {
8+
return data;
9+
}
10+
11+
public void setData(Data data) {
12+
this.data = data;
13+
}
14+
}

src/main/java/com/stanfy/hellium/server/HelloWorld.java

-10
This file was deleted.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>helium-1222</application>
4+
<version>1</version>
5+
<threadsafe>true</threadsafe>
6+
7+
<system-properties>
8+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
</system-properties>
10+
</appengine-web-app>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A default java.util.logging configuration.
2+
# (All App Engine logging is through java.util.logging by default).
3+
#
4+
# To use this configuration, copy it into your application's WEB-INF
5+
# folder and add the following to your appengine-web.xml:
6+
#
7+
# <system-properties>
8+
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
# </system-properties>
10+
#
11+
12+
# Set the default logging level for all loggers to WARNING
13+
.level = WARNING

src/main/webapp/WEB-INF/web.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
version="2.5">
5+
<servlet>
6+
<servlet-name>retrieveData</servlet-name>
7+
<servlet-class>com.stanfy.helium.server.DataRetrieveServlet</servlet-class>
8+
</servlet>
9+
<servlet-mapping>
10+
<servlet-name>retrieveData</servlet-name>
11+
<url-pattern>/data/*</url-pattern>
12+
</servlet-mapping>
13+
</web-app>

0 commit comments

Comments
 (0)