Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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