PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/egats/API.java

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