You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotifyMyAndroidClient.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2014 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. package com.dmdirc.addons.nma;
  23. import com.dmdirc.util.io.Downloader;
  24. import java.io.IOException;
  25. import java.util.Arrays;
  26. import java.util.Collection;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import org.slf4j.LoggerFactory;
  31. /**
  32. * Simple client for sending events to NotifyMyAndroid.
  33. */
  34. public class NotifyMyAndroidClient {
  35. private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(NotifyMyAndroidClient.class);
  36. /** The base URL for the NMA API. */
  37. private static final String BASE_URL = "https://www.notifymyandroid.com";
  38. /** The method to call to send a notification. */
  39. private static final String NOTIFY_PATH = "/publicapi/notify";
  40. /** The API keys to deliver to. */
  41. private final Collection<String> apiKeys;
  42. /** The application to report ourselves as. */
  43. private final String application;
  44. public NotifyMyAndroidClient(final Collection<String> apiKeys, final String application) {
  45. this.apiKeys = apiKeys;
  46. this.application = application;
  47. }
  48. /**
  49. * Creates a new instance of {@link NotifyMyAndroidClient} with a single API key.
  50. *
  51. * @param apiKey The API key to use when connecting to NMA
  52. * @param application The application string to report to NMA.
  53. */
  54. public NotifyMyAndroidClient(final String apiKey, final String application) {
  55. this(Arrays.asList(new String[]{apiKey}), application);
  56. }
  57. /**
  58. * Sends a notification to NotifyMyAndroid. At present return status and text is ignored.
  59. *
  60. * @param event The name of the event to send (max 1,000 chars).
  61. * @param description The description of the event (max 10,000 chars).
  62. *
  63. * @throws IOException If the NMA service couldn't be reached
  64. */
  65. public void notify(final String event, final String description) throws IOException {
  66. final Map<String, String> arguments = new HashMap<>();
  67. arguments.put("apikey", getApiKeys());
  68. arguments.put("application", application);
  69. arguments.put("event", event);
  70. arguments.put("description", description);
  71. LOG.info("Sending notification to NMA for event '{}'", event);
  72. LOG.debug("Arguments: {}", arguments);
  73. final List<String> response = Downloader.getPage(BASE_URL + NOTIFY_PATH, arguments);
  74. LOG.debug("Response: {}", response);
  75. }
  76. /**
  77. * Returns a comma-separated list of API keys.
  78. *
  79. * @return A string representation of API keys.
  80. */
  81. private String getApiKeys() {
  82. final StringBuilder builder = new StringBuilder();
  83. for (String apiKey : apiKeys) {
  84. if (builder.length() > 0) {
  85. builder.append(',');
  86. }
  87. builder.append(apiKey);
  88. }
  89. return builder.toString();
  90. }
  91. }