|
@@ -0,0 +1,107 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2012 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.addons.nma;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.util.io.Downloader;
|
|
26
|
+
|
|
27
|
+import java.io.IOException;
|
|
28
|
+import java.util.Arrays;
|
|
29
|
+import java.util.Collection;
|
|
30
|
+import java.util.HashMap;
|
|
31
|
+import java.util.List;
|
|
32
|
+import java.util.Map;
|
|
33
|
+
|
|
34
|
+import lombok.RequiredArgsConstructor;
|
|
35
|
+import lombok.extern.slf4j.Slf4j;
|
|
36
|
+
|
|
37
|
+/**
|
|
38
|
+ * Simple client for sending events to NotifyMyAndroid.
|
|
39
|
+ */
|
|
40
|
+@Slf4j
|
|
41
|
+@RequiredArgsConstructor
|
|
42
|
+public class NotifyMyAndroidClient {
|
|
43
|
+
|
|
44
|
+ /** The base URL for the NMA API. */
|
|
45
|
+ private static final String BASE_URL = "https://www.notifymyandroid.com";
|
|
46
|
+ /** The method to call to send a notification. */
|
|
47
|
+ private static final String NOTIFY_PATH = "/publicapi/notify";
|
|
48
|
+
|
|
49
|
+ /** The API keys to deliver to. */
|
|
50
|
+ private final Collection<String> apiKeys;
|
|
51
|
+
|
|
52
|
+ /** The application to report ourselves as. */
|
|
53
|
+ private final String application;
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * Creates a new instance of {@link NotifyMyAndroidClient} with a single
|
|
57
|
+ * API key.
|
|
58
|
+ *
|
|
59
|
+ * @param apiKey The API key to use when connecting to NMA
|
|
60
|
+ * @param application The application string to report to NMA.
|
|
61
|
+ */
|
|
62
|
+ public NotifyMyAndroidClient(final String apiKey, final String application) {
|
|
63
|
+ this(Arrays.asList(new String[] { apiKey }), application);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * Sends a notification to NotifyMyAndroid. At present return status and
|
|
68
|
+ * text is ignored.
|
|
69
|
+ *
|
|
70
|
+ * @param event The name of the event to send (max 1,000 chars).
|
|
71
|
+ * @param description The description of the event (max 10,000 chars).
|
|
72
|
+ * @throws IOException If the NMA service couldn't be reached
|
|
73
|
+ */
|
|
74
|
+ public void notify(final String event, final String description) throws IOException {
|
|
75
|
+ final Map<String, String> arguments = new HashMap<String, String>();
|
|
76
|
+ arguments.put("apikey", getApiKeys());
|
|
77
|
+ arguments.put("application", application);
|
|
78
|
+ arguments.put("event", event);
|
|
79
|
+ arguments.put("description", description);
|
|
80
|
+
|
|
81
|
+ log.info("Sending notification to NMA for event '{}'", event);
|
|
82
|
+ log.debug("Arguments: {}", arguments);
|
|
83
|
+
|
|
84
|
+ final List<String> response = Downloader.getPage(BASE_URL + NOTIFY_PATH, arguments);
|
|
85
|
+ log.debug("Response: {}", response);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ /**
|
|
89
|
+ * Returns a comma-separated list of API keys.
|
|
90
|
+ *
|
|
91
|
+ * @return A string representation of API keys.
|
|
92
|
+ */
|
|
93
|
+ private String getApiKeys() {
|
|
94
|
+ final StringBuilder builder = new StringBuilder();
|
|
95
|
+
|
|
96
|
+ for (String apiKey : apiKeys) {
|
|
97
|
+ if (builder.length() > 0) {
|
|
98
|
+ builder.append(',');
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ builder.append(apiKey);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ return builder.toString();
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+}
|