PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/egats/API.java

https://github.com/wgychris/egats
Java | 570 lines | 312 code | 33 blank | 225 comment | 79 complexity | 73b6043f3a7e7bdb18e38484fae7e3f2 MD5 | raw file
  1. package egats;
  2. import com.mongodb.util.JSON;
  3. import java.io.IOException;
  4. import java.io.OutputStreamWriter;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import org.apache.commons.io.IOUtils;
  10. import org.apache.commons.lang3.StringUtils;
  11. /**
  12. *
  13. * @author Augie Hill - augie@umich.edu
  14. */
  15. public class API {
  16. private static String HOST = "localhost:55555";
  17. public static final String PING = "/ping";
  18. public static final String MIRROR = "/mirror";
  19. public static final String REFRESH = "/refresh";
  20. public static final String OBJECTS_FOLDER = "/o/";
  21. public static final String PROCESSES_FOLDER = "/p/";
  22. public static final String PROCESSES_BY_TIMESTAMP_FOLDER = "/pt/";
  23. public static final String WORKFLOWS_FOLDER = "/w/";
  24. public static final String WORKFLOWS_BY_TIMESTAMP_FOLDER = "/wt/";
  25. public static final String TOOLKIT_FOLDER = "/t/";
  26. /**
  27. * The IP or domain name (and optionally the port number) of the EGATS server.
  28. */
  29. public static String getHost() {
  30. return HOST;
  31. }
  32. /**
  33. * Sets the IP or domain name (and optionally the port number) of the EGATS server.
  34. * @param host
  35. */
  36. public static void setHost(String host) {
  37. HOST = host;
  38. if (HOST.startsWith("http://")) {
  39. HOST = HOST.substring(7);
  40. }
  41. if (HOST.startsWith("https://")) {
  42. HOST = HOST.substring(8);
  43. }
  44. if (!HOST.endsWith("/")) {
  45. HOST = HOST + "/";
  46. }
  47. }
  48. /**
  49. * HTTP GET request.
  50. * @param url
  51. * @return
  52. * @throws IOException
  53. */
  54. public static Response send(String url) throws IOException {
  55. if (url == null) {
  56. throw new NullPointerException("URL is null.");
  57. }
  58. Response response = Response.fromJSON(IOUtils.toString(new URL(url).openStream()));
  59. if (response.getStatusCode() != Response.STATUS_CODE_OK) {
  60. throw new IOException(response.getMessage());
  61. }
  62. return response;
  63. }
  64. /**
  65. * HTTP POST request.
  66. * @param url
  67. * @param body
  68. * @return
  69. * @throws IOException
  70. */
  71. public static Response send(String url, String body) throws IOException {
  72. // Checks
  73. if (url == null) {
  74. throw new NullPointerException("URL is null.");
  75. }
  76. if (body == null) {
  77. throw new NullPointerException("Body is null.");
  78. }
  79. // Send
  80. OutputStreamWriter wr = null;
  81. try {
  82. // Send data
  83. URL urlObj = new URL(url);
  84. URLConnection conn = urlObj.openConnection();
  85. conn.setDoOutput(true);
  86. wr = new OutputStreamWriter(conn.getOutputStream());
  87. wr.write(body);
  88. wr.flush();
  89. // Get the response
  90. Response response = Response.fromJSON(IOUtils.toString(conn.getInputStream()));
  91. if (response.getStatusCode() != Response.STATUS_CODE_OK) {
  92. throw new IOException(response.getMessage());
  93. }
  94. return response;
  95. } finally {
  96. IOUtils.closeQuietly(wr);
  97. }
  98. }
  99. /**
  100. *
  101. * @param folder
  102. * @return
  103. */
  104. public static String getURL(String folder) {
  105. return getURL(folder, null);
  106. }
  107. /**
  108. *
  109. * @param folder
  110. * @param param
  111. * @return
  112. */
  113. public static String getURL(String folder, String param) {
  114. StringBuilder sb = new StringBuilder();
  115. sb.append("http://");
  116. sb.append(HOST);
  117. if (folder != null) {
  118. if (folder.startsWith("/")) {
  119. folder = folder.substring(1);
  120. }
  121. if (!folder.endsWith("/")) {
  122. folder = folder + "/";
  123. }
  124. sb.append(folder);
  125. }
  126. if (param != null) {
  127. if (param.startsWith("/")) {
  128. param = param.substring(1);
  129. }
  130. if (param.endsWith("/")) {
  131. param = param.substring(0, param.length() - 1);
  132. }
  133. sb.append(param);
  134. }
  135. return sb.toString();
  136. }
  137. /**
  138. *
  139. * @param id
  140. * @return
  141. * @throws IOException
  142. */
  143. public static String getObjectJSON(String id) throws IOException {
  144. // Checks
  145. if (id == null) {
  146. throw new NullPointerException("ID is null.");
  147. }
  148. // Get
  149. List<String> ids = new LinkedList<String>();
  150. ids.add(id);
  151. return getObjectsJSON(ids).get(0);
  152. }
  153. /**
  154. *
  155. * @param ids
  156. * @return
  157. * @throws IOException
  158. */
  159. public static List<String> getObjectsJSON(List<String> ids) throws IOException {
  160. // Check
  161. if (ids == null) {
  162. throw new NullPointerException("ID list is null.");
  163. }
  164. // Get
  165. List<String> objectsJSON = new LinkedList<String>();
  166. if (!ids.isEmpty()) {
  167. Response response = send(getURL(OBJECTS_FOLDER, StringUtils.join(ids, ",")));
  168. for (Object o : (List<Object>) JSON.parse(response.getBody())) {
  169. objectsJSON.add(JSON.serialize(o));
  170. }
  171. }
  172. return objectsJSON;
  173. }
  174. /**
  175. *
  176. * @param id
  177. * @return
  178. * @throws Exception
  179. */
  180. public static EGATSObject getObject(String id) throws Exception {
  181. // Check
  182. if (id == null) {
  183. throw new NullPointerException("ID is null.");
  184. }
  185. // Get
  186. return EGATSObject.read(API.getObjectJSON(id));
  187. }
  188. /**
  189. *
  190. * @param ids
  191. * @return
  192. * @throws Exception
  193. */
  194. public static List<EGATSObject> getObjects(List<String> ids) throws Exception {
  195. // Check
  196. if (ids == null) {
  197. throw new NullPointerException("ID list is null.");
  198. }
  199. // Get
  200. List<EGATSObject> objects = new LinkedList<EGATSObject>();
  201. for (String objectJSON : getObjectsJSON(ids)) {
  202. objects.add(EGATSObject.read(objectJSON));
  203. }
  204. return objects;
  205. }
  206. /**
  207. *
  208. * @param object
  209. * @return
  210. * @throws Exception
  211. */
  212. public static String createObject(EGATSObject object) throws Exception {
  213. // Check
  214. if (object == null) {
  215. throw new NullPointerException("Object is null.");
  216. }
  217. // Send
  218. List<EGATSObject> list = new LinkedList<EGATSObject>();
  219. list.add(object);
  220. return createObjects(list).get(0);
  221. }
  222. /**
  223. *
  224. * @param objects
  225. * @return
  226. * @throws Exception
  227. */
  228. public static List<String> createObjects(List<EGATSObject> objects) throws Exception {
  229. // Check
  230. if (objects == null) {
  231. throw new NullPointerException("Object list is null.");
  232. }
  233. // Send
  234. if (!objects.isEmpty()) {
  235. Response response = send(getURL(OBJECTS_FOLDER), JSON.serialize(objects));
  236. return (List<String>) JSON.parse(response.getBody());
  237. }
  238. return new LinkedList<String>();
  239. }
  240. /**
  241. *
  242. * @param name
  243. * @param object
  244. * @return
  245. * @throws Exception
  246. */
  247. public static String createObjectFile(String name, String object) throws Exception {
  248. // Wrap the file info (name is the file name, object is the file contents)
  249. EGATSObjectFile egatsObjectFile = new EGATSObjectFile(name, object);
  250. // Create
  251. EGATSObject egatsObject = new EGATSObject();
  252. egatsObject.setClassPath(EGATSObjectFile.class.getName());
  253. egatsObject.setObject(Data.GSON.toJson(egatsObjectFile));
  254. return createObject(egatsObject);
  255. }
  256. /**
  257. *
  258. * @param id
  259. * @return
  260. * @throws IOException
  261. */
  262. public static String getProcessJSON(String id) throws IOException {
  263. // Checks
  264. if (id == null) {
  265. throw new NullPointerException("ID is null.");
  266. }
  267. // Get
  268. List<String> ids = new LinkedList<String>();
  269. ids.add(id);
  270. return getProcessesJSON(ids).get(0);
  271. }
  272. /**
  273. *
  274. * @param ids
  275. * @return
  276. * @throws IOException
  277. */
  278. public static List<String> getProcessesJSON(List<String> ids) throws IOException {
  279. // Check
  280. if (ids == null) {
  281. throw new NullPointerException("ID list is null.");
  282. }
  283. // Get
  284. List<String> processesJSON = new LinkedList<String>();
  285. if (!ids.isEmpty()) {
  286. Response response = send(getURL(PROCESSES_FOLDER, StringUtils.join(ids, ",")));
  287. for (Object o : (List<Object>) JSON.parse(response.getBody())) {
  288. processesJSON.add(JSON.serialize(o));
  289. }
  290. }
  291. return processesJSON;
  292. }
  293. /**
  294. *
  295. * @param id
  296. * @return
  297. * @throws Exception
  298. */
  299. public static EGATSProcess getProcess(String id) throws Exception {
  300. // Check
  301. if (id == null) {
  302. throw new NullPointerException("ID is null.");
  303. }
  304. // Get
  305. return EGATSProcess.read(API.getProcessJSON(id));
  306. }
  307. /**
  308. *
  309. * @param ids
  310. * @return
  311. * @throws Exception
  312. */
  313. public static List<EGATSProcess> getProcesses(List<String> ids) throws Exception {
  314. // Check
  315. if (ids == null) {
  316. throw new NullPointerException("ID list is null.");
  317. }
  318. // Get
  319. List<EGATSProcess> processes = new LinkedList<EGATSProcess>();
  320. for (String processJSON : getProcessesJSON(ids)) {
  321. processes.add(EGATSProcess.read(processJSON));
  322. }
  323. return processes;
  324. }
  325. /**
  326. *
  327. * @param process
  328. * @return
  329. * @throws Exception
  330. */
  331. public static String createProcess(EGATSProcess process) throws Exception {
  332. // Check
  333. if (process == null) {
  334. throw new NullPointerException("Process is null.");
  335. }
  336. // Send
  337. List<EGATSProcess> list = new LinkedList<EGATSProcess>();
  338. list.add(process);
  339. return createProcesses(list).get(0);
  340. }
  341. /**
  342. *
  343. * @param processes
  344. * @return
  345. * @throws Exception
  346. */
  347. public static List<String> createProcesses(List<EGATSProcess> processes) throws Exception {
  348. // Check
  349. if (processes == null) {
  350. throw new NullPointerException("Process list is null.");
  351. }
  352. // Send
  353. if (!processes.isEmpty()) {
  354. Response response = send(getURL(PROCESSES_FOLDER), JSON.serialize(processes));
  355. return (List<String>) JSON.parse(response.getBody());
  356. }
  357. return new LinkedList<String>();
  358. }
  359. /**
  360. *
  361. * @param createTime
  362. * @return
  363. * @throws IOException
  364. */
  365. public static List<String> getProcessesByTimestampJSON(Long createTime) throws IOException {
  366. // Check
  367. if (createTime == null || createTime < 0) {
  368. createTime = 0l;
  369. }
  370. // Get
  371. Response response = send(getURL(PROCESSES_BY_TIMESTAMP_FOLDER, String.valueOf(createTime.longValue())));
  372. List<String> processesJSON = new LinkedList<String>();
  373. for (Object o : (List<Object>) JSON.parse(response.getBody())) {
  374. processesJSON.add(JSON.serialize(o));
  375. }
  376. return processesJSON;
  377. }
  378. /**
  379. *
  380. * @param createTime
  381. * @return
  382. * @throws Exception
  383. */
  384. public static List<EGATSProcess> getProcessesByTimestamp(Long createTime) throws Exception {
  385. List<String> processesJSON = getProcessesByTimestampJSON(createTime);
  386. List<EGATSProcess> processes = new LinkedList<EGATSProcess>();
  387. for (String processJSON : processesJSON) {
  388. processes.add(EGATSProcess.read(processJSON));
  389. }
  390. return processes;
  391. }
  392. /**
  393. *
  394. * @param id
  395. * @return
  396. * @throws IOException
  397. */
  398. public static String getWorkflowJSON(String id) throws IOException {
  399. // Checks
  400. if (id == null) {
  401. throw new NullPointerException("ID is null.");
  402. }
  403. // Get
  404. List<String> ids = new LinkedList<String>();
  405. ids.add(id);
  406. return getWorkflowsJSON(ids).get(0);
  407. }
  408. /**
  409. *
  410. * @param ids
  411. * @return
  412. * @throws IOException
  413. */
  414. public static List<String> getWorkflowsJSON(List<String> ids) throws IOException {
  415. // Check
  416. if (ids == null) {
  417. throw new NullPointerException("ID list is null.");
  418. }
  419. // Get
  420. List<String> workflowsJSON = new LinkedList<String>();
  421. if (!ids.isEmpty()) {
  422. Response response = send(getURL(WORKFLOWS_FOLDER, StringUtils.join(ids, ",")));
  423. for (Object o : (List<Object>) JSON.parse(response.getBody())) {
  424. workflowsJSON.add(JSON.serialize(o));
  425. }
  426. }
  427. return workflowsJSON;
  428. }
  429. /**
  430. *
  431. * @param id
  432. * @return
  433. * @throws Exception
  434. */
  435. public static EGATSWorkflow getWorkflow(String id) throws Exception {
  436. // Check
  437. if (id == null) {
  438. throw new NullPointerException("ID is null.");
  439. }
  440. // Get
  441. return EGATSWorkflow.read(API.getWorkflowJSON(id));
  442. }
  443. /**
  444. *
  445. * @param ids
  446. * @return
  447. * @throws Exception
  448. */
  449. public static List<EGATSWorkflow> getWorkflows(List<String> ids) throws Exception {
  450. // Check
  451. if (ids == null) {
  452. throw new NullPointerException("ID list is null.");
  453. }
  454. // Get
  455. List<EGATSWorkflow> workflows = new LinkedList<EGATSWorkflow>();
  456. for (String workflowJSON : getWorkflowsJSON(ids)) {
  457. workflows.add(EGATSWorkflow.read(workflowJSON));
  458. }
  459. return workflows;
  460. }
  461. /**
  462. *
  463. * @param workflow
  464. * @return
  465. * @throws Exception
  466. */
  467. public static String createWorkflow(EGATSWorkflow workflow) throws Exception {
  468. // Check
  469. if (workflow == null) {
  470. throw new NullPointerException("Workflow is null.");
  471. }
  472. // Send
  473. List<EGATSWorkflow> list = new LinkedList<EGATSWorkflow>();
  474. list.add(workflow);
  475. return createWorkflows(list).get(0);
  476. }
  477. /**
  478. *
  479. * @param workflows
  480. * @return
  481. * @throws Exception
  482. */
  483. public static List<String> createWorkflows(List<EGATSWorkflow> workflows) throws Exception {
  484. // Check
  485. if (workflows == null) {
  486. throw new NullPointerException("Process list is null.");
  487. }
  488. // Send
  489. if (!workflows.isEmpty()) {
  490. Response response = send(getURL(WORKFLOWS_FOLDER), JSON.serialize(workflows));
  491. return (List<String>) JSON.parse(response.getBody());
  492. }
  493. return new LinkedList<String>();
  494. }
  495. /**
  496. *
  497. * @param createTime
  498. * @return
  499. * @throws IOException
  500. */
  501. public static List<String> getWorkflowsByTimestampJSON(Long createTime) throws IOException {
  502. // Check
  503. if (createTime == null || createTime < 0) {
  504. createTime = 0l;
  505. }
  506. // Get
  507. Response response = send(getURL(WORKFLOWS_BY_TIMESTAMP_FOLDER, String.valueOf(createTime.longValue())));
  508. List<String> workflowsJSON = new LinkedList<String>();
  509. for (Object o : (List<Object>) JSON.parse(response.getBody())) {
  510. workflowsJSON.add(JSON.serialize(o));
  511. }
  512. return workflowsJSON;
  513. }
  514. /**
  515. *
  516. * @param createTime
  517. * @return
  518. * @throws Exception
  519. */
  520. public static List<EGATSWorkflow> getWorkflowsByTimestamp(Long createTime) throws Exception {
  521. List<String> workflowsJSON = getWorkflowsByTimestampJSON(createTime);
  522. List<EGATSWorkflow> workflows = new LinkedList<EGATSWorkflow>();
  523. for (String workflowJSON : workflowsJSON) {
  524. workflows.add(EGATSWorkflow.read(workflowJSON));
  525. }
  526. return workflows;
  527. }
  528. /**
  529. *
  530. * @return
  531. * @throws Exception
  532. */
  533. public static List<String> getToolkit() throws Exception {
  534. Response response = send(getURL(TOOLKIT_FOLDER));
  535. return (List<String>) JSON.parse(response.getBody());
  536. }
  537. }