/src/ru/eltech/elerning/geotracker/core/services/BaseGeo2TagService.java

https://github.com/e-learning/geotracker · Java · 268 lines · 237 code · 27 blank · 4 comment · 0 complexity · d1f86742291b35411c7f6e6915010b8f MD5 · raw file

  1. package ru.eltech.elerning.geotracker.core.services;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4. import ru.eltech.elerning.geotracker.util.ConnectionUtils;
  5. import ru.eltech.elerning.geotracker.util.StringUtils;
  6. import java.io.IOException;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import static ru.eltech.elerning.geotracker.core.Geo2TagConstants.Params;
  11. import static ru.eltech.elerning.geotracker.core.Geo2TagConstants.Services;
  12. /**
  13. * Author: Kirill Korgov (korgov@yandex-team.ru)
  14. * Date: 12/18/11
  15. */
  16. public class BaseGeo2TagService {
  17. private static final String HOST = "http://tracks.osll.spb.ru:81";
  18. private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MM yyyy HH:mm:ss.SSS");
  19. public static JSONObject login(final String userLogin, final String userPassword) {
  20. final JSONObject requestParams = new JSONObject();
  21. try {
  22. requestParams.put(Params.LOGIN, userLogin);
  23. requestParams.put(Params.PASSWORD, userPassword);
  24. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.LOGIN, requestParams.toString()));
  25. } catch (JSONException e) {
  26. throw new RuntimeException("Incorrect login or password format: login: {" + userLogin + "}, pwd: {" + userPassword + "}", e);
  27. } catch (IOException e) {
  28. throw new RuntimeException(e);
  29. }
  30. }
  31. public static JSONObject addUser(final String userLogin, final String userPassword) {
  32. final JSONObject requestParams = new JSONObject();
  33. try {
  34. requestParams.put(Params.LOGIN, userLogin);
  35. requestParams.put(Params.PASSWORD, userPassword);
  36. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.ADD_USER, requestParams.toString()));
  37. } catch (JSONException e) {
  38. throw new RuntimeException("Incorrect login or password format: login: {" + userLogin + "}, pwd: {" + userPassword + "}", e);
  39. } catch (IOException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43. public static JSONObject loadAvailableChannels(final String authToken, final double lat, final double lon, final double radius) {
  44. final JSONObject requestParams = new JSONObject();
  45. try {
  46. requestParams.put(Params.AUTH_TOKEN, authToken);
  47. requestParams.put(Params.LATITUDE, lat);
  48. requestParams.put(Params.LONGITUDE, lon);
  49. requestParams.put(Params.RADIUS, radius);
  50. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.CHANNELS, requestParams.toString()));
  51. } catch (JSONException e) {
  52. throw new RuntimeException("Incorrect loadAvailableChannels() params format", e);
  53. } catch (IOException e) {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. public static JSONObject loadSubscribedChannels(final String authToken) {
  58. final JSONObject requestParams = new JSONObject();
  59. try {
  60. requestParams.put(Params.AUTH_TOKEN, authToken);
  61. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SUBSCRIBED, requestParams.toString()));
  62. } catch (JSONException e) {
  63. throw new RuntimeException("Incorrect loadSubscribedChannels() params format", e);
  64. } catch (IOException e) {
  65. throw new RuntimeException(e);
  66. }
  67. }
  68. public static JSONObject subscribeChannel(final String authToken, final String channelName) {
  69. final JSONObject requestParams = new JSONObject();
  70. try {
  71. requestParams.put(Params.AUTH_TOKEN, authToken);
  72. requestParams.put(Params.CHANNEL, channelName);
  73. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SUBSCRIBE, requestParams.toString()));
  74. } catch (JSONException e) {
  75. throw new RuntimeException("Incorrect subscribeChannel() params format", e);
  76. } catch (IOException e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. public static JSONObject unSubscribeChannel(final String authToken, final String channelName) {
  81. final JSONObject requestParams = new JSONObject();
  82. try {
  83. requestParams.put(Params.AUTH_TOKEN, authToken);
  84. requestParams.put(Params.CHANNEL, channelName);
  85. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.UNSUBSCRIBE, requestParams.toString()));
  86. } catch (JSONException e) {
  87. throw new RuntimeException("Incorrect unSubscribeChannel() params format", e);
  88. } catch (IOException e) {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. public static JSONObject applyMark(final String authToken, final String channelName,
  93. final String description, final String link,
  94. final double lat, final double lon,
  95. final Date time, final String title) {
  96. final JSONObject requestParams = new JSONObject();
  97. try {
  98. requestParams.put(Params.AUTH_TOKEN, authToken);
  99. requestParams.put(Params.CHANNEL, channelName);
  100. requestParams.put(Params.DESCRIPTION, description);
  101. requestParams.put(Params.LINK, StringUtils.nvl(link, "unknown"));
  102. requestParams.put(Params.LATITUDE, lat);
  103. requestParams.put(Params.LONGITUDE, lon);
  104. requestParams.put(Params.TIME, DATE_FORMAT.format(time));
  105. requestParams.put(Params.TITLE, title);
  106. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.APPLY_MARK, requestParams.toString()));
  107. } catch (JSONException e) {
  108. throw new RuntimeException("Incorrect applyMark() params format", e);
  109. } catch (IOException e) {
  110. throw new RuntimeException(e);
  111. }
  112. }
  113. public static JSONObject addChannel(final String authToken, final String channelName,
  114. final String description, final String url, final double activeRadius) {
  115. final JSONObject requestParams = new JSONObject();
  116. try {
  117. requestParams.put(Params.AUTH_TOKEN, authToken);
  118. requestParams.put(Params.NAME, channelName);
  119. requestParams.put(Params.DESCRIPTION, description);
  120. requestParams.put(Params.URL, url);
  121. requestParams.put(Params.ACTIVE_RADIUS, activeRadius);
  122. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.ADD_CHANNEL, requestParams.toString()));
  123. } catch (JSONException e) {
  124. throw new RuntimeException("Incorrect addChannel() params format", e);
  125. } catch (IOException e) {
  126. throw new RuntimeException(e);
  127. }
  128. }
  129. public static JSONObject rssFeed(final String authToken, final double lat, final double lon, final double radius) {
  130. final JSONObject requestParams = new JSONObject();
  131. try {
  132. requestParams.put(Params.AUTH_TOKEN, authToken);
  133. requestParams.put(Params.LATITUDE, lat);
  134. requestParams.put(Params.LONGITUDE, lon);
  135. requestParams.put(Params.RADIUS, radius);
  136. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.RSS_FEED, requestParams.toString()));
  137. } catch (JSONException e) {
  138. throw new RuntimeException("Incorrect rssFeed() params format", e);
  139. } catch (IOException e) {
  140. throw new RuntimeException(e);
  141. }
  142. }
  143. public static JSONObject getTimeSlotOfChannel(final String authToken, final String channelName) {
  144. final JSONObject requestParams = new JSONObject();
  145. try {
  146. requestParams.put(Params.AUTH_TOKEN, authToken);
  147. requestParams.put(Params.CHANNEL, channelName);
  148. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.GET_TIME_SLOT_OF_CHANNEL, requestParams.toString()));
  149. } catch (JSONException e) {
  150. throw new RuntimeException("Incorrect getTimeSlotOfChannel() params format", e);
  151. } catch (IOException e) {
  152. throw new RuntimeException(e);
  153. }
  154. }
  155. public static JSONObject setTimeSlotForChannel(final String authToken, final String channelName, final long timeSlotInMinutes) {
  156. final JSONObject requestParams = new JSONObject();
  157. try {
  158. requestParams.put(Params.AUTH_TOKEN, authToken);
  159. requestParams.put(Params.CHANNEL, channelName);
  160. requestParams.put(Params.TIME_SLOT, timeSlotInMinutes);
  161. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SET_TIME_SLOT_FOR_CHANNEL, requestParams.toString()));
  162. } catch (JSONException e) {
  163. throw new RuntimeException("Incorrect setTimeSlotForChannel() params format", e);
  164. } catch (IOException e) {
  165. throw new RuntimeException(e);
  166. }
  167. }
  168. public static JSONObject setDefaultTimeSlotForChannel(final String authToken, final String channelName) {
  169. final JSONObject requestParams = new JSONObject();
  170. try {
  171. requestParams.put(Params.AUTH_TOKEN, authToken);
  172. requestParams.put(Params.CHANNEL, channelName);
  173. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SET_DEFAULT_TIME_SLOT_FOR_CHANNEL, requestParams.toString()));
  174. } catch (JSONException e) {
  175. throw new RuntimeException("Incorrect setDefaultTimeSlotForChannel() params format", e);
  176. } catch (IOException e) {
  177. throw new RuntimeException(e);
  178. }
  179. }
  180. public static JSONObject getTimeSlotOfMark(final String authToken, final long markId) {
  181. final JSONObject requestParams = new JSONObject();
  182. try {
  183. requestParams.put(Params.AUTH_TOKEN, authToken);
  184. requestParams.put(Params.MARK_ID, markId);
  185. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.GET_TIME_SLOT_OF_MARK, requestParams.toString()));
  186. } catch (JSONException e) {
  187. throw new RuntimeException("Incorrect getTimeSlotOfMark() params format", e);
  188. } catch (IOException e) {
  189. throw new RuntimeException(e);
  190. }
  191. }
  192. public static JSONObject setTimeSlotForMark(final String authToken, final long markId, final long timeSlotInMinutes) {
  193. final JSONObject requestParams = new JSONObject();
  194. try {
  195. requestParams.put(Params.AUTH_TOKEN, authToken);
  196. requestParams.put(Params.MARK_ID, markId);
  197. requestParams.put(Params.TIME_SLOT, timeSlotInMinutes);
  198. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SET_TIME_SLOT_FOR_MARK, requestParams.toString()));
  199. } catch (JSONException e) {
  200. throw new RuntimeException("Incorrect setTimeSlotForMark() params format", e);
  201. } catch (IOException e) {
  202. throw new RuntimeException(e);
  203. }
  204. }
  205. public static JSONObject setDefaultTimeSlotForMark(final String authToken, final long markId) {
  206. final JSONObject requestParams = new JSONObject();
  207. try {
  208. requestParams.put(Params.AUTH_TOKEN, authToken);
  209. requestParams.put(Params.MARK_ID, markId);
  210. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.SET_DEFAULT_TIME_SLOT_FOR_MARK, requestParams.toString()));
  211. } catch (JSONException e) {
  212. throw new RuntimeException("Incorrect setDefaultTimeSlotForMark() params format", e);
  213. } catch (IOException e) {
  214. throw new RuntimeException(e);
  215. }
  216. }
  217. @Deprecated
  218. public static JSONObject rssSession(final String authToken) {
  219. final JSONObject requestParams = new JSONObject();
  220. try {
  221. requestParams.put(Params.AUTH_TOKEN, authToken);
  222. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.RSS_SESSION, requestParams.toString()));
  223. } catch (JSONException e) {
  224. throw new RuntimeException("Incorrect rssSession() params format", e);
  225. } catch (IOException e) {
  226. throw new RuntimeException(e);
  227. }
  228. }
  229. @Deprecated
  230. public static JSONObject logout(final String authToken) {
  231. final JSONObject requestParams = new JSONObject();
  232. try {
  233. requestParams.put(Params.AUTH_TOKEN, authToken);
  234. return new JSONObject(ConnectionUtils.sendPostRequest(HOST + Services.QUIT, requestParams.toString()));
  235. } catch (JSONException e) {
  236. throw new RuntimeException("Incorrect auth_token format: {" + authToken + "}", e);
  237. } catch (IOException e) {
  238. throw new RuntimeException(e);
  239. }
  240. }
  241. }