Skip to content

Commit 5792226

Browse files
author
Javen
committed
Refactored the app create ant task.
1. Normally app client 2. Ant task can set properties back.
1 parent 650f0ca commit 5792226

10 files changed

Lines changed: 246 additions & 209 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ bin/
33
.metadata/
44
target/
55
test-output/
6+
classes/
7+
dist/
68

79
*.class
810
*.project

build.xml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<project name="AntTask" basedir="." default="use">
2+
<project name="jpush-create-app" basedir="." default="use">
33

44
<property name="src.dir" value="src" />
55
<property name="classes.dir" value="classes" />
6+
67
<!--Modify them to create you own app -->
7-
<property name="dev_key" value="dev_key" />
8-
<property name="dev_secret" value="dev_secret" />
9-
<property name="app_name" value="app_name" />
10-
<property name="android_package" value="android_package" />
11-
<property name="group_name" value="group_name" />
8+
<property name="jpush_dev_key" value="903724367fadb34e4820b6e0" />
9+
<property name="jpush_dev_secret" value="654321" />
1210

1311
<path id="lib_classpath">
1412
<fileset dir="${basedir}/libs">
1513
<include name="*.jar" />
1614
</fileset>
1715
</path>
16+
1817
<target name="clean" description="Delete all generated files">
1918
<delete dir="${classes.dir}" failonerror="false" />
20-
<delete file="${ant.project.name}.jar" />
19+
<delete file="dist/${ant.project.name}.jar" />
2120
</target>
2221

2322
<target name="compile" description="Compiles the Task">
@@ -34,14 +33,20 @@
3433
</target>
3534

3635
<target name="jar" description="JARs the Task" depends="compile">
37-
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}" />
36+
<jar destfile="dist/${ant.project.name}.jar" basedir="${classes.dir}" />
3837
</target>
3938

4039
<target name="use" description="Use the Task" depends="jar">
41-
<taskdef name="antTask" classname="cn.jpush.api.ant.AntTask" classpath="${ant.project.name}.jar" classpathref="lib_classpath" />
42-
<antTask />
43-
40+
<taskdef name="create-app"
41+
classname="cn.jpush.api.tools.AppCreateAntTask"
42+
classpath="dist/${ant.project.name}.jar"
43+
classpathref="lib_classpath" />
44+
<create-app app_name="Test4"
45+
android_package="com.example.test4"
46+
group_name="group1" />
47+
<echo message="app_key:${jpush_app_key},
48+
android_package:${jpush_android_package},
49+
is_new_created:${jpush_is_new_created}" />
4450
</target>
51+
</project>
4552

46-
47-
</project>

resources/jpush-api.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ username=jpush_api_client
66
password=654321
77
appKey=dd1066407b044738b6479275
88
masterSecret=2b38ce69b1de2a7fa95706ea
9+
devKey=903724367fadb34e4820b6e0
910

1011
alias=alias_api
1112
tag=tag_api
1213
registrationID=0900e8d85ef
13-

src/cn/jpush/api/ant/AntTask.java

Lines changed: 0 additions & 194 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.jpush.api.app;
2+
3+
import cn.jpush.api.app.model.AppModel;
4+
import cn.jpush.api.common.NativeHttpClient;
5+
import cn.jpush.api.common.ResponseWrapper;
6+
import cn.jpush.api.common.ServiceHelper;
7+
8+
/**
9+
* Entrance for sending Push.
10+
*
11+
* For the following parameters, you can set them by instance creation.
12+
* This action will override setting in PushPayload Optional.
13+
* * apnsProduction If not present, the default is true.
14+
* * timeToLive If not present, the default is 86400(s) (one day).
15+
*
16+
* Can be used directly.
17+
*/
18+
public class AppClient {
19+
private static final String HOST_NAME_SSL = "https://admin.jpush.cn";
20+
private static final String REQUEST_PATH = "/v1/app";
21+
22+
private NativeHttpClient _httpClient = new NativeHttpClient();;
23+
24+
// The API secret of the appKey. Please get it from JPush Web Portal
25+
private final String _devSecret;
26+
27+
// The KEY of the Application created on JPush. Please get it from JPush Web Portal
28+
private final String _devKey;
29+
30+
// Generated HTTP Basic authorization string.
31+
private final String _authCode;
32+
private String _baseUrl;
33+
34+
/**
35+
* Create a Push Client.
36+
*
37+
* @param devSecret API access secret of the appKey.
38+
* @param devKey The KEY of one application on JPush.
39+
*/
40+
public AppClient(String devKey, String devSecret) {
41+
this._devKey = devKey;
42+
this._devSecret = devSecret;
43+
44+
this._authCode = ServiceHelper.getAuthorizationBase64(_devKey, _devSecret);
45+
this._baseUrl = HOST_NAME_SSL + REQUEST_PATH;
46+
//ServiceHelper.checkBasic(devKey, devSecret);
47+
}
48+
49+
public void setBaseUrl(String baseUrl) {
50+
this._baseUrl = baseUrl;
51+
}
52+
53+
public AppCreateResult create(AppModel app) {
54+
ResponseWrapper response = _httpClient.sendPost(_baseUrl, app.toString(), _authCode);
55+
56+
return AppCreateResult.fromResponse(response);
57+
}
58+
59+
}
60+
61+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.jpush.api.app;
2+
3+
import cn.jpush.api.common.BaseResult;
4+
import cn.jpush.api.common.ResponseWrapper;
5+
6+
import com.google.gson.annotations.Expose;
7+
8+
public class AppCreateResult extends BaseResult {
9+
@Expose public String app_key;
10+
@Expose public String android_package;
11+
@Expose public boolean is_new_created;
12+
13+
public static AppCreateResult fromResponse(ResponseWrapper responseWrapper) {
14+
AppCreateResult result = null;
15+
16+
if (responseWrapper.isServerResonse()) {
17+
result = _gson.fromJson(responseWrapper.responseContent, AppCreateResult.class);
18+
} else {
19+
result = new AppCreateResult();
20+
}
21+
22+
result.setResponseWrapper(responseWrapper);
23+
24+
return result;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)