Guest User

Untitled

a guest
Jun 13th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. package com.socialtext;
  2.  
  3. public class Resting
  4. {
  5. public enum Route
  6. {
  7. PAGE("/data/workspaces/%s/pages/%s"),
  8. PAGES("/data/workspaces/%s/pages"),
  9. PAGETAG("/data/workspaces/%s/pages/%s/tags/%s"),
  10. PAGECOMMENTS("/data/workspaces/%s/pages/%s/comments"),
  11. PAGEATTACHMENT("/data/workspaces/%s/pages/%s/attachments/%s"),
  12. PAGEATTACHMENTS("/data/workspaces/%s/pages/%s/attachments"),
  13. PEOPLE("/data/people"),
  14. PERSON("/data/person/%s"),
  15. SIGNALS("/data/signals"),
  16. TAGGEDPAGES("/data/workspaces/%s/tags/%s/pages"),
  17. WORKSPACE("/data/workspaces/%s"),
  18. WORKSPACES("/data/workspaces"),
  19. WORKSPACETAG("/data/workspaces/%s/tags/%s"),
  20. WORKSPACETAGS("/data/workspaces/%s/tags"),
  21. WORKSPACEATTACHMENT("/data/workspaces/%s/attachments/%s"),
  22. WORKSPACEATTACHMENTS("/data/workspaces/%s/attachments"),
  23. WORKSPACEUSER("/data/workspaces/%s/users/%s"),
  24. WORKSPACEUSERS("/data/workspaces/%s/users"),
  25. USER("/data/users/%s"),
  26. USERS("/data/users");
  27.  
  28. private String m_path;
  29.  
  30. private Route(String path)
  31. {
  32. m_path = path;
  33. }
  34.  
  35. public String getPath()
  36. {
  37. return m_path;
  38. }
  39.  
  40. public static String getRoute(String name)
  41. throws IllegalArgumentException
  42. {
  43. Route r = Route.valueOf(name.trim().toUpperCase());
  44. return r.getPath();
  45. }
  46. }
  47.  
  48. public enum Mimetype
  49. {
  50. TEXT("text/plain"),
  51. HTML("text/html"),
  52. JSON("application/json"),
  53. WIKI("application/x-socialtext-wiki");
  54.  
  55. private String m_mimetype;
  56.  
  57. private Mimetype(String type)
  58. {
  59. m_mimetype = type;
  60. }
  61.  
  62. public String getType()
  63. {
  64. return m_mimetype;
  65. }
  66. }
  67.  
  68. public enum Method
  69. {
  70. GET,
  71. PUT,
  72. POST,
  73. DELETE;
  74. }
  75.  
  76. private String m_site_url;
  77. private String m_username;
  78. private String m_password;
  79. private String m_workspace;
  80. private Mimetype m_accept = Mimetype.JSON; /* JSON by default */
  81.  
  82. /**
  83. * Creates a Socialtext ReST connection.
  84. *
  85. * @param url The URL of the connection.
  86. */
  87. public Resting(String url)
  88. {
  89. m_site_url = url;
  90. m_username = "";
  91. m_password = "";
  92. m_workspace = "";
  93. }
  94.  
  95. /**
  96. * Creates a Socialtext ReST connection.
  97. *
  98. * @param url The URL of the connection.
  99. * @param username The username for authentication.
  100. * @param password The password for authnetication.
  101. */
  102. public Resting(String url, String username, String password)
  103. {
  104. m_site_url = url;
  105. m_username = username;
  106. m_password = password;
  107. m_workspace = "";
  108. }
  109.  
  110. /**
  111. * Creates a Socialtext ReST connection.
  112. *
  113. * @param url The URL of the connection.
  114. * @param username The username for authentication.
  115. * @param password The password for authnetication.
  116. * @param ws The workspace name.
  117. */
  118. public Resting(String url, String username, String password, String ws)
  119. {
  120. m_site_url = url;
  121. m_username = username;
  122. m_password = password;
  123. m_workspace = ws;
  124. }
  125.  
  126. /**
  127. * Returns the username of the authenticated user.
  128. *
  129. * @return The user's username.
  130. */
  131. public String getUsername()
  132. {
  133. return m_username;
  134. }
  135.  
  136. /**
  137. * Sets the username and password for the connection.
  138. *
  139. * @param username The username to use for authentication.
  140. * @param password The password to use for authentication.
  141. */
  142. public void setCredentials(String username, String password)
  143. {
  144. m_username = username;
  145. m_password = password;
  146. }
  147.  
  148. /**
  149. * Gets the current workspace.
  150. *
  151. * @return The workspace name.
  152. */
  153. public String getWorkspace()
  154. {
  155. return m_workspace;
  156. }
  157.  
  158. /**
  159. * Sets the current workspace.
  160. *
  161. * @param workspace The name of the workspace.
  162. */
  163. public void setWorkspace(String workspace)
  164. {
  165. m_workspace = workspace;
  166. }
  167.  
  168. /**
  169. * Private function to actually send the HTTP request.
  170. * Maybe we need a callback here that is passed as a parameter too?
  171. *
  172. * @param url The url to request.
  173. * @param method The method (GET, POST, PUT, DELETE)
  174. * @param contenttype The content type (text/html, text/plain,
  175. * text/x.socialtext-wiki, application/json)
  176. * @param accepttype The type we accept (see contenttype for values)
  177. * @param data The data that we are sending (if any)
  178. */
  179. private void request(String url, Method method, Mimetype contenttype,
  180. Mimetype accepttype, String data)
  181. {
  182. if (accepttype == null)
  183. {
  184. accepttype = m_accept;
  185. }
  186. /* We send the HTTP request here */
  187. /* Make sure we auth with username/password! */
  188. }
  189.  
  190. /**
  191. * Posts a signal to the socialtext stream.
  192. *
  193. * @param signal The signal object to be posted.
  194. */
  195. public void postSignal(Signal signal)
  196. {
  197. String path = m_site_url + Route.getRoute("SIGNALS");
  198.  
  199. request(path, Method.POST, Mimetype.JSON, Mimetype.TEXT,
  200. signal.toJSON());
  201. }
  202. }
Add Comment
Please, Sign In to add comment