Sitelet https://github.com/keep-trying-00/java/commit/506014fdff301d1830911336f8aaf93ac2467053
Skip to content

Commit 506014f

Browse files
author
Devendra
committed
adding options to push test jar
1 parent 4be7ac6 commit 506014f

4 files changed

Lines changed: 152 additions & 54 deletions

File tree

‎java/build.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<pathelement location="libs/json-simple-1.1.1.jar"/>
2121
<pathelement location="libs/slf4j-api-1.7.5.jar"/>
2222
<pathelement location="libs/junit-4.11.jar"/>
23+
<pathelement location="libs/commons-cli-1.2.jar"/>
2324
<pathelement location="jars/Pubnub-UnitTests-${VERSION}.jar"/>
2425
</path>
2526
<target name="build-all" depends="debug-build, build" />
@@ -148,7 +149,7 @@
148149
<attribute name="Company" value="Pubnub" />
149150
<attribute name="Project" value="pubnub-api" />
150151
<attribute name="Java-Version" value="${java.version}" />
151-
<attribute name="Rsrc-Class-Path" value="./ bcprov-jdk15on-1.47.jar json-simple-1.1.1.jar json-20090211.jar slf4j-api-1.7.5.jar slf4j-simple-1.7.5.jar" />
152+
<attribute name="Rsrc-Class-Path" value="./ bcprov-jdk15on-1.47.jar commons-cli-1.2.jar json-simple-1.1.1.jar json-20090211.jar slf4j-api-1.7.5.jar slf4j-simple-1.7.5.jar" />
152153
<attribute name="Class-Path" value="." />
153154
<attribute name="Rsrc-Main-Class" value="@{jar.classname}" />
154155
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader" />

‎java/examples/src/com/pubnub/examples/PubnubPushSampleCode.java‎

Lines changed: 150 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package com.pubnub.examples;
2+
import org.apache.commons.cli.*;
3+
24

35
import org.json.JSONArray;
46
import org.json.JSONException;
@@ -15,21 +17,142 @@
1517
import com.pubnub.api.PubnubSenderMissingException;
1618

1719
public class PubnubPushSampleCode {
18-
19-
String publish_key = "pub-c-c077418d-f83c-4860-b213-2f6c77bde29a";
20-
String subscribe_key = "sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe";
21-
String secret_key = "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy";
22-
23-
String channel = "abcd";
24-
25-
String auth_key = "abcd";
2620

21+
private static void usage(Options options){
22+
23+
HelpFormatter formatter = new HelpFormatter();
24+
formatter.printHelp( "Publisher", options );
25+
}
2726

2827
public void start() {
29-
Pubnub pubnub = new Pubnub(publish_key, subscribe_key);
30-
pubnub.setAuthKey("abcd");
28+
29+
30+
}
31+
/**
32+
* @param args
33+
*/
34+
public static void main(String[] args) {
35+
Options options = new Options();
36+
String publish_key = "demo";
37+
String subscribe_key = "demo";
38+
boolean nativ = false;
39+
boolean gcm = false;
40+
boolean apns = false;
41+
42+
String channel = "my_channel";
43+
44+
String origin = "pubsub";
45+
46+
String auth_key = "";
47+
48+
CommandLine cmd = null;
49+
50+
options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("publish_key").
51+
withType(String.class).withDescription("Publish Key ( default: 'demo' )").create());
52+
53+
options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("subscribe_key").
54+
withType(String.class).withDescription("Subscribe Key ( default: 'demo' )").create());
55+
56+
options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("origin").
57+
withType(String.class).withDescription("Origin ( Ex. pubsub )").create());
58+
59+
options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("auth_key").
60+
withType(String.class).withDescription("Auth Key").create());
61+
62+
options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("channel").
63+
withType(String.class).withDescription("Secret Key ( default: 'my_channel' )").create());
64+
65+
options.addOption(OptionBuilder.withLongOpt("apns").withDescription("APNS message").create());
66+
67+
options.addOption(OptionBuilder.withLongOpt("gcm").withDescription("GCM message").create());
68+
69+
options.addOption(OptionBuilder.withLongOpt("native").withDescription("Native message").create());
70+
71+
CommandLineParser parser = new BasicParser();
72+
try {
73+
cmd = parser.parse( options, args);
74+
} catch (ParseException e1) {
75+
usage(options);return;
76+
}
77+
78+
if (cmd.hasOption("publish_key")) {
79+
try {
80+
publish_key = cmd.getOptionValue("publish_key");
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
usage(options);return;
84+
}
85+
}
86+
87+
if (cmd.hasOption("subscribe_key")) {
88+
try {
89+
subscribe_key = cmd.getOptionValue("subscribe_key");
90+
} catch (Exception e) {
91+
e.printStackTrace();
92+
usage(options);return;
93+
}
94+
}
95+
96+
if (cmd.hasOption("origin")) {
97+
try {
98+
origin = cmd.getOptionValue("origin");
99+
} catch (Exception e) {
100+
e.printStackTrace();
101+
usage(options);return;
102+
}
103+
}
104+
105+
if (cmd.hasOption("channel")) {
106+
try {
107+
channel = cmd.getOptionValue("channel");
108+
} catch (Exception e) {
109+
e.printStackTrace();
110+
usage(options);return;
111+
}
112+
}
113+
114+
if (cmd.hasOption("auth_key")) {
115+
try {
116+
auth_key = cmd.getOptionValue("auth_key");
117+
} catch (Exception e) {
118+
e.printStackTrace();
119+
usage(options);return;
120+
}
121+
}
122+
123+
if (cmd.hasOption("apns")) {
124+
try {
125+
apns = true;
126+
} catch (Exception e) {
127+
e.printStackTrace();
128+
usage(options);return;
129+
}
130+
}
131+
132+
if (cmd.hasOption("gcm")) {
133+
try {
134+
gcm = true;
135+
} catch (Exception e) {
136+
e.printStackTrace();
137+
usage(options);return;
138+
}
139+
}
140+
141+
142+
if (cmd.hasOption("native")) {
143+
try {
144+
nativ = true;
145+
} catch (Exception e) {
146+
e.printStackTrace();
147+
usage(options);return;
148+
}
149+
}
150+
151+
152+
final Pubnub pubnub = new Pubnub(publish_key, subscribe_key);
153+
pubnub.setAuthKey(auth_key);
31154
pubnub.setCacheBusting(false);
32-
pubnub.setOrigin("dara20.devbuild");
155+
pubnub.setOrigin(origin);
33156

34157
// Create APNS message
35158

@@ -63,67 +186,41 @@ public void start() {
63186
@Override
64187
public void successCallback(String channel, Object response) {
65188
System.out.println(response);
189+
pubnub.shutdown();
66190
}
67191
@Override
68192
public void errorCallback(String channel, PubnubError error) {
69193
System.out.println(error);
194+
pubnub.shutdown();
70195
}
71196
};
72197

73198
PubnubSender sender = new PubnubSender(channel, pubnub, callback);
74199

75-
// Create message1 with apns, gcm and normal data
200+
PnMessage message = null;
76201

77-
PnMessage message1 = new PnMessage(sender, apnsMessage, gcmMessage);
78-
try {
79-
message1.put("test","hi");
80-
} catch (JSONException e1) {
81-
202+
if (apns && gcm) {
203+
message = new PnMessage(sender, apnsMessage, gcmMessage);
204+
} else if (apns) {
205+
message = new PnMessage(sender, apnsMessage);
206+
} else if (gcm) {
207+
message = new PnMessage(sender, gcmMessage);
82208
}
83-
84-
85-
// Create message2 with only apns payload
86-
87-
PnMessage message2 = new PnMessage(sender, apnsMessage);
88-
89-
90-
91-
// Create message3 with only gcm payload
92-
93-
PnMessage message3 = new PnMessage(sender, gcmMessage);
94-
95-
96-
// Create message4 with only no gcm or apns data
97-
98-
PnMessage message4 = new PnMessage(sender);
99-
try {
100-
message4.put("data","mydata");
101-
} catch (JSONException e1) {
209+
if (message == null) message = new PnMessage(sender);
210+
if (nativ) {
211+
try {
212+
message.put("test","hi");
213+
} catch (JSONException e1) {
102214

215+
}
103216
}
104217

105-
106-
// Create message4 with only gcm and apns data
107-
108-
PnMessage message5 = new PnMessage(sender, apnsMessage, gcmMessage);
109-
110-
111218
try {
112-
message1.publish();
113-
message2.publish();
114-
message3.publish();
115-
message4.publish();
116-
message5.publish();
219+
message.publish();
117220
} catch (PubnubSenderMissingException e) {
118221
System.out.println("Set Sender");
119222
}
120223

121-
}
122-
/**
123-
* @param args
124-
*/
125-
public static void main(String[] args) {
126-
new PubnubPushSampleCode().start();
127224

128225
}
129226

‎java/jars/PubnubPushTest.jar‎

37.5 KB
Binary file not shown.

‎java/libs/commons-cli-1.2.jar‎

40.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)