PageRenderTime 78ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dumbhippo/trunk/server/src/com/dumbhippo/services/smugmug/SmugMugWebServices.java

https://gitlab.com/manoj-makkuboy/magnetism
Java | 209 lines | 180 code | 29 blank | 0 comment | 13 complexity | 23858ce0f66d8c9d1c0e9a6868807b75 MD5 | raw file
  1. package com.dumbhippo.services.smugmug;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.Hashtable;
  10. import java.util.List;
  11. import java.util.Set;
  12. import java.util.Map.Entry;
  13. import com.dumbhippo.*;
  14. import com.dumbhippo.services.smugmug.rest.bind.*;
  15. import com.dumbhippo.services.smugmug.rest.bind.types.RspStatType;
  16. public class SmugMugWebServices
  17. {
  18. private static final Object HTTP_PROTOCOL = "http";
  19. private static final Object SMUGMUG_URL = "api.smugmug.com/hack";
  20. private static final Object REQUEST_PROTOCOL = "rest";
  21. private static final Object API_VERSION = "1.2.0";
  22. private int timeoutMilliseconds = 8000;
  23. private String apiKey = null;
  24. public SmugMugWebServices(int timeout, String apiKey)
  25. {
  26. timeoutMilliseconds = timeout;
  27. this.apiKey = apiKey;
  28. }
  29. private boolean validateResponse(Rsp response)
  30. {
  31. boolean result = false;
  32. if (response != null && response.getStat() == RspStatType.OK)
  33. result = true;
  34. return result;
  35. }
  36. private URL buildRequestUrl(String method, Set<Entry<String, String>> args) throws MalformedURLException
  37. {
  38. String urlString = String.format("%1$s://%2$s/%3$s/%4$s?method=%5$s",
  39. new Object[]{HTTP_PROTOCOL, SMUGMUG_URL, REQUEST_PROTOCOL, API_VERSION, method});
  40. StringBuilder params = new StringBuilder();
  41. for(Entry<String, String> en:args)
  42. {
  43. params.append("&");
  44. params.append(en.getKey());
  45. params.append("=");
  46. params.append(StringUtils.urlEncode(en.getValue()));
  47. }
  48. urlString = urlString + params.toString();;
  49. URL url = new URL(urlString);
  50. return url;
  51. }
  52. private Rsp doSmugMugCall(SmugMugMethod method) throws Exception
  53. {
  54. URL requestURL = buildRequestUrl(method.getName(), method.getParams());
  55. URLConnection connection = URLUtils.openConnection(requestURL);
  56. connection.setConnectTimeout(timeoutMilliseconds);
  57. connection.setReadTimeout(timeoutMilliseconds);
  58. Rsp response = new Rsp();
  59. InputStream input = connection.getInputStream();
  60. response = Rsp.unmarshal(new InputStreamReader(input));
  61. return response;
  62. }
  63. private Exception doException(SmugMugMethod method, Err error, Hashtable<String, String> params)
  64. {
  65. Set<Entry<String, String>> paramSet = params.entrySet();
  66. StringBuffer paramList = new StringBuffer("Param list:");
  67. for(Entry<String, String> en:paramSet)
  68. {
  69. paramList.append(" name=");
  70. paramList.append(en.getKey());
  71. paramList.append(" value=");
  72. paramList.append(en.getValue());
  73. paramList.append(";");
  74. }
  75. String message = String.format("Method %1$s has returned error (code = %2$s, msg=%3$s), %4$s",
  76. new Object[]{method.getName().toString(), error.getCode(), error.getMsg(), paramList.toString()});
  77. return new Exception(message);
  78. }
  79. public Login loginAnonymously() throws Exception
  80. {
  81. Hashtable<String, String> params = new Hashtable<String, String>();
  82. params.put("APIKey", apiKey);
  83. SmugMugMethod method = new SmugMugMethod(SmugMugMethodsEnum.smugmug_login_anonymously);
  84. method.addAll(params);
  85. Rsp response = doSmugMugCall(method);
  86. if (validateResponse(response))
  87. return response.getRspChoice().getLogin();
  88. else
  89. throw doException(method, response.getRspChoice().getErr(), params);
  90. }
  91. public Login loginWithPassword(String emailAddress, String password) throws Exception
  92. {
  93. Hashtable<String, String> params = new Hashtable<String, String>();
  94. params.put("APIKey", apiKey);
  95. params.put("EmailAddress", emailAddress);
  96. params.put("Password", password);
  97. SmugMugMethod method = new SmugMugMethod(SmugMugMethodsEnum.smugmug_login_withPassword);
  98. method.addAll(params);
  99. Rsp response = doSmugMugCall(method);
  100. if (validateResponse(response))
  101. return response.getRspChoice().getLogin();
  102. else
  103. throw doException(method, response.getRspChoice().getErr(), params);
  104. }
  105. public boolean ping(String sessionId, String nickName) throws Exception
  106. {
  107. Hashtable<String, String> params = new Hashtable<String, String>();
  108. params.put("SessionID", sessionId);
  109. params.put("NickName", nickName);
  110. SmugMugMethod method = new SmugMugMethod(SmugMugMethodsEnum.smugmug_albums_get);
  111. method.addAll(params);
  112. Rsp response = doSmugMugCall(method);
  113. return validateResponse(response);
  114. }
  115. public Album[] getAlbums(String sessionId, String nickName) throws Exception
  116. {
  117. Hashtable<String, String> params = new Hashtable<String, String>();
  118. params.put("SessionID", sessionId);
  119. params.put("NickName", nickName);
  120. SmugMugMethod method = new SmugMugMethod(SmugMugMethodsEnum.smugmug_albums_get);
  121. method.addAll(params);
  122. Rsp response = doSmugMugCall(method);
  123. if (validateResponse(response))
  124. return response.getRspChoice().getAlbums().getAlbum();
  125. else
  126. throw doException(method, response.getRspChoice().getErr(), params);
  127. }
  128. public Image[] getImages(String sessionId, String nickName, boolean heavy,
  129. String albumId, String albumKey) throws Exception
  130. {
  131. Hashtable<String, String> params = new Hashtable<String, String>();
  132. params.put("SessionID", sessionId);
  133. params.put("NickName", nickName);
  134. params.put("Heavy", (heavy)?"1":"0");
  135. params.put("AlbumID", albumId);
  136. params.put("AlbumKey", albumKey);
  137. SmugMugMethod method = new SmugMugMethod(SmugMugMethodsEnum.smugmug_images_get);
  138. method.addAll(params);
  139. Rsp response = doSmugMugCall(method);
  140. if (validateResponse(response))
  141. return response.getRspChoice().getImages().getImage();
  142. else
  143. throw doException(method, response.getRspChoice().getErr(), params);
  144. }
  145. public Image[] getLastImages(String nickName, int imgCount) throws Exception
  146. {
  147. Login login = loginAnonymously();
  148. Image[] images = getAllImages(login.getSession().getId(), nickName);
  149. Arrays.sort(images, new java.util.Comparator<Image>()
  150. {
  151. public int compare(Image i1, Image i2)
  152. {
  153. return i1.getId().compareTo(i2.getId());
  154. }
  155. public boolean equals(Object img)
  156. {
  157. return this == img;
  158. }
  159. });
  160. List<Image> imgList = Arrays.asList(images);
  161. java.util.Collections.reverse(imgList);
  162. images = imgList.toArray(images);
  163. if (imgCount < images.length)
  164. images = Arrays.copyOfRange(images, 0, imgCount);
  165. return images;
  166. }
  167. public Image[] getAllImages(String sessionId, String nickName) throws Exception
  168. {
  169. Album[] albums = getAlbums(sessionId, nickName);
  170. ArrayList<Image> imageList = new ArrayList<Image>();
  171. for(Album a:albums)
  172. {
  173. Image[] images = getImages(sessionId, nickName, true, a.getId(), a.getKey());
  174. List<Image> list = Arrays.asList(images);
  175. imageList.addAll(list);
  176. }
  177. Image[] res = new Image[0];
  178. return imageList.toArray(res);
  179. }
  180. }