PageRenderTime 44ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/core2/src/main/java/org/synbiohub/frontend/SynBioHubFrontend.java

http://github.com/SynBioDex/libSBOLj
Java | 3733 lines | 2213 code | 441 blank | 1079 comment | 130 complexity | cbd916f2495ee2d5415914759869196c MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.synbiohub.frontend;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.StringWriter;
  13. import java.io.UnsupportedEncodingException;
  14. import java.net.URI;
  15. import java.net.URLEncoder;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.Set;
  19. import org.apache.commons.io.IOUtils;
  20. import org.apache.http.HttpEntity;
  21. import org.apache.http.HttpResponse;
  22. import org.apache.http.NameValuePair;
  23. import org.apache.http.client.HttpClient;
  24. import org.apache.http.client.config.RequestConfig;
  25. import org.apache.http.client.entity.UrlEncodedFormEntity;
  26. import org.apache.http.client.methods.HttpGet;
  27. import org.apache.http.client.methods.HttpPost;
  28. import org.apache.http.client.methods.HttpRequestBase;
  29. import org.apache.http.entity.ContentType;
  30. import org.apache.http.entity.mime.HttpMultipartMode;
  31. import org.apache.http.entity.mime.MultipartEntityBuilder;
  32. import org.apache.http.impl.client.HttpClientBuilder;
  33. import org.apache.http.impl.client.HttpClients;
  34. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  35. import org.apache.http.message.BasicNameValuePair;
  36. import org.json.simple.JSONArray;
  37. import org.json.simple.JSONObject;
  38. import org.json.simple.parser.JSONParser;
  39. import org.sbolstandard.core2.SBOLDocument;
  40. import org.sbolstandard.core2.SBOLReader;
  41. import org.sbolstandard.core2.SBOLValidationException;
  42. import org.sbolstandard.core2.SBOLWriter;
  43. import org.sbolstandard.core2.TopLevel;
  44. import com.google.gson.Gson;
  45. import com.google.gson.reflect.TypeToken;
  46. /**
  47. * Provides a Java API to SynBioHub instances.
  48. * @author James McLaughlin
  49. * @author Chris Myers
  50. *
  51. */
  52. public class SynBioHubFrontend
  53. {
  54. PoolingHttpClientConnectionManager connectionManager;
  55. HttpClient client;
  56. String backendUrl;
  57. String uriPrefix;
  58. String user = "";
  59. String username = null;
  60. /**
  61. * Creates an instance of the SynBioHub API.
  62. * @param backendUrl - URL for the SynBioHub instance.
  63. * @param uriPrefix - prefix for all URIs stored in this repository
  64. */
  65. public SynBioHubFrontend(String backendUrl, String uriPrefix)
  66. {
  67. this.backendUrl = backendUrl;
  68. this.uriPrefix = uriPrefix;
  69. connectionManager = new PoolingHttpClientConnectionManager();
  70. client = HttpClients.custom().setConnectionManager(connectionManager).build();
  71. }
  72. /**
  73. * Creates an instance of the SynBioHub API.
  74. * @param backendUrl - URL for the SynBioHub instance.
  75. */
  76. public SynBioHubFrontend(String backendUrl)
  77. {
  78. this.backendUrl = backendUrl;
  79. this.uriPrefix = backendUrl;
  80. connectionManager = new PoolingHttpClientConnectionManager();
  81. client = HttpClients.custom().setConnectionManager(connectionManager).build();
  82. }
  83. /**
  84. * Creates an instance of the SynBioHub API.
  85. * @param backendUrl - URL for the SynBioHub instance.
  86. * @param timeout - timeout for connections in seconds
  87. */
  88. public SynBioHubFrontend(String backendUrl,int timeout)
  89. {
  90. this.backendUrl = backendUrl;
  91. this.uriPrefix = backendUrl;
  92. connectionManager = new PoolingHttpClientConnectionManager();
  93. // client = HttpClients.custom().setConnectionManager(connectionManager).build();
  94. RequestConfig config = RequestConfig.custom()
  95. .setConnectTimeout(timeout * 1000)
  96. .setConnectionRequestTimeout(timeout * 1000)
  97. .setSocketTimeout(timeout * 1000).build();
  98. client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
  99. }
  100. /**
  101. * Creates an instance of the SynBioHub API.
  102. * @param backendUrl - URL for the SynBioHub instance.
  103. * @param uriPrefix - prefix for all URIs stored in this repository
  104. * @param timeout - timeout for connections in seconds
  105. */
  106. public SynBioHubFrontend(String backendUrl, String uriPrefix, int timeout)
  107. {
  108. this.backendUrl = backendUrl;
  109. this.uriPrefix = uriPrefix;
  110. connectionManager = new PoolingHttpClientConnectionManager();
  111. RequestConfig config = RequestConfig.custom()
  112. .setConnectTimeout(timeout * 1000)
  113. .setConnectionRequestTimeout(timeout * 1000)
  114. .setSocketTimeout(timeout * 1000).build();
  115. client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
  116. }
  117. /**
  118. * Returns the URL for the SynBioHub instance.
  119. * @return the URL for the SynBioHub instance.
  120. */
  121. public String getBackendUrl()
  122. {
  123. return this.backendUrl;
  124. }
  125. /**
  126. * Return the total number of objects of a specified type in the repository.
  127. *
  128. * @return the total number of objects of a specified type in the repository.
  129. *
  130. * @param objectType The object type to count
  131. * (Collection, ComponentDefinition, Sequence, ModuleDefinition, Model, etc.).
  132. *
  133. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  134. */
  135. public int getCount(String objectType) throws SynBioHubException
  136. {
  137. return fetchCount(backendUrl + "/" + objectType + "/count");
  138. }
  139. /**
  140. * Retrieve SBOL TopLevel object from a SynBioHub instance using its URI.
  141. *
  142. * @param topLevelUri The URI of the SBOL TopLevel
  143. *
  144. * @return A libSBOLj TopLevel instance corresponding to the TopLevel
  145. *
  146. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  147. */
  148. public SBOLDocument getSBOL(URI topLevelUri) throws SynBioHubException
  149. {
  150. return getSBOL(topLevelUri,true);
  151. }
  152. /**
  153. * Retrieve SBOL TopLevel object from a SynBioHub instance using its URI.
  154. *
  155. * @param topLevelUri The URI of the SBOL TopLevel
  156. * @param recursive indicates if the complete SBOL document should be fetched recursively
  157. *
  158. * @return A libSBOLj TopLevel instance corresponding to the TopLevel
  159. *
  160. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  161. */
  162. public SBOLDocument getSBOL(URI topLevelUri,boolean recursive) throws SynBioHubException
  163. {
  164. if (topLevelUri==null) return null;
  165. if (!topLevelUri.toString().startsWith(uriPrefix)) {
  166. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  167. }
  168. String url = topLevelUri + "/sbol";
  169. if (!recursive) {
  170. url = topLevelUri + "/sbolnr";
  171. }
  172. url = url.replace(uriPrefix, backendUrl);
  173. try {
  174. SBOLDocument document = fetchFromSynBioHub(url);
  175. return document;
  176. } catch (NotFoundException e) {
  177. return null;
  178. }
  179. }
  180. /**
  181. * Retrieve a GFF3 version of a topLevel object from a SynBioHub instance using its URI,
  182. * and save to the path provided.
  183. *
  184. * @param topLevelURI The URI of the SBOL Attachment object
  185. * @param path The path to store the downloaded attachment
  186. * @return the name of the file being downloaded
  187. *
  188. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  189. * @throws IOException if there is an I/O error
  190. */
  191. public String getGFF3(URI topLevelURI, String path) throws SynBioHubException, IOException
  192. {
  193. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  194. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  195. }
  196. String url = topLevelURI + "/gff";
  197. url = url.replace(uriPrefix, backendUrl);
  198. return fetchContentSaveToFile(url,null,path);
  199. }
  200. /**
  201. * Retrieve a GFF3 version of a topLevel object from a SynBioHub instance using its URI,
  202. * and save into the provided output stream.
  203. *
  204. * @param topLevelURI The URI of the SBOL Attachment object
  205. * @param outputStream The output stream to store the downloaded attachment
  206. * @return the name of the file being downloaded
  207. *
  208. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  209. * @throws IOException if there is an I/O error
  210. */
  211. public String getGFF3(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  212. {
  213. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  214. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  215. }
  216. String url = topLevelURI + "/gff";
  217. url = url.replace(uriPrefix, backendUrl);
  218. return fetchContentSaveToFile(url,outputStream,null);
  219. }
  220. /**
  221. * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
  222. * and save to the path provided.
  223. *
  224. * @param topLevelURI The URI of the SBOL Attachment object
  225. * @param path The path to store the downloaded attachment
  226. * @return the name of the file being downloaded
  227. *
  228. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  229. * @throws IOException if there is an I/O error
  230. */
  231. public String getGenBank(URI topLevelURI, String path) throws SynBioHubException, IOException
  232. {
  233. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  234. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  235. }
  236. String url = topLevelURI + "/gb";
  237. url = url.replace(uriPrefix, backendUrl);
  238. return fetchContentSaveToFile(url,null,path);
  239. }
  240. /**
  241. * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
  242. * and save into the provided output stream.
  243. *
  244. * @param topLevelURI The URI of the SBOL Attachment object
  245. * @param outputStream The output stream to store the downloaded attachment
  246. * @return the name of the file being downloaded
  247. *
  248. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  249. * @throws IOException if there is an I/O error
  250. */
  251. public String getGenBank(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  252. {
  253. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  254. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  255. }
  256. String url = topLevelURI + "/gb";
  257. url = url.replace(uriPrefix, backendUrl);
  258. return fetchContentSaveToFile(url,outputStream,null);
  259. }
  260. /**
  261. * Retrieve a FASTA version of a topLevel object from a SynBioHub instance using its URI,
  262. * and save to the path provided.
  263. *
  264. * @param topLevelURI The URI of the SBOL Attachment object
  265. * @param path The path to store the downloaded attachment
  266. * @return the name of the file being downloaded
  267. *
  268. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  269. * @throws IOException if there is an I/O error
  270. */
  271. public String getFASTA(URI topLevelURI, String path) throws SynBioHubException, IOException
  272. {
  273. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  274. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  275. }
  276. String url = topLevelURI + "/fasta";
  277. url = url.replace(uriPrefix, backendUrl);
  278. return fetchContentSaveToFile(url,null,path);
  279. }
  280. /**
  281. * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
  282. * and save into the provided output stream.
  283. *
  284. * @param topLevelURI The URI of the SBOL Attachment object
  285. * @param outputStream The output stream to store the downloaded attachment
  286. * @return the name of the file being downloaded
  287. *
  288. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  289. * @throws IOException if there is an I/O error
  290. */
  291. public String getFASTA(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  292. {
  293. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  294. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  295. }
  296. String url = topLevelURI + "/fasta";
  297. url = url.replace(uriPrefix, backendUrl);
  298. return fetchContentSaveToFile(url,outputStream,null);
  299. }
  300. /**
  301. * Retrieve a JSON version of a topLevel object from a SynBioHub instance using its URI,
  302. * and save to the path provided.
  303. *
  304. * @param topLevelURI The URI of the SBOL Attachment object
  305. * @param path The path to store the downloaded attachment
  306. * @return the name of the file being downloaded
  307. *
  308. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  309. * @throws IOException if there is an I/O error
  310. */
  311. public String getJSON(URI topLevelURI, String path) throws SynBioHubException, IOException
  312. {
  313. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  314. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  315. }
  316. String url = topLevelURI + "/summary";
  317. url = url.replace(uriPrefix, backendUrl);
  318. return fetchContentSaveToFile(url,null,path);
  319. }
  320. /**
  321. * Retrieve a JSON version of a topLevel object from a SynBioHub instance using its URI,
  322. * and save into the provided output stream.
  323. *
  324. * @param topLevelURI The URI of the SBOL Attachment object
  325. * @param outputStream The output stream to store the downloaded attachment
  326. * @return the name of the file being downloaded
  327. *
  328. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  329. * @throws IOException if there is an I/O error
  330. */
  331. public String getJSON(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  332. {
  333. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  334. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  335. }
  336. String url = topLevelURI + "/summary";
  337. url = url.replace(uriPrefix, backendUrl);
  338. return fetchContentSaveToFile(url,outputStream,null);
  339. }
  340. /**
  341. * Retrieve metadata for a topLevel object from a SynBioHub instance using its URI,
  342. * and save to the path provided.
  343. *
  344. * @param topLevelURI The URI of the SBOL Attachment object
  345. * @param path The path to store the downloaded attachment
  346. * @return the name of the file being downloaded
  347. *
  348. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  349. * @throws IOException if there is an I/O error
  350. */
  351. public String getMetadata(URI topLevelURI, String path) throws SynBioHubException, IOException
  352. {
  353. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  354. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  355. }
  356. String url = topLevelURI + "/metadata";
  357. url = url.replace(uriPrefix, backendUrl);
  358. return fetchContentSaveToFile(url,null,path);
  359. }
  360. /**
  361. * Retrieve metadata for a topLevel object from a SynBioHub instance using its URI,
  362. * and save into the provided output stream.
  363. *
  364. * @param topLevelURI The URI of the SBOL Attachment object
  365. * @param outputStream The output stream to store the downloaded attachment
  366. * @return the name of the file being downloaded
  367. *
  368. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  369. * @throws IOException if there is an I/O error
  370. */
  371. public String getMetadata(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  372. {
  373. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  374. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  375. }
  376. String url = topLevelURI + "/metadata";
  377. url = url.replace(uriPrefix, backendUrl);
  378. return fetchContentSaveToFile(url,outputStream,null);
  379. }
  380. /**
  381. * Retrieve a COMBINE Archive version of a topLevel object from a SynBioHub instance using its URI,
  382. * and save to the path provided.
  383. *
  384. * @param topLevelURI The URI of the SBOL Attachment object
  385. * @param path The path to store the downloaded attachment
  386. * @return the name of the file being downloaded
  387. *
  388. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  389. * @throws IOException if there is an I/O error
  390. */
  391. public String getArchive(URI topLevelURI, String path) throws SynBioHubException, IOException
  392. {
  393. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  394. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  395. }
  396. String url = topLevelURI + "/omex";
  397. url = url.replace(uriPrefix, backendUrl);
  398. return fetchContentSaveToFile(url,null,path);
  399. }
  400. /**
  401. * Retrieve a COMBINE Archive version of a topLevel object from a SynBioHub instance using its URI,
  402. * and save into the provided output stream.
  403. *
  404. * @param topLevelURI The URI of the SBOL Attachment object
  405. * @param outputStream The output stream to store the downloaded attachment
  406. * @return the name of the file being downloaded
  407. *
  408. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  409. * @throws IOException if there is an I/O error
  410. */
  411. public String getArchive(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
  412. {
  413. if (!topLevelURI.toString().startsWith(uriPrefix)) {
  414. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  415. }
  416. String url = topLevelURI + "/omex";
  417. url = url.replace(uriPrefix, backendUrl);
  418. return fetchContentSaveToFile(url,outputStream,null);
  419. }
  420. /**
  421. * Retrieve an attachment from a SynBioHub instance using its URI,
  422. * and save to the path provided.
  423. *
  424. * @param attachmentUri The URI of the SBOL Attachment object
  425. * @param path The path to store the downloaded attachment
  426. * @return the name of the file being downloaded
  427. *
  428. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  429. * @throws IOException if there is an I/O error
  430. */
  431. public String getAttachment(URI attachmentUri, String path) throws SynBioHubException, IOException
  432. {
  433. if (!attachmentUri.toString().startsWith(uriPrefix)) {
  434. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  435. }
  436. String url = attachmentUri + "/download";
  437. url = url.replace(uriPrefix, backendUrl);
  438. return fetchContentSaveToFile(url,null,path);
  439. }
  440. /**
  441. * Retrieve an attachment from a SynBioHub instance using its URI,
  442. * and save into the provided output stream.
  443. *
  444. * @param attachmentUri The URI of the SBOL Attachment object
  445. * @param outputStream The output stream to store the downloaded attachment
  446. * @return the name of the file being downloaded
  447. *
  448. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  449. * @throws IOException if there is an I/O error
  450. */
  451. public String getAttachment(URI attachmentUri, OutputStream outputStream) throws SynBioHubException, IOException
  452. {
  453. if (!attachmentUri.toString().startsWith(uriPrefix)) {
  454. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  455. }
  456. String url = attachmentUri + "/download";
  457. url = url.replace(uriPrefix, backendUrl);
  458. return fetchContentSaveToFile(url,outputStream,null);
  459. }
  460. /**
  461. * Remove SBOL Collection from a SynBioHub instance using its URI.
  462. *
  463. * @param topLevelUri The URI of the SBOL Collection
  464. *
  465. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  466. */
  467. public void removeCollection(URI topLevelUri) throws SynBioHubException
  468. {
  469. if (!topLevelUri.toString().startsWith(uriPrefix)) {
  470. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  471. }
  472. String url = topLevelUri + "/removeCollection";
  473. url = url.replace(uriPrefix, backendUrl);
  474. removeFromSynBioHub(url);
  475. }
  476. /**
  477. * Remove SBOL TopLevel object from a SynBioHub instance using its URI.
  478. *
  479. * @param topLevelUri The URI of the SBOL TopLevel
  480. *
  481. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  482. */
  483. public void removeSBOL(URI topLevelUri) throws SynBioHubException
  484. {
  485. if (!topLevelUri.toString().startsWith(uriPrefix)) {
  486. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  487. }
  488. String url = topLevelUri + "/remove";
  489. url = url.replace(uriPrefix, backendUrl);
  490. removeFromSynBioHub(url);
  491. }
  492. /**
  493. * Remove SBOL TopLevel object from a SynBioHub instance using its URI,
  494. * but leave references to this object, since it is going to be replaced.
  495. *
  496. * @param topLevelUri The URI of the SBOL TopLevel
  497. *
  498. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  499. */
  500. public void replaceSBOL(URI topLevelUri) throws SynBioHubException
  501. {
  502. if (!topLevelUri.toString().startsWith(uriPrefix)) {
  503. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  504. }
  505. String url = topLevelUri + "/replace";
  506. url = url.replace(uriPrefix, backendUrl);
  507. removeFromSynBioHub(url);
  508. }
  509. /**
  510. * Search the default store for ComponentDefinition instances matching a name and/or a set of roles
  511. *
  512. * @param name The dcterms:title to search for, or null
  513. * @param roles A set of role URIs to search for, or null
  514. * @param types A set of type URIs to search for, or null
  515. * @param collections A set of Collection URIs to search for, or null
  516. * @param offset The offset of the results to begin at, or null to begin at 0
  517. * @param limit The maximum number of results to return, or null to return all results
  518. *
  519. * @return An ArrayList of ComponentDefinitionMetaData objects with a summary of all matching ComponentDefinitions.
  520. *
  521. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  522. */
  523. public ArrayList<IdentifiedMetadata> getMatchingComponentDefinitionMetadata(String name, Set<URI> roles,
  524. Set<URI> types, Set<URI> collections, Integer offset, Integer limit)
  525. throws SynBioHubException
  526. {
  527. SearchQuery query = new SearchQuery();
  528. query.setOffset(offset);
  529. query.setLimit(limit);
  530. SearchCriteria objectCriteria = new SearchCriteria();
  531. objectCriteria.setKey("objectType");
  532. objectCriteria.setValue("ComponentDefinition");
  533. query.addCriteria(objectCriteria);
  534. if (roles != null) {
  535. for(URI uri : roles)
  536. {
  537. SearchCriteria roleCriteria = new SearchCriteria();
  538. roleCriteria.setKey("role");
  539. roleCriteria.setValue(uri.toString());
  540. query.getCriteria().add(roleCriteria);
  541. }
  542. }
  543. if (types != null) {
  544. for(URI uri : types)
  545. {
  546. SearchCriteria typeCriteria = new SearchCriteria();
  547. typeCriteria.setKey("type");
  548. typeCriteria.setValue(uri.toString());
  549. query.getCriteria().add(typeCriteria);
  550. }
  551. }
  552. if (collections != null) {
  553. for(URI uri : collections)
  554. {
  555. SearchCriteria collectionCriteria = new SearchCriteria();
  556. collectionCriteria.setKey("collection");
  557. collectionCriteria.setValue(uri.toString());
  558. query.getCriteria().add(collectionCriteria);
  559. }
  560. }
  561. if(name != null)
  562. {
  563. SearchCriteria nameCriteria = new SearchCriteria();
  564. nameCriteria.setKey("name");
  565. nameCriteria.setValue(name);
  566. query.getCriteria().add(nameCriteria);
  567. }
  568. return search(query);
  569. }
  570. private String constructQueryURL(String url,SearchQuery query) {
  571. //query.offset = offset;
  572. //query.limit = limit;
  573. String textQuery = "";
  574. boolean first = true;
  575. for (SearchCriteria criteria : query.getCriteria()) {
  576. if (criteria.getKey().equals("objectType")) {
  577. url += encodeUri(criteria.getKey()+"="+criteria.getValue()+"&");
  578. continue;
  579. }
  580. if (criteria.getKey().equals("name")) {
  581. if (first) first = false;
  582. else textQuery = " ";
  583. textQuery = criteria.getValue();
  584. continue;
  585. }
  586. if (criteria.getKey().startsWith("http")) {
  587. url += encodeUri("<" + criteria.getKey() + ">=");
  588. } else {
  589. url += encodeUri(criteria.getKey()+"=");
  590. }
  591. if (criteria.getValue().startsWith("http")) {
  592. url += encodeUri("<"+criteria.getValue()+">&");
  593. } else {
  594. url += encodeUri("'"+criteria.getValue()+"'&");
  595. }
  596. }
  597. url += encodeUri(textQuery);
  598. if (query.getOffset()!=null && query.getLimit()!=null) {
  599. url += "/?offset="+query.getOffset() + "&" + "limit="+query.getLimit();
  600. } else if (query.getOffset()!=null) {
  601. url += "/?offset="+query.getOffset();
  602. } else if (query.getLimit()!=null) {
  603. url += "/?limit="+query.getLimit();
  604. }
  605. return url;
  606. }
  607. /**
  608. * Search this SynBioHub instance for objects matching a search query
  609. *
  610. * @param query the search query
  611. *
  612. * @return An ArrayList of MetaData for objects that match the specified search query
  613. *
  614. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  615. */
  616. public ArrayList<IdentifiedMetadata> search(SearchQuery query) throws SynBioHubException
  617. {
  618. String url = backendUrl + "/search/";
  619. //query.offset = offset;
  620. //query.limit = limit;
  621. url = constructQueryURL(url,query);
  622. //System.out.println(url);
  623. Gson gson = new Gson();
  624. HttpGet request = new HttpGet(url);
  625. request.setHeader("X-authorization", user);
  626. request.setHeader("Accept", "text/plain");
  627. try
  628. {
  629. HttpResponse response = client.execute(request);
  630. checkResponseCode(response);
  631. InputStream inputStream = response.getEntity().getContent();
  632. ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
  633. new InputStreamReader(inputStream),
  634. new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
  635. return metadataList;
  636. }
  637. catch (Exception e)
  638. {
  639. throw new SynBioHubException(e);
  640. }
  641. finally
  642. {
  643. request.releaseConnection();
  644. }
  645. }
  646. /**
  647. * Search this SynBioHub instance for objects matching a search query
  648. * and return the number of matches
  649. *
  650. * @param query the search query
  651. *
  652. * @return the number of objects matching a search query
  653. *
  654. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  655. */
  656. public int searchCount(SearchQuery query) throws SynBioHubException
  657. {
  658. String url = backendUrl + "/searchCount/";
  659. //query.offset = offset;
  660. //query.limit = limit;
  661. url = constructQueryURL(url,query);
  662. return fetchCount(url);
  663. }
  664. /**
  665. * Search the default store for Collections that are not members of any other Collections
  666. *
  667. * @return An ArrayList of CollectionMetaData objects with a summary of all matching Collections.
  668. *
  669. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  670. */
  671. public ArrayList<IdentifiedMetadata> getRootCollectionMetadata()
  672. throws SynBioHubException
  673. {
  674. String url = backendUrl + "/rootCollections";
  675. Gson gson = new Gson();
  676. HttpGet request = new HttpGet(url);
  677. request.setHeader("X-authorization", user);
  678. request.setHeader("Accept", "text/plain");
  679. try
  680. {
  681. HttpResponse response = client.execute(request);
  682. checkResponseCode(response);
  683. InputStream inputStream = response.getEntity().getContent();
  684. ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
  685. new InputStreamReader(inputStream),
  686. new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
  687. return metadataList;
  688. }
  689. catch (Exception e)
  690. {
  691. throw new SynBioHubException(e);
  692. }
  693. finally
  694. {
  695. request.releaseConnection();
  696. }
  697. }
  698. /**
  699. * Search the default store for submissions from the specified user
  700. *
  701. * @return An ArrayList of MetaData for all submissions for the specified user.
  702. *
  703. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  704. */
  705. public ArrayList<IdentifiedMetadata> getSubmissionsMetadata()
  706. throws SynBioHubException
  707. {
  708. String url = backendUrl + "/manage";
  709. Gson gson = new Gson();
  710. HttpGet request = new HttpGet(url);
  711. request.setHeader("X-authorization", user);
  712. request.setHeader("Accept", "text/plain");
  713. try
  714. {
  715. HttpResponse response = client.execute(request);
  716. checkResponseCode(response);
  717. InputStream inputStream = response.getEntity().getContent();
  718. ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
  719. new InputStreamReader(inputStream),
  720. new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
  721. return metadataList;
  722. }
  723. catch (Exception e)
  724. {
  725. throw new SynBioHubException(e);
  726. }
  727. finally
  728. {
  729. request.releaseConnection();
  730. }
  731. }
  732. /**
  733. * Search the default store for objects that are shared with the specified user
  734. *
  735. * @return An ArrayList of MetaData for all shared objects.
  736. *
  737. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  738. */
  739. public ArrayList<IdentifiedMetadata> getSharedMetadata()
  740. throws SynBioHubException
  741. {
  742. String url = backendUrl + "/shared";
  743. Gson gson = new Gson();
  744. HttpGet request = new HttpGet(url);
  745. request.setHeader("X-authorization", user);
  746. request.setHeader("Accept", "text/plain");
  747. try
  748. {
  749. HttpResponse response = client.execute(request);
  750. checkResponseCode(response);
  751. InputStream inputStream = response.getEntity().getContent();
  752. ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
  753. new InputStreamReader(inputStream),
  754. new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
  755. return metadataList;
  756. }
  757. catch (Exception e)
  758. {
  759. throw new SynBioHubException(e);
  760. }
  761. finally
  762. {
  763. request.releaseConnection();
  764. }
  765. }
  766. /**
  767. * Get configuration of a SynBioHub
  768. *
  769. * @return A JSON object of the configuration of a SynBioHub
  770. *
  771. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  772. */
  773. public JSONObject getConfig() throws SynBioHubException
  774. {
  775. return getConfig("");
  776. }
  777. /**
  778. * Get graphs in a SynBioHub
  779. *
  780. * @return A JSON array of the graphs in a SynBioHub
  781. *
  782. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  783. */
  784. public JSONArray getGraphs() throws SynBioHubException
  785. {
  786. if (user.equals("")) {
  787. Exception e = new Exception("Must be logged in to get the graphs.");
  788. throw new SynBioHubException(e);
  789. }
  790. String url = backendUrl + "/admin/graphs";
  791. HttpGet request = new HttpGet(url);
  792. request.setHeader("X-authorization", user);
  793. request.setHeader("Accept", "text/plain");
  794. try
  795. {
  796. HttpResponse response = client.execute(request);
  797. checkResponseCode(response);
  798. InputStream inputStream = response.getEntity().getContent();
  799. JSONParser parser = new JSONParser();
  800. JSONArray config = (JSONArray) parser.parse(inputStreamToString(inputStream));
  801. return config;
  802. }
  803. catch (Exception e)
  804. {
  805. throw new SynBioHubException(e);
  806. }
  807. finally
  808. {
  809. request.releaseConnection();
  810. }
  811. }
  812. /**
  813. * Get graphs in a SynBioHub
  814. *
  815. * @return A JSONObject of the graphs in a SynBioHub
  816. *
  817. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  818. */
  819. public JSONArray getLogs() throws SynBioHubException
  820. {
  821. if (user.equals("")) {
  822. Exception e = new Exception("Must be logged in to get the logs.");
  823. throw new SynBioHubException(e);
  824. }
  825. String url = backendUrl + "/admin/log";
  826. HttpGet request = new HttpGet(url);
  827. request.setHeader("X-authorization", user);
  828. request.setHeader("Accept", "text/plain");
  829. try
  830. {
  831. HttpResponse response = client.execute(request);
  832. checkResponseCode(response);
  833. InputStream inputStream = response.getEntity().getContent();
  834. JSONParser parser = new JSONParser();
  835. JSONArray config = (JSONArray) parser.parse(inputStreamToString(inputStream));
  836. return config;
  837. }
  838. catch (Exception e)
  839. {
  840. throw new SynBioHubException(e);
  841. }
  842. finally
  843. {
  844. request.releaseConnection();
  845. }
  846. }
  847. /**
  848. * Get mail configuration of a SynBioHub
  849. *
  850. * @return A JSON object of the mail configuration of a SynBioHub
  851. *
  852. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  853. */
  854. public JSONObject getMailConfig() throws SynBioHubException
  855. {
  856. return getConfig("mail");
  857. }
  858. /**
  859. * Set the mail configuration for a SynBioHub
  860. *
  861. * @param key SendGrid API Key
  862. * @param fromEmail SendGrid from Email
  863. *
  864. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  865. */
  866. public void setMailConfig(String key, String fromEmail) throws SynBioHubException
  867. {
  868. if (user.equals("")) {
  869. Exception e = new Exception("Must be logged in to set the mail configuration.");
  870. throw new SynBioHubException(e);
  871. }
  872. String url = backendUrl + "/admin/mail";
  873. HttpPost request = new HttpPost(url);
  874. request.setHeader("X-authorization", user);
  875. request.setHeader("Accept", "text/plain");
  876. List<NameValuePair> arguments = new ArrayList<>(4);
  877. arguments.add(new BasicNameValuePair("key", key));
  878. arguments.add(new BasicNameValuePair("fromEmail", fromEmail));
  879. try
  880. {
  881. request.setEntity(new UrlEncodedFormEntity(arguments));
  882. HttpResponse response = client.execute(request);
  883. checkResponseCode(response);
  884. }
  885. catch (Exception e)
  886. {
  887. //e.printStackTrace();
  888. throw new SynBioHubException(e);
  889. }
  890. finally
  891. {
  892. request.releaseConnection();
  893. }
  894. }
  895. /**
  896. * Get plugin configuration for a SynBioHub
  897. *
  898. * @return A JSON object of the plugin configuration for a SynBioHub
  899. *
  900. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  901. */
  902. public JSONObject getPluginsConfig() throws SynBioHubException
  903. {
  904. return getConfig("plugins");
  905. }
  906. /**
  907. * Edit a plugin configuration in a SynBioHub
  908. *
  909. * @param id Id of plugin
  910. * @param category Type of plugin (rendering, submit, download)
  911. * @param name Name of the plugin
  912. * @param pluginURL URL for the plugin
  913. *
  914. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  915. */
  916. public void editPluginConfig(int id, String category, String name, String pluginURL) throws SynBioHubException
  917. {
  918. id = id + 1;
  919. savePluginConfig(Integer.toString(id), category, name, pluginURL);
  920. }
  921. /**
  922. * Add a plugin configuration to a SynBioHub
  923. *
  924. * @param category Type of plugin (rendering, submit, download)
  925. * @param name Name of the plugin
  926. * @param pluginURL URL for the plugin
  927. *
  928. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  929. */
  930. public void addPluginConfig(String category, String name, String pluginURL) throws SynBioHubException
  931. {
  932. savePluginConfig("New", category, name, pluginURL);
  933. }
  934. /**
  935. * Save a plugin configuration to a SynBioHub
  936. *
  937. * @param id Id of plugin, if adding a plugin, id should be New
  938. * @param category Type of plugin (rendering, submit, download)
  939. * @param name Name of the plugin
  940. * @param pluginURL URL for the plugin
  941. *
  942. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  943. */
  944. private void savePluginConfig(String id, String category, String name, String pluginURL) throws SynBioHubException
  945. {
  946. if (user.equals("")) {
  947. Exception e = new Exception("Must be logged in to add a plugin configuration.");
  948. throw new SynBioHubException(e);
  949. }
  950. String url = backendUrl + "/admin/savePlugin";
  951. HttpPost request = new HttpPost(url);
  952. request.setHeader("X-authorization", user);
  953. request.setHeader("Accept", "text/plain");
  954. List<NameValuePair> arguments = new ArrayList<>(4);
  955. arguments.add(new BasicNameValuePair("id", id));
  956. arguments.add(new BasicNameValuePair("category", category));
  957. arguments.add(new BasicNameValuePair("name", name));
  958. arguments.add(new BasicNameValuePair("url", pluginURL));
  959. try
  960. {
  961. request.setEntity(new UrlEncodedFormEntity(arguments));
  962. HttpResponse response = client.execute(request);
  963. checkResponseCode(response);
  964. }
  965. catch (Exception e)
  966. {
  967. //e.printStackTrace();
  968. throw new SynBioHubException(e);
  969. }
  970. finally
  971. {
  972. request.releaseConnection();
  973. }
  974. }
  975. /**
  976. * Delete a plugin configuration to a SynBioHub
  977. *
  978. * @param id Id of plugin
  979. * @param category Type of plugin (rendering, submit, download)
  980. *
  981. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  982. */
  983. public void deletePluginConfig(int id, String category) throws SynBioHubException
  984. {
  985. if (user.equals("")) {
  986. Exception e = new Exception("Must be logged in to delete a plugin configuration.");
  987. throw new SynBioHubException(e);
  988. }
  989. String url = backendUrl + "/admin/deletePlugin";
  990. HttpPost request = new HttpPost(url);
  991. request.setHeader("X-authorization", user);
  992. request.setHeader("Accept", "text/plain");
  993. id = id + 1;
  994. List<NameValuePair> arguments = new ArrayList<>(4);
  995. arguments.add(new BasicNameValuePair("id", Integer.toString(id)));
  996. arguments.add(new BasicNameValuePair("category", category));
  997. try
  998. {
  999. request.setEntity(new UrlEncodedFormEntity(arguments));
  1000. HttpResponse response = client.execute(request);
  1001. checkResponseCode(response);
  1002. }
  1003. catch (Exception e)
  1004. {
  1005. //e.printStackTrace();
  1006. throw new SynBioHubException(e);
  1007. }
  1008. finally
  1009. {
  1010. request.releaseConnection();
  1011. }
  1012. }
  1013. /**
  1014. * Get registries configuration for a SynBioHub
  1015. *
  1016. * @return A JSON object of the registries configuration for a SynBioHub
  1017. *
  1018. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1019. */
  1020. public JSONObject getRegistriesConfig() throws SynBioHubException
  1021. {
  1022. return getConfig("registries");
  1023. }
  1024. /**
  1025. * Save a registry configuration for a SynBioHub
  1026. *
  1027. * @param uriPrefix URI prefix for the registry
  1028. * @param registryURL URL for the registry
  1029. *
  1030. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1031. */
  1032. public void saveRegistryConfig(String uriPrefix, String registryURL) throws SynBioHubException
  1033. {
  1034. if (user.equals("")) {
  1035. Exception e = new Exception("Must be logged in to save a registry configuration.");
  1036. throw new SynBioHubException(e);
  1037. }
  1038. String url = backendUrl + "/admin/saveRegistry";
  1039. HttpPost request = new HttpPost(url);
  1040. request.setHeader("X-authorization", user);
  1041. request.setHeader("Accept", "text/plain");
  1042. List<NameValuePair> arguments = new ArrayList<>(4);
  1043. arguments.add(new BasicNameValuePair("uri", uriPrefix));
  1044. arguments.add(new BasicNameValuePair("url", registryURL));
  1045. try
  1046. {
  1047. request.setEntity(new UrlEncodedFormEntity(arguments));
  1048. HttpResponse response = client.execute(request);
  1049. checkResponseCode(response);
  1050. }
  1051. catch (Exception e)
  1052. {
  1053. //e.printStackTrace();
  1054. throw new SynBioHubException(e);
  1055. }
  1056. finally
  1057. {
  1058. request.releaseConnection();
  1059. }
  1060. }
  1061. /**
  1062. * Delete a registry configuration in a SynBioHub
  1063. *
  1064. * @param uriPrefix URI prefix of the registry to delete
  1065. *
  1066. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1067. */
  1068. public void deleteRegistryConfig(String uriPrefix) throws SynBioHubException
  1069. {
  1070. if (user.equals("")) {
  1071. Exception e = new Exception("Must be logged in to remove a registry configuration.");
  1072. throw new SynBioHubException(e);
  1073. }
  1074. String url = backendUrl + "/admin/deleteRegistry";
  1075. HttpPost request = new HttpPost(url);
  1076. request.setHeader("X-authorization", user);
  1077. request.setHeader("Accept", "text/plain");
  1078. List<NameValuePair> arguments = new ArrayList<>(4);
  1079. arguments.add(new BasicNameValuePair("uri", uriPrefix));
  1080. try
  1081. {
  1082. request.setEntity(new UrlEncodedFormEntity(arguments));
  1083. HttpResponse response = client.execute(request);
  1084. checkResponseCode(response);
  1085. }
  1086. catch (Exception e)
  1087. {
  1088. //e.printStackTrace();
  1089. throw new SynBioHubException(e);
  1090. }
  1091. finally
  1092. {
  1093. request.releaseConnection();
  1094. }
  1095. }
  1096. /**
  1097. * Update the administrator email configuration for a SynBioHub
  1098. *
  1099. * @param administratorEmail Administrator email address
  1100. *
  1101. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1102. */
  1103. public void updateAdministratorEmailConfig(String administratorEmail) throws SynBioHubException
  1104. {
  1105. if (user.equals("")) {
  1106. Exception e = new Exception("Must be logged in to update the administrator email configuration.");
  1107. throw new SynBioHubException(e);
  1108. }
  1109. String url = backendUrl + "/admin/setAdministratorEmail";
  1110. HttpPost request = new HttpPost(url);
  1111. request.setHeader("X-authorization", user);
  1112. request.setHeader("Accept", "text/plain");
  1113. List<NameValuePair> arguments = new ArrayList<>(4);
  1114. arguments.add(new BasicNameValuePair("administratorEmail", administratorEmail));
  1115. try
  1116. {
  1117. request.setEntity(new UrlEncodedFormEntity(arguments));
  1118. HttpResponse response = client.execute(request);
  1119. checkResponseCode(response);
  1120. }
  1121. catch (Exception e)
  1122. {
  1123. //e.printStackTrace();
  1124. throw new SynBioHubException(e);
  1125. }
  1126. finally
  1127. {
  1128. request.releaseConnection();
  1129. }
  1130. }
  1131. /**
  1132. * Retrieve update from Web-of-Registries for a SynBioHub
  1133. *
  1134. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1135. */
  1136. public void retreiveUpdateFromWebOfRegistries() throws SynBioHubException
  1137. {
  1138. if (user.equals("")) {
  1139. Exception e = new Exception("Must be logged in to retrieve update from Web-of-Registries.");
  1140. throw new SynBioHubException(e);
  1141. }
  1142. String url = backendUrl + "/admin/retrieveFromWebOfRegistries";
  1143. HttpPost request = new HttpPost(url);
  1144. request.setHeader("X-authorization", user);
  1145. request.setHeader("Accept", "text/plain");
  1146. try
  1147. {
  1148. HttpResponse response = client.execute(request);
  1149. checkResponseCode(response);
  1150. }
  1151. catch (Exception e)
  1152. {
  1153. //e.printStackTrace();
  1154. throw new SynBioHubException(e);
  1155. }
  1156. finally
  1157. {
  1158. request.releaseConnection();
  1159. }
  1160. }
  1161. /**
  1162. * Send request to join Web-of-Registries for a SynBioHub
  1163. *
  1164. * @param administratorEmail Administrator email address
  1165. * @param webOfRegistries URL for the Web-of-Registries
  1166. *
  1167. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1168. */
  1169. public void requestToJoinWebOfRegistries(String administratorEmail, String webOfRegistries) throws SynBioHubException
  1170. {
  1171. if (user.equals("")) {
  1172. Exception e = new Exception("Must be logged in to send request to join Web-of-Registries.");
  1173. throw new SynBioHubException(e);
  1174. }
  1175. String url = backendUrl + "/admin/federate";
  1176. HttpPost request = new HttpPost(url);
  1177. request.setHeader("X-authorization", user);
  1178. request.setHeader("Accept", "text/plain");
  1179. List<NameValuePair> arguments = new ArrayList<>(4);
  1180. arguments.add(new BasicNameValuePair("administratorEmail", administratorEmail));
  1181. arguments.add(new BasicNameValuePair("webOfRegistries", webOfRegistries));
  1182. try
  1183. {
  1184. request.setEntity(new UrlEncodedFormEntity(arguments));
  1185. HttpResponse response = client.execute(request);
  1186. checkResponseCode(response);
  1187. }
  1188. catch (Exception e)
  1189. {
  1190. //e.printStackTrace();
  1191. throw new SynBioHubException(e);
  1192. }
  1193. finally
  1194. {
  1195. request.releaseConnection();
  1196. }
  1197. }
  1198. /**
  1199. * Get remotes configuration for a SynBioHub
  1200. *
  1201. * @return A JSON object of the remotes configuration for a SynBioHub
  1202. *
  1203. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1204. */
  1205. public JSONObject getRemotesConfig() throws SynBioHubException
  1206. {
  1207. return getConfig("remotes");
  1208. }
  1209. /**
  1210. * Save a Benchling remote configuration for a SynBioHub
  1211. *
  1212. * @param id Id of the Benchling remote
  1213. * @param remoteURL URL for the Benchling remote
  1214. * @param benchlingApiToken API token for the Benchling remote
  1215. * @param rejectUnauthorized Check SSL certificate?
  1216. * @param folderPrefix Prefix to use for folders on Benchling
  1217. * @param sequenceSuffix Suffix to use for sequences found on Benchling
  1218. * @param defaultFolderId Default folder on Benchling to access
  1219. * @param isPublic Should the remote be visible publicly?
  1220. * @param rootCollectionDisplayId Display id for the root collection on the remote
  1221. * @param rootCollectionName Name for the root collection on the remote
  1222. * @param rootCollectionDescription Description for the root collection on the remote
  1223. *
  1224. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1225. */
  1226. public void saveBenchlingRemoteConfig(String id, String remoteURL, String benchlingApiToken,
  1227. boolean rejectUnauthorized, String folderPrefix, String sequenceSuffix, String defaultFolderId,
  1228. boolean isPublic, String rootCollectionDisplayId, String rootCollectionName,
  1229. String rootCollectionDescription) throws SynBioHubException
  1230. {
  1231. if (user.equals("")) {
  1232. Exception e = new Exception("Must be logged in to save a Benchling remote configuration.");
  1233. throw new SynBioHubException(e);
  1234. }
  1235. String url = backendUrl + "/admin/saveRemote";
  1236. HttpPost request = new HttpPost(url);
  1237. request.setHeader("X-authorization", user);
  1238. request.setHeader("Accept", "text/plain");
  1239. List<NameValuePair> arguments = new ArrayList<>(4);
  1240. arguments.add(new BasicNameValuePair("type", "benchling"));
  1241. arguments.add(new BasicNameValuePair("id", id));
  1242. arguments.add(new BasicNameValuePair("url", remoteURL));
  1243. arguments.add(new BasicNameValuePair("benchlingApiToken", benchlingApiToken));
  1244. if (rejectUnauthorized) {
  1245. arguments.add(new BasicNameValuePair("rejectUnauthorized", "on"));
  1246. }
  1247. arguments.add(new BasicNameValuePair("folderPrefix", folderPrefix));
  1248. arguments.add(new BasicNameValuePair("sequenceSuffix", sequenceSuffix));
  1249. arguments.add(new BasicNameValuePair("defaultFolderId", defaultFolderId));
  1250. if (isPublic) {
  1251. arguments.add(new BasicNameValuePair("isPublic", "on"));
  1252. }
  1253. arguments.add(new BasicNameValuePair("rootCollectionDisplayId", rootCollectionDisplayId));
  1254. arguments.add(new BasicNameValuePair("rootCollectionName", rootCollectionName));
  1255. arguments.add(new BasicNameValuePair("rootCollectionDescription", rootCollectionDescription));
  1256. try
  1257. {
  1258. request.setEntity(new UrlEncodedFormEntity(arguments));
  1259. HttpResponse response = client.execute(request);
  1260. checkResponseCode(response);
  1261. }
  1262. catch (Exception e)
  1263. {
  1264. //e.printStackTrace();
  1265. throw new SynBioHubException(e);
  1266. }
  1267. finally
  1268. {
  1269. request.releaseConnection();
  1270. }
  1271. }
  1272. /**
  1273. * Save a ICE remote configuration for a SynBioHub
  1274. *
  1275. * @param id Id of the ICE remote
  1276. * @param remoteURL URL for the ICE remote
  1277. * @param iceApiTokenClient ICE API token client
  1278. * @param iceApiToken ICE API token
  1279. * @param iceApiTokenOwner ICE API token owner
  1280. * @param iceCollection ICE collection
  1281. * @param rejectUnauthorized Check SSL certificate?
  1282. * @param folderPrefix Prefix to use for folders on ICE
  1283. * @param sequenceSuffix Suffix to use for sequences found on Benchling
  1284. * @param defaultFolderId Default folder on Benchling to access
  1285. * @param groupId Group id on ICE
  1286. * @param pi Principal Investigator name
  1287. * @param piEmail Principal Investigator email
  1288. * @param isPublic Should the remote be visible publicly?
  1289. * @param partNumberPrefix Prefix to use for parts
  1290. * @param rootCollectionDisplayId Display id for the root collection on the remote
  1291. * @param rootCollectionName Name for the root collection on the remote
  1292. * @param rootCollectionDescription Description for the root collection on the remote
  1293. *
  1294. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1295. */
  1296. public void saveICERemoteConfig(String id, String remoteURL, String iceApiTokenClient, String iceApiToken,
  1297. String iceApiTokenOwner, String iceCollection, boolean rejectUnauthorized, String folderPrefix,
  1298. String sequenceSuffix, String defaultFolderId, String groupId, String pi, String piEmail,
  1299. boolean isPublic, String partNumberPrefix, String rootCollectionDisplayId, String rootCollectionName,
  1300. String rootCollectionDescription) throws SynBioHubException
  1301. {
  1302. if (user.equals("")) {
  1303. Exception e = new Exception("Must be logged in to save a ICE remote configuration.");
  1304. throw new SynBioHubException(e);
  1305. }
  1306. String url = backendUrl + "/admin/saveRemote";
  1307. HttpPost request = new HttpPost(url);
  1308. request.setHeader("X-authorization", user);
  1309. request.setHeader("Accept", "text/plain");
  1310. List<NameValuePair> arguments = new ArrayList<>(4);
  1311. arguments.add(new BasicNameValuePair("type", "ice"));
  1312. arguments.add(new BasicNameValuePair("id", id));
  1313. arguments.add(new BasicNameValuePair("url", remoteURL));
  1314. arguments.add(new BasicNameValuePair("iceApiTokenClient", iceApiTokenClient));
  1315. arguments.add(new BasicNameValuePair("iceApiToken", iceApiToken));
  1316. arguments.add(new BasicNameValuePair("iceApiTokenOwner", iceApiTokenOwner));
  1317. arguments.add(new BasicNameValuePair("iceCollection", iceCollection));
  1318. if (rejectUnauthorized) {
  1319. arguments.add(new BasicNameValuePair("rejectUnauthorized", "on"));
  1320. }
  1321. arguments.add(new BasicNameValuePair("folderPrefix", folderPrefix));
  1322. arguments.add(new BasicNameValuePair("sequenceSuffix", sequenceSuffix));
  1323. arguments.add(new BasicNameValuePair("defaultFolderId", defaultFolderId));
  1324. arguments.add(new BasicNameValuePair("groupId", groupId));
  1325. arguments.add(new BasicNameValuePair("pi", pi));
  1326. arguments.add(new BasicNameValuePair("piEmail", piEmail));
  1327. if (isPublic) {
  1328. arguments.add(new BasicNameValuePair("isPublic", "on"));
  1329. }
  1330. arguments.add(new BasicNameValuePair("partNumberPrefix", partNumberPrefix));
  1331. arguments.add(new BasicNameValuePair("rootCollectionDisplayId", rootCollectionDisplayId));
  1332. arguments.add(new BasicNameValuePair("rootCollectionName", rootCollectionName));
  1333. arguments.add(new BasicNameValuePair("rootCollectionDescription", rootCollectionDescription));
  1334. try
  1335. {
  1336. request.setEntity(new UrlEncodedFormEntity(arguments));
  1337. HttpResponse response = client.execute(request);
  1338. checkResponseCode(response);
  1339. }
  1340. catch (Exception e)
  1341. {
  1342. //e.printStackTrace();
  1343. throw new SynBioHubException(e);
  1344. }
  1345. finally
  1346. {
  1347. request.releaseConnection();
  1348. }
  1349. }
  1350. /**
  1351. * Delete a remote configuration in a SynBioHub
  1352. *
  1353. * @param id Id of the remote configuration to remove
  1354. *
  1355. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1356. */
  1357. public void deleteRemoteConfig(String id) throws SynBioHubException
  1358. {
  1359. if (user.equals("")) {
  1360. Exception e = new Exception("Must be logged in to remove a remote configuration.");
  1361. throw new SynBioHubException(e);
  1362. }
  1363. String url = backendUrl + "/admin/deleteRemote";
  1364. HttpPost request = new HttpPost(url);
  1365. request.setHeader("X-authorization", user);
  1366. request.setHeader("Accept", "text/plain");
  1367. List<NameValuePair> arguments = new ArrayList<>(4);
  1368. arguments.add(new BasicNameValuePair("id", id));
  1369. try
  1370. {
  1371. request.setEntity(new UrlEncodedFormEntity(arguments));
  1372. HttpResponse response = client.execute(request);
  1373. checkResponseCode(response);
  1374. }
  1375. catch (Exception e)
  1376. {
  1377. //e.printStackTrace();
  1378. throw new SynBioHubException(e);
  1379. }
  1380. finally
  1381. {
  1382. request.releaseConnection();
  1383. }
  1384. }
  1385. /**
  1386. * Get explorer configuration for a SynBioHub
  1387. *
  1388. * @return A JSON object of the explorer configuration for a SynBioHub
  1389. *
  1390. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1391. */
  1392. public JSONObject getExplorerConfig() throws SynBioHubException
  1393. {
  1394. return getConfig("explorer");
  1395. }
  1396. /**
  1397. * Set the explorer configuration for a SynBioHub
  1398. *
  1399. * @param useSBOLExplorer Boolean indicating whether SBOLExplorer is to be used or not
  1400. * @param SBOLExplorerEndpoint The endpoint where SBOLExplorer can be found
  1401. * @param useDistributedSearch Boolean indicating whether distributed search should be used
  1402. * @param pagerankTolerance The Pagerank tolerance factor
  1403. * @param uclustIdentity The UClust clustering identity
  1404. * @param synbiohubPublicGraph The SynBioHub public graph for this instance
  1405. * @param elasticsearchEndpoint The endpoint where Elasticsearch can be found
  1406. * @param elasticsearchIndexName The Elasticsearch index name
  1407. * @param sparqlEndpoint The Virtuoso SPARQL endpoint
  1408. *
  1409. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1410. */
  1411. public void setExplorerConfig(boolean useSBOLExplorer, String SBOLExplorerEndpoint, boolean useDistributedSearch,
  1412. String pagerankTolerance, String uclustIdentity, String synbiohubPublicGraph, String elasticsearchEndpoint,
  1413. String elasticsearchIndexName, String sparqlEndpoint) throws SynBioHubException
  1414. {
  1415. if (user.equals("")) {
  1416. Exception e = new Exception("Must be logged in to set the explorer configuration.");
  1417. throw new SynBioHubException(e);
  1418. }
  1419. String url = backendUrl + "/admin/explorer";
  1420. HttpPost request = new HttpPost(url);
  1421. request.setHeader("X-authorization", user);
  1422. request.setHeader("Accept", "text/plain");
  1423. List<NameValuePair> arguments = new ArrayList<>(4);
  1424. if (useSBOLExplorer) {
  1425. arguments.add(new BasicNameValuePair("useSBOLExplorer", "true"));
  1426. }
  1427. arguments.add(new BasicNameValuePair("SBOLExplorerEndpoint", SBOLExplorerEndpoint));
  1428. if (useDistributedSearch) {
  1429. arguments.add(new BasicNameValuePair("useDistributedSearch", "true"));
  1430. }
  1431. arguments.add(new BasicNameValuePair("pagerankTolerance", pagerankTolerance));
  1432. arguments.add(new BasicNameValuePair("uclustIdentity", uclustIdentity));
  1433. arguments.add(new BasicNameValuePair("synbiohubPublicGraph", synbiohubPublicGraph));
  1434. arguments.add(new BasicNameValuePair("elasticsearchEndpoint", elasticsearchEndpoint));
  1435. arguments.add(new BasicNameValuePair("elasticsearchIndexName", elasticsearchIndexName));
  1436. arguments.add(new BasicNameValuePair("sparqlEndpoint", sparqlEndpoint));
  1437. try
  1438. {
  1439. request.setEntity(new UrlEncodedFormEntity(arguments));
  1440. HttpResponse response = client.execute(request);
  1441. checkResponseCode(response);
  1442. }
  1443. catch (Exception e)
  1444. {
  1445. //e.printStackTrace();
  1446. throw new SynBioHubException(e);
  1447. }
  1448. finally
  1449. {
  1450. request.releaseConnection();
  1451. }
  1452. }
  1453. /**
  1454. * Update SBOLExplorer index
  1455. *
  1456. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1457. */
  1458. public void updateExplorerIndex() throws SynBioHubException
  1459. {
  1460. if (user.equals("")) {
  1461. Exception e = new Exception("Must be logged in to update the SBOLExplorer index.");
  1462. throw new SynBioHubException(e);
  1463. }
  1464. String url = backendUrl + "/admin/explorerUpdateIndex";
  1465. HttpPost request = new HttpPost(url);
  1466. request.setHeader("X-authorization", user);
  1467. request.setHeader("Accept", "text/plain");
  1468. try
  1469. {
  1470. HttpResponse response = client.execute(request);
  1471. checkResponseCode(response);
  1472. }
  1473. catch (Exception e)
  1474. {
  1475. //e.printStackTrace();
  1476. throw new SynBioHubException(e);
  1477. }
  1478. finally
  1479. {
  1480. request.releaseConnection();
  1481. }
  1482. }
  1483. /**
  1484. * Get the SBOLExplorer log file
  1485. *
  1486. * @param path Path where to store the SBOLExplorer log file
  1487. *
  1488. * @return the filename of the SBOLExplorer log file
  1489. *
  1490. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1491. * @throws IOException if there is an I/O error
  1492. */
  1493. public String getExplorerLog(String path) throws SynBioHubException, IOException
  1494. {
  1495. String url = backendUrl + "/admin/explorerLog";
  1496. return fetchContentSaveToFile(url,null,path);
  1497. }
  1498. /**
  1499. * Get the SBOLExplorer log file
  1500. *
  1501. * @param outputStream OutputStream to return the SBOLExplorer log
  1502. *
  1503. * @return the filename of the SBOLExplorer log file
  1504. *
  1505. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1506. * @throws IOException if there is an I/O error
  1507. */
  1508. public String getExplorerLog(OutputStream outputStream) throws SynBioHubException, IOException
  1509. {
  1510. String url = backendUrl + "/admin/explorerLog";
  1511. return fetchContentSaveToFile(url,outputStream,null);
  1512. }
  1513. /**
  1514. * Get theme configuration for a SynBioHub
  1515. *
  1516. * @return A JSON object of the theme configuration for a SynBioHub
  1517. *
  1518. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1519. */
  1520. public JSONObject getThemeConfig() throws SynBioHubException
  1521. {
  1522. return getConfig("theme");
  1523. }
  1524. /**
  1525. * Set the theme configuration for a SynBioHub
  1526. *
  1527. * @param instanceName Name of the SynBioHub instance
  1528. * @param frontPageText Text to show on front page of the SynBioHub intance
  1529. * @param baseColor Base color to use fo this SynBioHub instance
  1530. * @param showModuleInteractions Should module interactions be shown using VisBol
  1531. * @param logoFileName Name of the logo file
  1532. *
  1533. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1534. * @throws FileNotFoundException if the file is not found
  1535. */
  1536. public void setThemeConfig(String instanceName, String frontPageText, String baseColor,
  1537. boolean showModuleInteractions, String logoFileName) throws SynBioHubException, FileNotFoundException
  1538. {
  1539. setThemeConfig(instanceName, frontPageText, baseColor, showModuleInteractions, new File(logoFileName));
  1540. }
  1541. /**
  1542. * Set the theme configuration for a SynBioHub
  1543. *
  1544. * @param instanceName Name of the SynBioHub instance
  1545. * @param frontPageText Text to show on front page of the SynBioHub intance
  1546. * @param baseColor Base color to use fo this SynBioHub instance
  1547. * @param showModuleInteractions Should module interactions be shown using VisBol
  1548. * @param logoFile File for the logo file
  1549. *
  1550. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1551. * @throws FileNotFoundException if the file is not found
  1552. */
  1553. public void setThemeConfig(String instanceName, String frontPageText, String baseColor,
  1554. boolean showModuleInteractions, File logoFile) throws SynBioHubException, FileNotFoundException
  1555. {
  1556. String name = logoFile.getName();
  1557. InputStream inputStream = new FileInputStream(logoFile);
  1558. setThemeConfig(instanceName, frontPageText, baseColor, showModuleInteractions, inputStream, name);
  1559. }
  1560. /**
  1561. * Set the theme configuration for a SynBioHub
  1562. *
  1563. * @param instanceName Name of the SynBioHub instance
  1564. * @param frontPageText Text to show on front page of the SynBioHub intance
  1565. * @param baseColor Base color to use fo this SynBioHub instance
  1566. * @param showModuleInteractions Should module interactions be shown using VisBol
  1567. * @param logoInputStream InputStream for logo file
  1568. *
  1569. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1570. */
  1571. public void setThemeConfig(String instanceName, String frontPageText, String baseColor,
  1572. boolean showModuleInteractions, InputStream logoInputStream) throws SynBioHubException
  1573. {
  1574. setThemeConfig(instanceName, frontPageText, baseColor, showModuleInteractions, logoInputStream, "file");
  1575. }
  1576. /**
  1577. * Set the theme configuration for a SynBioHub
  1578. *
  1579. * @param instanceName Name of the SynBioHub instance
  1580. * @param frontPageText Text to show on front page of the SynBioHub intance
  1581. * @param baseColor Base color to use fo this SynBioHub instance
  1582. * @param showModuleInteractions Should module interactions be shown using VisBol
  1583. * @param logoInputStream InputStream for logo file
  1584. * @param logoFileName Name of the logo file
  1585. *
  1586. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1587. */
  1588. public void setThemeConfig(String instanceName, String frontPageText, String baseColor,
  1589. boolean showModuleInteractions, InputStream logoInputStream, String logoFileName) throws SynBioHubException
  1590. {
  1591. if (user.equals("")) {
  1592. Exception e = new Exception("Must be logged in to set the theme configuration.");
  1593. throw new SynBioHubException(e);
  1594. }
  1595. String url = backendUrl + "/admin/theme";
  1596. HttpPost request = new HttpPost(url);
  1597. request.setHeader("X-authorization", user);
  1598. request.setHeader("Accept", "text/plain");
  1599. MultipartEntityBuilder params = MultipartEntityBuilder.create();
  1600. params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  1601. params.addTextBody("instanceName", instanceName);
  1602. params.addTextBody("frontPageText", frontPageText);
  1603. params.addTextBody("baseColor", baseColor);
  1604. if (showModuleInteractions) {
  1605. params.addTextBody("showModuleInteractions", "true");
  1606. }
  1607. if (logoInputStream != null) {
  1608. params.addBinaryBody("logo", logoInputStream, ContentType.DEFAULT_BINARY, logoFileName);
  1609. } else {
  1610. params.addTextBody("logo", "");
  1611. }
  1612. try
  1613. {
  1614. request.setEntity(params.build());
  1615. HttpResponse response = client.execute(request);
  1616. checkResponseCode(response);
  1617. }
  1618. catch (Exception e)
  1619. {
  1620. //e.printStackTrace();
  1621. throw new SynBioHubException(e);
  1622. }
  1623. finally
  1624. {
  1625. request.releaseConnection();
  1626. }
  1627. }
  1628. /**
  1629. * Get users configuration for a SynBioHub
  1630. *
  1631. * @return A JSON object of the users configuration for a SynBioHub
  1632. *
  1633. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1634. */
  1635. public JSONObject getUsersConfig() throws SynBioHubException
  1636. {
  1637. return getConfig("users");
  1638. }
  1639. /**
  1640. * Set the users configuration for a SynBioHub
  1641. *
  1642. * @param allowPublicSignup Flag indicating if public signup is allowed
  1643. *
  1644. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1645. */
  1646. public void setUsersConfig(boolean allowPublicSignup) throws SynBioHubException
  1647. {
  1648. if (user.equals("")) {
  1649. Exception e = new Exception("Must be logged in to set the users configuration.");
  1650. throw new SynBioHubException(e);
  1651. }
  1652. String url = backendUrl + "/admin/users";
  1653. HttpPost request = new HttpPost(url);
  1654. request.setHeader("X-authorization", user);
  1655. request.setHeader("Accept", "text/plain");
  1656. List<NameValuePair> arguments = new ArrayList<>(4);
  1657. if (allowPublicSignup) {
  1658. arguments.add(new BasicNameValuePair("allowPublicSignup", "true"));
  1659. }
  1660. try
  1661. {
  1662. request.setEntity(new UrlEncodedFormEntity(arguments));
  1663. HttpResponse response = client.execute(request);
  1664. checkResponseCode(response);
  1665. }
  1666. catch (Exception e)
  1667. {
  1668. //e.printStackTrace();
  1669. throw new SynBioHubException(e);
  1670. }
  1671. finally
  1672. {
  1673. request.releaseConnection();
  1674. }
  1675. }
  1676. /**
  1677. * Add a new uesr to a SynBioHub
  1678. *
  1679. * @param username username of new user
  1680. * @param name name of new user
  1681. * @param email email of new user
  1682. * @param affiliation affiliation of new user
  1683. * @param isMember is the new user a member of the team
  1684. * @param isCurator is the new user a curator
  1685. * @param isAdmin is the new user an administrator
  1686. *
  1687. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1688. */
  1689. public void addNewUser(String username, String name, String email, String affiliation,
  1690. boolean isMember, boolean isCurator, boolean isAdmin) throws SynBioHubException
  1691. {
  1692. if (user.equals("")) {
  1693. Exception e = new Exception("Must be logged in to add a new user.");
  1694. throw new SynBioHubException(e);
  1695. }
  1696. String url = backendUrl + "/admin/newUser";
  1697. HttpPost request = new HttpPost(url);
  1698. request.setHeader("X-authorization", user);
  1699. request.setHeader("Accept", "text/plain");
  1700. List<NameValuePair> arguments = new ArrayList<>(4);
  1701. arguments.add(new BasicNameValuePair("username", username));
  1702. arguments.add(new BasicNameValuePair("name", name));
  1703. arguments.add(new BasicNameValuePair("email", email));
  1704. arguments.add(new BasicNameValuePair("affiliation", affiliation));
  1705. if (isMember) {
  1706. arguments.add(new BasicNameValuePair("isMember", "true"));
  1707. }
  1708. if (isCurator) {
  1709. arguments.add(new BasicNameValuePair("isCurator", "true"));
  1710. }
  1711. if (isAdmin) {
  1712. arguments.add(new BasicNameValuePair("isAdmin", "true"));
  1713. }
  1714. try
  1715. {
  1716. request.setEntity(new UrlEncodedFormEntity(arguments));
  1717. HttpResponse response = client.execute(request);
  1718. checkResponseCode(response);
  1719. }
  1720. catch (Exception e)
  1721. {
  1722. //e.printStackTrace();
  1723. throw new SynBioHubException(e);
  1724. }
  1725. finally
  1726. {
  1727. request.releaseConnection();
  1728. }
  1729. }
  1730. /**
  1731. * Update a uesr on a SynBioHub
  1732. *
  1733. * @param id Id of the user to update
  1734. * @param name name of new user
  1735. * @param email email of new user
  1736. * @param affiliation affiliation of new user
  1737. * @param isMember is the new user a member of the team
  1738. * @param isCurator is the new user a curator
  1739. * @param isAdmin is the new user an administrator
  1740. *
  1741. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1742. */
  1743. public void updateUser(int id, String name, String email, String affiliation,
  1744. boolean isMember, boolean isCurator, boolean isAdmin) throws SynBioHubException
  1745. {
  1746. if (user.equals("")) {
  1747. Exception e = new Exception("Must be logged in to update a user.");
  1748. throw new SynBioHubException(e);
  1749. }
  1750. String url = backendUrl + "/admin/updateUser";
  1751. HttpPost request = new HttpPost(url);
  1752. request.setHeader("X-authorization", user);
  1753. request.setHeader("Accept", "text/plain");
  1754. List<NameValuePair> arguments = new ArrayList<>(4);
  1755. arguments.add(new BasicNameValuePair("id", Integer.toString(id)));
  1756. arguments.add(new BasicNameValuePair("name", name));
  1757. arguments.add(new BasicNameValuePair("email", email));
  1758. arguments.add(new BasicNameValuePair("affiliation", affiliation));
  1759. if (isMember) {
  1760. arguments.add(new BasicNameValuePair("isMember", "true"));
  1761. }
  1762. if (isCurator) {
  1763. arguments.add(new BasicNameValuePair("isCurator", "true"));
  1764. }
  1765. if (isAdmin) {
  1766. arguments.add(new BasicNameValuePair("isAdmin", "true"));
  1767. }
  1768. try
  1769. {
  1770. request.setEntity(new UrlEncodedFormEntity(arguments));
  1771. HttpResponse response = client.execute(request);
  1772. checkResponseCode(response);
  1773. }
  1774. catch (Exception e)
  1775. {
  1776. //e.printStackTrace();
  1777. throw new SynBioHubException(e);
  1778. }
  1779. finally
  1780. {
  1781. request.releaseConnection();
  1782. }
  1783. }
  1784. /**
  1785. * Delete a user from a SynBioHub
  1786. *
  1787. * @param id Id of user to delete
  1788. *
  1789. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1790. */
  1791. public void deleteUser(int id) throws SynBioHubException
  1792. {
  1793. if (user.equals("")) {
  1794. Exception e = new Exception("Must be logged in to delete a user.");
  1795. throw new SynBioHubException(e);
  1796. }
  1797. String url = backendUrl + "/admin/deleteUser";
  1798. HttpPost request = new HttpPost(url);
  1799. request.setHeader("X-authorization", user);
  1800. request.setHeader("Accept", "text/plain");
  1801. List<NameValuePair> arguments = new ArrayList<>(4);
  1802. arguments.add(new BasicNameValuePair("id", Integer.toString(id)));
  1803. try
  1804. {
  1805. request.setEntity(new UrlEncodedFormEntity(arguments));
  1806. HttpResponse response = client.execute(request);
  1807. checkResponseCode(response);
  1808. }
  1809. catch (Exception e)
  1810. {
  1811. //e.printStackTrace();
  1812. throw new SynBioHubException(e);
  1813. }
  1814. finally
  1815. {
  1816. request.releaseConnection();
  1817. }
  1818. }
  1819. /**
  1820. * Get remotes configuration for a SynBioHub
  1821. *
  1822. * @return A JSON object of the remotes configuration for a SynBioHub
  1823. *
  1824. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1825. */
  1826. private JSONObject getConfig(String configType) throws SynBioHubException
  1827. {
  1828. String configTypeStr = "";
  1829. if (!configType.equals("")) {
  1830. configTypeStr += configType + " ";
  1831. }
  1832. configTypeStr += "configuration";
  1833. String url = backendUrl + "/admin";
  1834. if (!configType.equals("")) {
  1835. url += "/" + configType;
  1836. }
  1837. return getJSON(url,configTypeStr);
  1838. }
  1839. /**
  1840. * Get logged in user profile from a SynBioHub
  1841. *
  1842. * @return A JSON object of the logged in user profile from a SynBioHub
  1843. *
  1844. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1845. */
  1846. public JSONObject getUserProfile() throws SynBioHubException
  1847. {
  1848. return getJSON(backendUrl+"/profile","user profile");
  1849. }
  1850. /**
  1851. * Register a new user in SynBioHub
  1852. *
  1853. * @param username User name of the user
  1854. * @param name Name of the user
  1855. * @param affiliation Affilation of the user
  1856. * @param email Email address of the user
  1857. * @param password Password for the user
  1858. *
  1859. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1860. */
  1861. public void registerUser(String username, String name, String affiliation, String email, String password) throws SynBioHubException
  1862. {
  1863. addUpdateUserProfile(username,name,affiliation,email,password);
  1864. }
  1865. /**
  1866. * Update the logged in user's profile
  1867. *
  1868. * @param name Name of the user
  1869. * @param affiliation Affilation of the user
  1870. * @param email Email address of the user
  1871. * @param password Password for the user
  1872. *
  1873. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1874. */
  1875. public void updateUserProfile(String name, String affiliation, String email, String password) throws SynBioHubException
  1876. {
  1877. addUpdateUserProfile(null,name,affiliation,email,password);
  1878. }
  1879. /**
  1880. * Update the logged in user's profile
  1881. *
  1882. * @param username User name of the user
  1883. * @param name Name of the user
  1884. * @param affiliation Affilation of the user
  1885. * @param email Email address of the user
  1886. * @param password Password for the user
  1887. *
  1888. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1889. */
  1890. private void addUpdateUserProfile(String username, String name, String affiliation, String email, String password) throws SynBioHubException
  1891. {
  1892. if (username == null && user.equals("")) {
  1893. Exception e = new Exception("Must be logged in to update the logged in user's profile.");
  1894. throw new SynBioHubException(e);
  1895. }
  1896. String url;
  1897. if (username != null) {
  1898. url = backendUrl + "/register";
  1899. } else {
  1900. url = backendUrl + "/profile";
  1901. }
  1902. HttpPost request = new HttpPost(url);
  1903. request.setHeader("X-authorization", user);
  1904. request.setHeader("Accept", "text/plain");
  1905. List<NameValuePair> arguments = new ArrayList<>(4);
  1906. if (username != null) {
  1907. arguments.add(new BasicNameValuePair("username", username));
  1908. }
  1909. arguments.add(new BasicNameValuePair("name", name));
  1910. arguments.add(new BasicNameValuePair("affiliation", affiliation));
  1911. arguments.add(new BasicNameValuePair("email", email));
  1912. arguments.add(new BasicNameValuePair("password1", password));
  1913. arguments.add(new BasicNameValuePair("password2", password));
  1914. try
  1915. {
  1916. request.setEntity(new UrlEncodedFormEntity(arguments));
  1917. HttpResponse response = client.execute(request);
  1918. checkResponseCode(response);
  1919. }
  1920. catch (Exception e)
  1921. {
  1922. //e.printStackTrace();
  1923. throw new SynBioHubException(e);
  1924. }
  1925. finally
  1926. {
  1927. request.releaseConnection();
  1928. }
  1929. }
  1930. /**
  1931. * Request password reset
  1932. *
  1933. * @param email Email address of the user to send password reset link
  1934. *
  1935. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1936. */
  1937. public void requestPasswordReset(String email) throws SynBioHubException
  1938. {
  1939. String url = backendUrl + "/resetPassword";
  1940. HttpPost request = new HttpPost(url);
  1941. request.setHeader("X-authorization", user);
  1942. request.setHeader("Accept", "text/plain");
  1943. List<NameValuePair> arguments = new ArrayList<>(4);
  1944. arguments.add(new BasicNameValuePair("email", email));
  1945. try
  1946. {
  1947. request.setEntity(new UrlEncodedFormEntity(arguments));
  1948. HttpResponse response = client.execute(request);
  1949. checkResponseCode(response);
  1950. }
  1951. catch (Exception e)
  1952. {
  1953. //e.printStackTrace();
  1954. throw new SynBioHubException(e);
  1955. }
  1956. finally
  1957. {
  1958. request.releaseConnection();
  1959. }
  1960. }
  1961. /**
  1962. * Set new password
  1963. *
  1964. * @param token Token received by email to reset a password
  1965. * @param password New password
  1966. *
  1967. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  1968. */
  1969. public void setNewPassword(String token, String password) throws SynBioHubException
  1970. {
  1971. String url = backendUrl + "/setNewPassword";
  1972. HttpPost request = new HttpPost(url);
  1973. request.setHeader("X-authorization", user);
  1974. request.setHeader("Accept", "text/plain");
  1975. List<NameValuePair> arguments = new ArrayList<>(4);
  1976. arguments.add(new BasicNameValuePair("token", token));
  1977. arguments.add(new BasicNameValuePair("password1", password));
  1978. arguments.add(new BasicNameValuePair("password2", password));
  1979. try
  1980. {
  1981. request.setEntity(new UrlEncodedFormEntity(arguments));
  1982. HttpResponse response = client.execute(request);
  1983. checkResponseCode(response);
  1984. }
  1985. catch (Exception e)
  1986. {
  1987. //e.printStackTrace();
  1988. throw new SynBioHubException(e);
  1989. }
  1990. finally
  1991. {
  1992. request.releaseConnection();
  1993. }
  1994. }
  1995. /**
  1996. * Get remotes configuration for a SynBioHub
  1997. *
  1998. * @return A JSON object of the remotes configuration for a SynBioHub
  1999. *
  2000. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2001. */
  2002. private JSONObject getJSON(String url,String message) throws SynBioHubException
  2003. {
  2004. if (user.equals("")) {
  2005. Exception e = new Exception("Must be logged in to get the "+ message + ".");
  2006. throw new SynBioHubException(e);
  2007. }
  2008. HttpGet request = new HttpGet(url);
  2009. request.setHeader("X-authorization", user);
  2010. request.setHeader("Accept", "text/plain");
  2011. try
  2012. {
  2013. HttpResponse response = client.execute(request);
  2014. checkResponseCode(response);
  2015. InputStream inputStream = response.getEntity().getContent();
  2016. JSONParser parser = new JSONParser();
  2017. JSONObject config = (JSONObject) parser.parse(inputStreamToString(inputStream));
  2018. return config;
  2019. }
  2020. catch (Exception e)
  2021. {
  2022. throw new SynBioHubException(e);
  2023. }
  2024. finally
  2025. {
  2026. request.releaseConnection();
  2027. }
  2028. }
  2029. /**
  2030. * Fetch data about all registries in the web of registries.
  2031. *
  2032. * @return An ArrayList of WebOfRegistriesData describing each registry in the web of registries.
  2033. *
  2034. * @throws SynBioHubException if there was an error communicating with the WebOfRegistries
  2035. */
  2036. public static ArrayList<WebOfRegistriesData> getRegistries() throws SynBioHubException
  2037. {
  2038. return getRegistries("https://wor.synbiohub.org");
  2039. }
  2040. /**
  2041. * Fetch data about all registries in the web of registries.
  2042. * @param webOfRegistriesUrl The URL for the web-of-registries.
  2043. *
  2044. * @return An ArrayList of WebOfRegistriesData describing each registry in the web of registries.
  2045. *
  2046. * @throws SynBioHubException if there was an error communicating with the web-of-registries
  2047. */
  2048. public static ArrayList<WebOfRegistriesData> getRegistries(String webOfRegistriesUrl) throws SynBioHubException
  2049. {
  2050. PoolingHttpClientConnectionManager connectionManager;
  2051. HttpClient client;
  2052. connectionManager = new PoolingHttpClientConnectionManager();
  2053. client = HttpClients.custom().setConnectionManager(connectionManager).build();
  2054. String url = webOfRegistriesUrl + "/instances/";
  2055. Gson gson = new Gson();
  2056. HttpGet request = new HttpGet(url);
  2057. request.setHeader("Accept", "text/plain");
  2058. try
  2059. {
  2060. HttpResponse response = client.execute(request);
  2061. checkResponseCode(response);
  2062. InputStream inputStream = response.getEntity().getContent();
  2063. ArrayList<WebOfRegistriesData> metadataList = gson.fromJson(
  2064. new InputStreamReader(inputStream),
  2065. new TypeToken<ArrayList<WebOfRegistriesData>>(){}.getType());
  2066. return metadataList;
  2067. }
  2068. catch (Exception e)
  2069. {
  2070. throw new SynBioHubException(e);
  2071. }
  2072. finally
  2073. {
  2074. request.releaseConnection();
  2075. }
  2076. }
  2077. /**
  2078. * Perform a SPARQL query
  2079. * @param query SPARQL query string
  2080. *
  2081. * @return result as a JSON string
  2082. *
  2083. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2084. */
  2085. public String sparqlQuery(String query) throws SynBioHubException
  2086. {
  2087. String url = backendUrl + "/sparql";
  2088. url += "?query="+encodeUri(query);
  2089. HttpGet request = new HttpGet(url);
  2090. request.setHeader("X-authorization", user);
  2091. request.setHeader("Accept", "application/json");
  2092. try
  2093. {
  2094. HttpResponse response = client.execute(request);
  2095. checkResponseCode(response);
  2096. InputStream inputStream = response.getEntity().getContent();
  2097. String result = inputStreamToString(inputStream);
  2098. return result;
  2099. }
  2100. catch (Exception e)
  2101. {
  2102. throw new SynBioHubException(e);
  2103. }
  2104. finally
  2105. {
  2106. request.releaseConnection();
  2107. }
  2108. }
  2109. /**
  2110. * Perform a SPARQL admin query, must be logged in as an administrator
  2111. * @param query SPARQL query string
  2112. *
  2113. * @return result as a JSON string
  2114. *
  2115. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2116. */
  2117. public String sparqlAdminQuery(String query) throws SynBioHubException
  2118. {
  2119. String url = backendUrl + "/admin/sparql";
  2120. url += "?query="+encodeUri(query);
  2121. HttpGet request = new HttpGet(url);
  2122. request.setHeader("X-authorization", user);
  2123. request.setHeader("Accept", "application/json");
  2124. try
  2125. {
  2126. HttpResponse response = client.execute(request);
  2127. checkResponseCode(response);
  2128. InputStream inputStream = response.getEntity().getContent();
  2129. String result = inputStreamToString(inputStream);
  2130. return result;
  2131. }
  2132. catch (Exception e)
  2133. {
  2134. throw new SynBioHubException(e);
  2135. }
  2136. finally
  2137. {
  2138. request.releaseConnection();
  2139. }
  2140. }
  2141. /**
  2142. * Search the default store for Collections that are members of the specified Collection
  2143. *
  2144. * @param parentCollectionUri URI for Collection to search for member Collections
  2145. * @return An ArrayList of CollectionMetaData objects with a summary of all matching Collections.
  2146. *
  2147. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2148. */
  2149. public ArrayList<IdentifiedMetadata> getSubCollectionMetadata(URI parentCollectionUri)
  2150. throws SynBioHubException
  2151. {
  2152. return search(parentCollectionUri,"subCollections",0);
  2153. }
  2154. /**
  2155. * Search for uses of an object in SynBioHub
  2156. * returns up to 50 starting from the offset
  2157. *
  2158. * @param topLevelUri URI for object to search for uses of
  2159. * @param offset offset of first object to return
  2160. * @return An ArrayList of IdentifiedMetaData objects with a summary of all uses.
  2161. *
  2162. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2163. */
  2164. public ArrayList<IdentifiedMetadata> getUses(URI topLevelUri,int offset) throws SynBioHubException
  2165. {
  2166. return search(topLevelUri,"uses",offset);
  2167. }
  2168. /**
  2169. * Search for twins of an object in SynBioHub
  2170. * returns up to 50 starting from the offset
  2171. *
  2172. * @param topLevelUri URI for object to search for twins of
  2173. * @param offset offset of first object to return
  2174. * @return An ArrayList of IdentifiedMetaData objects with a summary of all twins.
  2175. *
  2176. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2177. */
  2178. public ArrayList<IdentifiedMetadata> getTwins(URI topLevelUri,int offset) throws SynBioHubException
  2179. {
  2180. return search(topLevelUri,"twins",offset);
  2181. }
  2182. /**
  2183. * Search for similar objects in SynBioHub
  2184. * @param offset offset of first object to return
  2185. * returns up to 50 starting from the offset
  2186. *
  2187. * @param topLevelUri URI for object to search for similar objects of
  2188. * @return An ArrayList of IdentifiedMetaData objects with a summary of all similar.
  2189. *
  2190. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2191. */
  2192. public ArrayList<IdentifiedMetadata> getSimilar(URI topLevelUri,int offset) throws SynBioHubException
  2193. {
  2194. return search(topLevelUri,"similar",offset);
  2195. }
  2196. private ArrayList<IdentifiedMetadata> search(URI topLevelUri, String searchType, int offset) throws SynBioHubException
  2197. {
  2198. if (!topLevelUri.toString().startsWith(uriPrefix)) {
  2199. throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
  2200. }
  2201. String url = topLevelUri + "/" + searchType;
  2202. if (offset!=0) {
  2203. url += "/?offset="+offset;
  2204. }
  2205. url = url.replace(uriPrefix, backendUrl);
  2206. Gson gson = new Gson();
  2207. HttpGet request = new HttpGet(url);
  2208. request.setHeader("X-authorization", user);
  2209. request.setHeader("Accept", "text/plain");
  2210. try
  2211. {
  2212. HttpResponse response = client.execute(request);
  2213. checkResponseCode(response);
  2214. InputStream inputStream = response.getEntity().getContent();
  2215. ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
  2216. new InputStreamReader(inputStream),
  2217. new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
  2218. return metadataList;
  2219. }
  2220. catch (Exception e)
  2221. {
  2222. throw new SynBioHubException(e);
  2223. }
  2224. finally
  2225. {
  2226. request.releaseConnection();
  2227. }
  2228. }
  2229. /**
  2230. * Sets the user to null to indicate that no user is logged in.
  2231. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2232. */
  2233. public void logout() throws SynBioHubException
  2234. {
  2235. String url = backendUrl + "/logout";
  2236. HttpPost request = new HttpPost(url);
  2237. request.setHeader("Accept", "text/plain");
  2238. request.setHeader("X-authorization", user);
  2239. List<NameValuePair> params = new ArrayList<NameValuePair>();
  2240. try
  2241. {
  2242. request.setEntity(new UrlEncodedFormEntity(params));
  2243. request.setHeader("Content-Type", "application/x-www-form-urlencoded");
  2244. HttpResponse response = client.execute(request);
  2245. checkResponseCode(response);
  2246. user = "";
  2247. username = null;
  2248. }
  2249. catch (Exception e)
  2250. {
  2251. throw new SynBioHubException(e);
  2252. }
  2253. finally
  2254. {
  2255. request.releaseConnection();
  2256. }
  2257. }
  2258. /**
  2259. * Returns if a user is logged in
  2260. *
  2261. * @return true if a user is logged in
  2262. */
  2263. public boolean isSetUsername()
  2264. {
  2265. return (username!=null);
  2266. }
  2267. /**
  2268. * Returns the username of the logged in user
  2269. *
  2270. * @return the username of the logged in user
  2271. */
  2272. public String getUsername()
  2273. {
  2274. return username;
  2275. }
  2276. /**
  2277. * Login to the SynBioHub.
  2278. * @param email The user's email
  2279. * @param password The user's password
  2280. *
  2281. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2282. */
  2283. public void login(String email, String password) throws SynBioHubException
  2284. {
  2285. String url = backendUrl + "/login";
  2286. HttpPost request = new HttpPost(url);
  2287. request.setHeader("Accept", "text/plain");
  2288. List<NameValuePair> params = new ArrayList<NameValuePair>();
  2289. params.add(new BasicNameValuePair("email", email));
  2290. params.add(new BasicNameValuePair("password", password));
  2291. try
  2292. {
  2293. request.setEntity(new UrlEncodedFormEntity(params));
  2294. request.setHeader("Content-Type", "application/x-www-form-urlencoded");
  2295. HttpResponse response = client.execute(request);
  2296. checkResponseCode(response);
  2297. HttpEntity entity = response.getEntity();
  2298. user = inputStreamToString(entity.getContent());
  2299. username = email;
  2300. }
  2301. catch (Exception e)
  2302. {
  2303. throw new SynBioHubException(e);
  2304. }
  2305. finally
  2306. {
  2307. request.releaseConnection();
  2308. }
  2309. }
  2310. /**
  2311. * Remove all parts from this registry from a given SBOL document
  2312. *
  2313. * @param document The document to remove all registry parts from
  2314. */
  2315. public void removeRegistryParts(SBOLDocument document) {
  2316. for (TopLevel topLevel : document.getTopLevels()) {
  2317. if (topLevel.getIdentity().toString().startsWith(uriPrefix)) {
  2318. try {
  2319. document.removeTopLevel(topLevel);
  2320. }
  2321. catch (SBOLValidationException e) {
  2322. // TODO: ignore for now
  2323. }
  2324. }
  2325. }
  2326. }
  2327. /**
  2328. * Update a collection icon for a collection in SynBioHub.
  2329. * @param topLevelUri identity of the collection
  2330. * @param filename the name of the file for the collection icon
  2331. *
  2332. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2333. * @throws FileNotFoundException if the file is not found
  2334. */
  2335. public void updateCollectionIcon(URI topLevelUri, String filename) throws SynBioHubException, FileNotFoundException
  2336. {
  2337. if (user.equals("")) {
  2338. Exception e = new Exception("Must be logged in to update a collection icon.");
  2339. throw new SynBioHubException(e);
  2340. }
  2341. File file = new File(filename);
  2342. InputStream inputStream = new FileInputStream(file);
  2343. String url = topLevelUri + "/icon";
  2344. url = url.replace(uriPrefix, backendUrl);
  2345. HttpPost request = new HttpPost(url);
  2346. request.setHeader("X-authorization", user);
  2347. request.setHeader("Accept", "text/plain");
  2348. MultipartEntityBuilder params = MultipartEntityBuilder.create();
  2349. /* example for setting a HttpMultipartMode */
  2350. params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  2351. params.addBinaryBody("collectionIcon", inputStream, ContentType.DEFAULT_BINARY, filename);
  2352. try
  2353. {
  2354. request.setEntity(params.build());
  2355. HttpResponse response = client.execute(request);
  2356. checkResponseCode(response);
  2357. }
  2358. catch (Exception e)
  2359. {
  2360. //e.printStackTrace();
  2361. throw new SynBioHubException(e);
  2362. }
  2363. finally
  2364. {
  2365. request.releaseConnection();
  2366. }
  2367. }
  2368. /**
  2369. * Attach a file to an object in SynBioHub.
  2370. * @param topLevelUri identity of the object to attach the file to
  2371. * @param filename the name of the file to attach
  2372. *
  2373. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2374. * @throws FileNotFoundException if the file is not found
  2375. */
  2376. public void attachFile(URI topLevelUri, String filename) throws SynBioHubException, FileNotFoundException
  2377. {
  2378. attachFile(topLevelUri,new File(filename));
  2379. }
  2380. /**
  2381. * Attach a file to an object in SynBioHub.
  2382. * @param topLevelUri identity of the object to attach the file to
  2383. * @param file the file to attach
  2384. *
  2385. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2386. * @throws FileNotFoundException if the file is not found
  2387. */
  2388. public void attachFile(URI topLevelUri, File file) throws SynBioHubException, FileNotFoundException
  2389. {
  2390. String name = file.getName();
  2391. InputStream inputStream = new FileInputStream(file);
  2392. attachFile(topLevelUri,inputStream,name);
  2393. }
  2394. /**
  2395. * Attach a file to an object in SynBioHub.
  2396. * @param topLevelUri identity of the object to attach the file to
  2397. * @param inputStream the inputStream to attach
  2398. *
  2399. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2400. */
  2401. public void attachFile(URI topLevelUri, InputStream inputStream) throws SynBioHubException
  2402. {
  2403. attachFile(topLevelUri,inputStream,"file");
  2404. }
  2405. /**
  2406. * Attach a file to an object in SynBioHub.
  2407. * @param topLevelUri identity of the object to attach the URL to
  2408. * @param inputStream the inputStream to attach
  2409. * @param filename name of file to attach
  2410. *
  2411. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2412. */
  2413. public void attachFile(URI topLevelUri, InputStream inputStream, String filename) throws SynBioHubException
  2414. {
  2415. if (user.equals("")) {
  2416. Exception e = new Exception("Must be logged in to add attachments.");
  2417. throw new SynBioHubException(e);
  2418. }
  2419. String url = topLevelUri + "/attach";
  2420. url = url.replace(uriPrefix, backendUrl);
  2421. HttpPost request = new HttpPost(url);
  2422. request.setHeader("X-authorization", user);
  2423. request.setHeader("Accept", "text/plain");
  2424. MultipartEntityBuilder params = MultipartEntityBuilder.create();
  2425. params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  2426. params.addTextBody("user", user);
  2427. params.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, filename);
  2428. try
  2429. {
  2430. request.setEntity(params.build());
  2431. HttpResponse response = client.execute(request);
  2432. checkResponseCode(response);
  2433. }
  2434. catch (Exception e)
  2435. {
  2436. //e.printStackTrace();
  2437. throw new SynBioHubException(e);
  2438. }
  2439. finally
  2440. {
  2441. request.releaseConnection();
  2442. }
  2443. }
  2444. /**
  2445. * Attach a URL to an object in SynBioHub.
  2446. * @param topLevelUri identity of the object to attach the file to
  2447. * @param attachmentURL the URL to attach
  2448. * @param attachmentType the format type of the object at the URL
  2449. * @param name the name of the attachment
  2450. *
  2451. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2452. */
  2453. public void attachURL(URI topLevelUri, URI attachmentURL, URI attachmentType, String name) throws SynBioHubException
  2454. {
  2455. if (user.equals("")) {
  2456. Exception e = new Exception("Must be logged in to add attachments.");
  2457. throw new SynBioHubException(e);
  2458. }
  2459. String url = topLevelUri + "/attachUrl";
  2460. url = url.replace(uriPrefix, backendUrl);
  2461. HttpPost request = new HttpPost(url);
  2462. request.setHeader("X-authorization", user);
  2463. request.setHeader("Accept", "text/plain");
  2464. List<NameValuePair> arguments = new ArrayList<>(4);
  2465. arguments.add(new BasicNameValuePair("user", user));
  2466. arguments.add(new BasicNameValuePair("url", attachmentURL.toString()));
  2467. arguments.add(new BasicNameValuePair("name", name));
  2468. arguments.add(new BasicNameValuePair("type", attachmentType.toString()));
  2469. try
  2470. {
  2471. request.setEntity(new UrlEncodedFormEntity(arguments));
  2472. HttpResponse response = client.execute(request);
  2473. checkResponseCode(response);
  2474. }
  2475. catch (Exception e)
  2476. {
  2477. //e.printStackTrace();
  2478. throw new SynBioHubException(e);
  2479. }
  2480. finally
  2481. {
  2482. request.releaseConnection();
  2483. }
  2484. }
  2485. /**
  2486. * Update mutable description of an object in SynBioHub.
  2487. * @param topLevelUri identity of the object to update
  2488. * @param value the new value for the mutable description
  2489. *
  2490. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2491. */
  2492. public void updateMutableDescription(URI topLevelUri, String value) throws SynBioHubException
  2493. {
  2494. updateMutable(topLevelUri,value,"updateMutableDescription");
  2495. }
  2496. /**
  2497. * Update mutable notes of an object in SynBioHub.
  2498. * @param topLevelUri identity of the object to update
  2499. * @param value the new value for the mutable notes
  2500. *
  2501. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2502. */
  2503. public void updateMutableNotes(URI topLevelUri, String value) throws SynBioHubException
  2504. {
  2505. updateMutable(topLevelUri,value,"updateMutableNotes");
  2506. }
  2507. /**
  2508. * Update mutable source of an object in SynBioHub.
  2509. * @param topLevelUri identity of the object to update
  2510. * @param value the new value for the mutable source
  2511. *
  2512. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2513. */
  2514. public void updateMutableSource(URI topLevelUri, String value) throws SynBioHubException
  2515. {
  2516. updateMutable(topLevelUri,value,"updateMutableSource");
  2517. }
  2518. /**
  2519. * Update citations of an object in SynBioHub.
  2520. * @param topLevelUri identity of the object to update
  2521. * @param value a comma separated list of PubMed ids
  2522. *
  2523. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2524. */
  2525. public void updateCitations(URI topLevelUri, String value) throws SynBioHubException
  2526. {
  2527. updateMutable(topLevelUri,value,"updateCitations");
  2528. }
  2529. private void updateMutable(URI topLevelUri, String value, String endpoint) throws SynBioHubException
  2530. {
  2531. if (user.equals("")) {
  2532. Exception e = new Exception("Must be logged in to update mutable descriptions.");
  2533. throw new SynBioHubException(e);
  2534. }
  2535. String url = backendUrl + "/" + endpoint;
  2536. HttpPost request = new HttpPost(url);
  2537. request.setHeader("X-authorization", user);
  2538. request.setHeader("Accept", "text/plain");
  2539. List<NameValuePair> arguments = new ArrayList<>(4);
  2540. arguments.add(new BasicNameValuePair("user", user));
  2541. arguments.add(new BasicNameValuePair("uri", topLevelUri.toString()));
  2542. arguments.add(new BasicNameValuePair("value", value));
  2543. try
  2544. {
  2545. request.setEntity(new UrlEncodedFormEntity(arguments));
  2546. HttpResponse response = client.execute(request);
  2547. checkResponseCode(response);
  2548. }
  2549. catch (Exception e)
  2550. {
  2551. //e.printStackTrace();
  2552. throw new SynBioHubException(e);
  2553. }
  2554. finally
  2555. {
  2556. request.releaseConnection();
  2557. }
  2558. }
  2559. /**
  2560. * Edit field of an object in SynBioHub.
  2561. * @param topLevelUri identity of the object to edit
  2562. * @param field field of the object to edit
  2563. * @param previous previous value of the field
  2564. * @param object new value of the field
  2565. *
  2566. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2567. */
  2568. public void editField(URI topLevelUri, String field, String previous, String object) throws SynBioHubException
  2569. {
  2570. if (user.equals("")) {
  2571. Exception e = new Exception("Must be logged in to edit fields.");
  2572. throw new SynBioHubException(e);
  2573. }
  2574. String url = topLevelUri + "/edit/"+field;
  2575. url = url.replace(uriPrefix, backendUrl);
  2576. HttpPost request = new HttpPost(url);
  2577. request.setHeader("X-authorization", user);
  2578. request.setHeader("Accept", "text/plain");
  2579. List<NameValuePair> arguments = new ArrayList<>(4);
  2580. arguments.add(new BasicNameValuePair("user", user));
  2581. arguments.add(new BasicNameValuePair("previous", previous));
  2582. arguments.add(new BasicNameValuePair("object", object));
  2583. try
  2584. {
  2585. request.setEntity(new UrlEncodedFormEntity(arguments));
  2586. HttpResponse response = client.execute(request);
  2587. checkResponseCode(response);
  2588. }
  2589. catch (Exception e)
  2590. {
  2591. //e.printStackTrace();
  2592. throw new SynBioHubException(e);
  2593. }
  2594. finally
  2595. {
  2596. request.releaseConnection();
  2597. }
  2598. }
  2599. /**
  2600. * Add field to an object in SynBioHub.
  2601. * @param topLevelUri identity of the object to add
  2602. * @param field field of the object to add to
  2603. * @param object new value of the field to add
  2604. *
  2605. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2606. */
  2607. public void addField(URI topLevelUri, String field, String object) throws SynBioHubException
  2608. {
  2609. if (user.equals("")) {
  2610. Exception e = new Exception("Must be logged in to add to fields.");
  2611. throw new SynBioHubException(e);
  2612. }
  2613. String url = topLevelUri + "/add/"+field;
  2614. url = url.replace(uriPrefix, backendUrl);
  2615. HttpPost request = new HttpPost(url);
  2616. request.setHeader("X-authorization", user);
  2617. request.setHeader("Accept", "text/plain");
  2618. List<NameValuePair> arguments = new ArrayList<>(4);
  2619. arguments.add(new BasicNameValuePair("user", user));
  2620. arguments.add(new BasicNameValuePair("object", object));
  2621. try
  2622. {
  2623. request.setEntity(new UrlEncodedFormEntity(arguments));
  2624. HttpResponse response = client.execute(request);
  2625. checkResponseCode(response);
  2626. }
  2627. catch (Exception e)
  2628. {
  2629. //e.printStackTrace();
  2630. throw new SynBioHubException(e);
  2631. }
  2632. finally
  2633. {
  2634. request.releaseConnection();
  2635. }
  2636. }
  2637. /**
  2638. * Remove field from an object in SynBioHub.
  2639. * @param topLevelUri identity of the object to remove
  2640. * @param field field of the object to remove from
  2641. * @param object value of the field to remove
  2642. *
  2643. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2644. */
  2645. public void removeField(URI topLevelUri, String field, String object) throws SynBioHubException
  2646. {
  2647. if (user.equals("")) {
  2648. Exception e = new Exception("Must be logged in to remove from fields.");
  2649. throw new SynBioHubException(e);
  2650. }
  2651. String url = topLevelUri + "/remove/"+field;
  2652. url = url.replace(uriPrefix, backendUrl);
  2653. HttpPost request = new HttpPost(url);
  2654. request.setHeader("X-authorization", user);
  2655. request.setHeader("Accept", "text/plain");
  2656. List<NameValuePair> arguments = new ArrayList<>(4);
  2657. arguments.add(new BasicNameValuePair("user", user));
  2658. arguments.add(new BasicNameValuePair("object", object));
  2659. try
  2660. {
  2661. request.setEntity(new UrlEncodedFormEntity(arguments));
  2662. HttpResponse response = client.execute(request);
  2663. checkResponseCode(response);
  2664. }
  2665. catch (Exception e)
  2666. {
  2667. //e.printStackTrace();
  2668. throw new SynBioHubException(e);
  2669. }
  2670. finally
  2671. {
  2672. request.releaseConnection();
  2673. }
  2674. }
  2675. /**
  2676. * Add member to a collection.
  2677. * @param collectionUri URI of collection to add a member to
  2678. * @param memberUri URI of the object to add as a member
  2679. *
  2680. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2681. */
  2682. public void addMember(URI collectionUri, URI memberUri) throws SynBioHubException
  2683. {
  2684. if (user.equals("")) {
  2685. Exception e = new Exception("Must be logged in to add to fields.");
  2686. throw new SynBioHubException(e);
  2687. }
  2688. String url = memberUri.toString() + "/addToCollection";
  2689. url = url.replace(uriPrefix, backendUrl);
  2690. HttpPost request = new HttpPost(url);
  2691. request.setHeader("X-authorization", user);
  2692. request.setHeader("Accept", "text/plain");
  2693. List<NameValuePair> arguments = new ArrayList<>(1);
  2694. arguments.add(new BasicNameValuePair("collections", collectionUri.toString()));
  2695. try
  2696. {
  2697. request.setEntity(new UrlEncodedFormEntity(arguments));
  2698. HttpResponse response = client.execute(request);
  2699. checkResponseCode(response);
  2700. }
  2701. catch (Exception e)
  2702. {
  2703. //e.printStackTrace();
  2704. throw new SynBioHubException(e);
  2705. }
  2706. finally
  2707. {
  2708. request.releaseConnection();
  2709. }
  2710. }
  2711. /**
  2712. * Remove member of a collection.
  2713. * @param collectionUri URI of collection to remove a member from
  2714. * @param memberUri URI of the object to remove as a member
  2715. *
  2716. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2717. */
  2718. public void removeMember(URI collectionUri, URI memberUri) throws SynBioHubException
  2719. {
  2720. if (user.equals("")) {
  2721. Exception e = new Exception("Must be logged in to add to fields.");
  2722. throw new SynBioHubException(e);
  2723. }
  2724. String url = collectionUri + "/removeMembership";
  2725. url = url.replace(uriPrefix, backendUrl);
  2726. HttpPost request = new HttpPost(url);
  2727. request.setHeader("X-authorization", user);
  2728. request.setHeader("Accept", "text/plain");
  2729. List<NameValuePair> arguments = new ArrayList<>(1);
  2730. arguments.add(new BasicNameValuePair("member", memberUri.toString()));
  2731. try
  2732. {
  2733. request.setEntity(new UrlEncodedFormEntity(arguments));
  2734. HttpResponse response = client.execute(request);
  2735. checkResponseCode(response);
  2736. }
  2737. catch (Exception e)
  2738. {
  2739. //e.printStackTrace();
  2740. throw new SynBioHubException(e);
  2741. }
  2742. finally
  2743. {
  2744. request.releaseConnection();
  2745. }
  2746. }
  2747. /**
  2748. * Add owner to an object in SynBioHub.
  2749. * @param topLevelUri identity of the object to add owner to
  2750. * @param userId user id of owner being added
  2751. *
  2752. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2753. */
  2754. public void addOwner(URI topLevelUri, String userId) throws SynBioHubException
  2755. {
  2756. if (user.equals("")) {
  2757. Exception e = new Exception("Must be logged in to add an owner.");
  2758. throw new SynBioHubException(e);
  2759. }
  2760. String url = topLevelUri + "/addOwner";
  2761. url = url.replace(uriPrefix, backendUrl);
  2762. HttpPost request = new HttpPost(url);
  2763. request.setHeader("X-authorization", user);
  2764. request.setHeader("Accept", "text/plain");
  2765. List<NameValuePair> arguments = new ArrayList<>(4);
  2766. arguments.add(new BasicNameValuePair("user", uriPrefix+"/user/"+userId));
  2767. arguments.add(new BasicNameValuePair("uri", topLevelUri.toString()));
  2768. try
  2769. {
  2770. request.setEntity(new UrlEncodedFormEntity(arguments));
  2771. HttpResponse response = client.execute(request);
  2772. checkResponseCode(response);
  2773. }
  2774. catch (Exception e)
  2775. {
  2776. //e.printStackTrace();
  2777. throw new SynBioHubException(e);
  2778. }
  2779. finally
  2780. {
  2781. request.releaseConnection();
  2782. }
  2783. }
  2784. /**
  2785. * Remove owner from an object in SynBioHub.
  2786. * @param topLevelUri identity of the object to remove an owner from
  2787. * @param userId user id of owner being removed
  2788. *
  2789. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2790. */
  2791. public void removeOwner(URI topLevelUri, String userId) throws SynBioHubException
  2792. {
  2793. if (user.equals("")) {
  2794. Exception e = new Exception("Must be logged in to remove an owner.");
  2795. throw new SynBioHubException(e);
  2796. }
  2797. String url = topLevelUri + "/removeOwner/"+userId;
  2798. url = url.replace(uriPrefix, backendUrl);
  2799. HttpPost request = new HttpPost(url);
  2800. request.setHeader("X-authorization", user);
  2801. request.setHeader("Accept", "text/plain");
  2802. List<NameValuePair> arguments = new ArrayList<>(4);
  2803. arguments.add(new BasicNameValuePair("userUri", uriPrefix+"/user/"+userId));
  2804. try
  2805. {
  2806. request.setEntity(new UrlEncodedFormEntity(arguments));
  2807. HttpResponse response = client.execute(request);
  2808. checkResponseCode(response);
  2809. }
  2810. catch (Exception e)
  2811. {
  2812. //e.printStackTrace();
  2813. throw new SynBioHubException(e);
  2814. }
  2815. finally
  2816. {
  2817. request.releaseConnection();
  2818. }
  2819. }
  2820. /**
  2821. * Move a private collection into an existing public collection.
  2822. * @param topLevelUri identity of the collection to make public
  2823. * @param collectionUri identity of the public collection
  2824. *
  2825. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2826. */
  2827. public void moveToPublicCollection(URI topLevelUri, URI collectionUri) throws SynBioHubException
  2828. {
  2829. makePublic(topLevelUri,collectionUri,"existing","","","","","");
  2830. }
  2831. /**
  2832. * Move a private collection into a new public collection.
  2833. * @param topLevelUri identity of the collection to make public
  2834. * @param id id for the new collection
  2835. * @param version version for the new collection
  2836. * @param name name for the new collection (optional: default is existing name)
  2837. * @param description description for the new collection (optional: default is existing description)
  2838. * @param citations comma-separated listed of PubMed ids (optional: default is existing citations)
  2839. *
  2840. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2841. */
  2842. public void moveToNewPublicCollection(URI topLevelUri, String id, String version,
  2843. String name, String description, String citations) throws SynBioHubException
  2844. {
  2845. makePublic(topLevelUri,null,"new",id,version,name,description,citations);
  2846. }
  2847. private void makePublic(URI topLevelUri, URI collectionUri, String tabState, String id,
  2848. String version, String name, String description, String citations) throws SynBioHubException
  2849. {
  2850. if (user.equals("")) {
  2851. Exception e = new Exception("Must be logged in to make a collection public.");
  2852. throw new SynBioHubException(e);
  2853. }
  2854. String url = topLevelUri + "/makePublic";
  2855. url = url.replace(uriPrefix, backendUrl);
  2856. HttpPost request = new HttpPost(url);
  2857. request.setHeader("X-authorization", user);
  2858. request.setHeader("Accept", "text/plain");
  2859. List<NameValuePair> arguments = new ArrayList<>(4);
  2860. arguments.add(new BasicNameValuePair("user", user));
  2861. arguments.add(new BasicNameValuePair("collections",
  2862. collectionUri!=null?collectionUri.toString():""));
  2863. arguments.add(new BasicNameValuePair("tabState", tabState));
  2864. arguments.add(new BasicNameValuePair("id", id));
  2865. arguments.add(new BasicNameValuePair("version", version));
  2866. arguments.add(new BasicNameValuePair("name", name));
  2867. arguments.add(new BasicNameValuePair("description", description));
  2868. arguments.add(new BasicNameValuePair("citations", citations));
  2869. try
  2870. {
  2871. request.setEntity(new UrlEncodedFormEntity(arguments));
  2872. HttpResponse response = client.execute(request);
  2873. checkResponseCode(response);
  2874. }
  2875. catch (Exception e)
  2876. {
  2877. //e.printStackTrace();
  2878. throw new SynBioHubException(e);
  2879. }
  2880. finally
  2881. {
  2882. request.releaseConnection();
  2883. }
  2884. }
  2885. /**
  2886. * Add SBOL document to an existing private collection on SynBioHub
  2887. * @param collectionUri Identity of the private collection
  2888. * @param overwrite if object exists in collection, overwrite it
  2889. * @param document the SBOL document to submit
  2890. *
  2891. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2892. */
  2893. public void addToCollection(URI collectionUri, boolean overwrite, SBOLDocument document) throws SynBioHubException
  2894. {
  2895. InputStream sbolDoc = new ByteArrayInputStream(serializeDocument(document).getBytes());
  2896. if (!collectionUri.toString().startsWith(uriPrefix)) {
  2897. throw new SynBioHubException("Collection URI does not start with correct URI prefix for this repository.");
  2898. }
  2899. submit(collectionUri, "", "", "", "", "", overwrite?"3":"2", sbolDoc);
  2900. }
  2901. /**
  2902. * Add file to an existing private collection on SynBioHub
  2903. * @param collectionUri Identity of the private collection
  2904. * @param overwrite if object exists in collection, overwrite it
  2905. * @param filename filename to submit to SynBioHub
  2906. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2907. * @throws IOException if there is an I/O error
  2908. */
  2909. public void addToCollection(URI collectionUri, boolean overwrite, String filename) throws SynBioHubException, IOException
  2910. {
  2911. if (!collectionUri.toString().startsWith(uriPrefix)) {
  2912. throw new SynBioHubException("Collection URI does not start with correct URI prefix for this repository.");
  2913. }
  2914. submit(collectionUri, "", "", "", "", "", overwrite?"3":"2", new FileInputStream(filename));
  2915. }
  2916. /**
  2917. * Add file to an existing private collection on SynBioHub
  2918. * @param collectionUri Identity of the private collection
  2919. * @param overwrite if object exists in collection, overwrite it
  2920. * @param file file to submit to SynBioHub
  2921. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2922. * @throws IOException if there is an I/O error
  2923. */
  2924. public void addToCollection(URI collectionUri, boolean overwrite, File file) throws SynBioHubException, IOException
  2925. {
  2926. if (!collectionUri.toString().startsWith(uriPrefix)) {
  2927. throw new SynBioHubException("Collection URI does not start with correct URI prefix for this repository.");
  2928. }
  2929. submit(collectionUri, "", "", "", "", "", overwrite?"3":"2", new FileInputStream(file));
  2930. }
  2931. /**
  2932. * Add file to an existing private collection on SynBioHub
  2933. * @param collectionUri Identity of the private collection
  2934. * @param overwrite if object exists in collection, overwrite it
  2935. * @param inputStream inputStream to submit to SynBioHub
  2936. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2937. */
  2938. public void addToCollection(URI collectionUri, boolean overwrite, InputStream inputStream) throws SynBioHubException
  2939. {
  2940. if (!collectionUri.toString().startsWith(uriPrefix)) {
  2941. throw new SynBioHubException("Collection URI does not start with correct URI prefix for this repository.");
  2942. }
  2943. submit(collectionUri,"","","","","",overwrite?"3":"2",inputStream);
  2944. }
  2945. /**
  2946. * Create a new private collection on SynBioHub
  2947. * @param id The submission identifier
  2948. * @param version The submission version
  2949. * @param name The submission name
  2950. * @param description The submission description
  2951. * @param citations The pubMedIds for this submission
  2952. * @param overwrite if collection exists, overwrite it
  2953. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2954. */
  2955. public void createCollection(String id, String version, String name, String description, String citations,
  2956. boolean overwrite) throws SynBioHubException
  2957. {
  2958. submit(null, id,version,name,description,citations,overwrite?"1":"0",(InputStream)null);
  2959. }
  2960. /**
  2961. * Create a new private collection on SynBioHub and add the contents of the
  2962. * SBOL document to this collection
  2963. * @param id The submission identifier
  2964. * @param version The submission version
  2965. * @param name The submission name
  2966. * @param description The submission description
  2967. * @param citations The pubMedIds for this submission
  2968. * @param overwrite if collection exists, overwrite it
  2969. * @param document the SBOL document to submit
  2970. *
  2971. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2972. */
  2973. public void createCollection(String id, String version, String name, String description, String citations,
  2974. boolean overwrite, SBOLDocument document) throws SynBioHubException
  2975. {
  2976. InputStream sbolDoc = new ByteArrayInputStream(serializeDocument(document).getBytes());
  2977. submit(null, id, version, name, description, citations, overwrite?"1":"0", sbolDoc);
  2978. }
  2979. /**
  2980. * Create a new private collection on SynBioHub and add the contents of the file to this collection
  2981. * @param id The submission identifier
  2982. * @param version The submission version
  2983. * @param name The submission name
  2984. * @param description The submission description
  2985. * @param citations The pubMedIds for this submission
  2986. * @param overwrite if collection exists, overwrite it
  2987. * @param filename filename to submit to SynBioHub
  2988. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  2989. * @throws IOException if there is an I/O error
  2990. */
  2991. public void createCollection(String id, String version, String name, String description, String citations,
  2992. boolean overwrite, String filename) throws SynBioHubException, IOException
  2993. {
  2994. if(filename != null){
  2995. submit(null, id, version, name, description, citations, overwrite?"1":"0", new FileInputStream(filename));
  2996. }
  2997. }
  2998. /**
  2999. * Create a new private collection on SynBioHub and add the contents of the file to this collection
  3000. * @param id The submission identifier
  3001. * @param version The submission version
  3002. * @param name The submission name
  3003. * @param description The submission description
  3004. * @param citations The pubMedIds for this submission
  3005. * @param overwrite if collection exists, overwrite it
  3006. * @param file file to submit to SynBioHub
  3007. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  3008. * @throws IOException if there is an I/O error
  3009. */
  3010. public void createCollection(String id, String version, String name, String description, String citations,
  3011. boolean overwrite, File file) throws SynBioHubException, IOException
  3012. {
  3013. if(file != null) {
  3014. submit(null, id, version, name, description, citations, overwrite?"1":"0", new FileInputStream(file));
  3015. }
  3016. }
  3017. /**
  3018. * Create a new private collection on SynBioHub and add the contents of the file to this collection
  3019. * @param id The submission identifier
  3020. * @param version The submission version
  3021. * @param name The submission name
  3022. * @param description The submission description
  3023. * @param citations The pubMedIds for this submission
  3024. * @param overwrite if collection exists, overwrite it
  3025. * @param inputStream inputStream to submit to SynBioHub
  3026. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  3027. */
  3028. public void createCollection(String id, String version, String name, String description, String citations,
  3029. boolean overwrite, InputStream inputStream) throws SynBioHubException
  3030. {
  3031. submit(null,id,version,name,description,citations,overwrite?"1":"0",inputStream);
  3032. }
  3033. /**
  3034. * Submit file to a new private collection on SynBioHub
  3035. * @param id The submission identifier
  3036. * @param version The submission version
  3037. * @param name The submission name
  3038. * @param description The submission description
  3039. * @param citations The pubMedIds for this submission
  3040. * @param overwrite_merge '0' prevent, '1' overwrite, '2' merge and prevent, '3' merge and overwrite
  3041. * @param inputStream inputStream to submit to SynBioHub
  3042. * @throws SynBioHubException if there was an error communicating with the SynBioHub
  3043. */
  3044. private void submit(URI uri, String id, String version, String name, String description, String citations,
  3045. String overwrite_merge, InputStream inputStream) throws SynBioHubException
  3046. {
  3047. if (user.equals(""))
  3048. {
  3049. Exception e = new Exception("Must be logged in to submit.");
  3050. throw new SynBioHubException(e);
  3051. }
  3052. String url = backendUrl + "/submit";
  3053. HttpPost request = new HttpPost(url);
  3054. request.setHeader("X-authorization", user);
  3055. request.setHeader("Accept", "text/plain");
  3056. MultipartEntityBuilder params = MultipartEntityBuilder.create();
  3057. params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  3058. if (uri==null) {
  3059. params.addTextBody("id", id);
  3060. params.addTextBody("version", version);
  3061. params.addTextBody("name", name);
  3062. params.addTextBody("description", description);
  3063. params.addTextBody("citations", citations);
  3064. params.addTextBody("collectionChoices", "");
  3065. } else {
  3066. params.addTextBody("rootCollections", uri.toString());
  3067. }
  3068. params.addTextBody("overwrite_merge", overwrite_merge);
  3069. params.addTextBody("user", user);
  3070. if (inputStream != null) {
  3071. params.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, "file");
  3072. } else {
  3073. params.addTextBody("file", "");
  3074. }
  3075. try
  3076. {
  3077. request.setEntity(params.build());
  3078. HttpResponse response = client.execute(request);
  3079. checkResponseCode(response);
  3080. }
  3081. catch (Exception e)
  3082. {
  3083. //e.printStackTrace();
  3084. throw new SynBioHubException(e);
  3085. }
  3086. finally
  3087. {
  3088. request.releaseConnection();
  3089. }
  3090. }
  3091. private String serializeDocument(SBOLDocument document) throws SynBioHubException
  3092. {
  3093. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  3094. try
  3095. {
  3096. SBOLWriter.write(document, outputStream);
  3097. return outputStream.toString("UTF-8");
  3098. }
  3099. catch(Exception e)
  3100. {
  3101. throw new SynBioHubException("Error serializing SBOL document", e);
  3102. }
  3103. }
  3104. private void removeFromSynBioHub(String url) throws SynBioHubException
  3105. {
  3106. HttpGet request = new HttpGet(url);
  3107. request.setHeader("X-authorization", user);
  3108. request.setHeader("Accept", "text/plain");
  3109. try
  3110. {
  3111. HttpResponse response = client.execute(request);
  3112. checkResponseCode(response);
  3113. HttpStream res = new HttpStream();
  3114. res.inputStream = response.getEntity().getContent();
  3115. res.request = request;
  3116. }
  3117. catch(Exception e)
  3118. {
  3119. request.releaseConnection();
  3120. throw new SynBioHubException("Error connecting to SynBioHub endpoint", e);
  3121. }
  3122. }
  3123. private SBOLDocument fetchFromSynBioHub(String url) throws SynBioHubException
  3124. {
  3125. HttpStream stream;
  3126. try
  3127. {
  3128. stream = fetchContentAsInputStream(url);
  3129. }
  3130. catch (IOException e)
  3131. {
  3132. throw new SynBioHubException("Error connecting to SynBioHub endpoint", e);
  3133. }
  3134. SBOLDocument document;
  3135. try
  3136. {
  3137. document = SBOLReader.read(stream.inputStream);
  3138. }
  3139. catch (Exception e)
  3140. {
  3141. throw new SynBioHubException("Error reading SBOL", e);
  3142. }
  3143. finally
  3144. {
  3145. stream.request.releaseConnection();
  3146. }
  3147. //TopLevel topLevel = document.getTopLevel(topLevelUri);
  3148. //if(topLevel == null)
  3149. //{
  3150. // throw new SynBioHubException("Matching top-level not found in response");
  3151. //}
  3152. return document;
  3153. }
  3154. private int fetchCount(String url) throws SynBioHubException
  3155. {
  3156. try
  3157. {
  3158. return Integer.parseInt(fetchContentAsString(url));
  3159. }
  3160. catch(Exception e)
  3161. {
  3162. throw new SynBioHubException(e);
  3163. }
  3164. }
  3165. private String fetchContentSaveToFile(String url,OutputStream outputStream,String path) throws SynBioHubException, IOException
  3166. {
  3167. HttpGet request = new HttpGet(url);
  3168. request.setHeader("X-authorization", user);
  3169. request.setHeader("Accept", "text/plain");
  3170. try
  3171. {
  3172. HttpResponse response = client.execute(request);
  3173. checkResponseCode(response);
  3174. String filename = "default";
  3175. if (response.getFirstHeader("Content-Disposition")!=null) {
  3176. String dispositionValue = response.getFirstHeader("Content-Disposition").getValue();
  3177. int index = dispositionValue.indexOf("filename=");
  3178. if (index > 0) {
  3179. filename = dispositionValue.substring(index + 10, dispositionValue.length() - 1);
  3180. }
  3181. }
  3182. if (outputStream==null) {
  3183. outputStream = new FileOutputStream(path+filename);
  3184. }
  3185. HttpEntity entity = response.getEntity();
  3186. if (entity != null) {
  3187. entity.writeTo(outputStream);
  3188. }
  3189. return filename;
  3190. }
  3191. catch(SynBioHubException e)
  3192. {
  3193. request.releaseConnection();
  3194. throw e;
  3195. }
  3196. catch(IOException e)
  3197. {
  3198. request.releaseConnection();
  3199. throw e;
  3200. }
  3201. }
  3202. private String fetchContentAsString(String url) throws SynBioHubException, IOException
  3203. {
  3204. HttpStream stream = fetchContentAsInputStream(url);
  3205. String str;
  3206. try
  3207. {
  3208. str = inputStreamToString(stream.inputStream);
  3209. }
  3210. finally
  3211. {
  3212. stream.request.releaseConnection();
  3213. }
  3214. return str;
  3215. }
  3216. private static String inputStreamToString(InputStream inputStream) throws IOException
  3217. {
  3218. StringWriter writer = new StringWriter();
  3219. IOUtils.copy(inputStream, writer);
  3220. return writer.toString();
  3221. }
  3222. class HttpStream
  3223. {
  3224. public InputStream inputStream;
  3225. public HttpRequestBase request;
  3226. }
  3227. private HttpStream fetchContentAsInputStream(String url) throws SynBioHubException, IOException
  3228. {
  3229. HttpGet request = new HttpGet(url);
  3230. request.setHeader("X-authorization", user);
  3231. request.setHeader("Accept", "text/plain");
  3232. try
  3233. {
  3234. HttpResponse response = client.execute(request);
  3235. checkResponseCode(response);
  3236. HttpStream res = new HttpStream();
  3237. res.inputStream = response.getEntity().getContent();
  3238. res.request = request;
  3239. return res;
  3240. }
  3241. catch(Exception e)
  3242. {
  3243. request.releaseConnection();
  3244. throw e;
  3245. }
  3246. }
  3247. private String encodeUri(String uri)
  3248. {
  3249. try
  3250. {
  3251. return URLEncoder.encode(uri, "UTF-8").replace("+", "%20");
  3252. }
  3253. catch (UnsupportedEncodingException e)
  3254. {
  3255. throw new RuntimeException("UTF-8 not supported?");
  3256. }
  3257. }
  3258. private static void checkResponseCode(HttpResponse response) throws SynBioHubException
  3259. {
  3260. int statusCode = response.getStatusLine().getStatusCode();
  3261. if(statusCode >= 300)
  3262. {
  3263. switch(statusCode)
  3264. {
  3265. case 401:
  3266. throw new PermissionException();
  3267. case 404:
  3268. throw new NotFoundException();
  3269. default:
  3270. HttpEntity entity = response.getEntity();
  3271. try {
  3272. throw new SynBioHubException(statusCode+" "+inputStreamToString(entity.getContent()));
  3273. }
  3274. catch (UnsupportedOperationException | IOException e) {
  3275. throw new SynBioHubException(statusCode+"");
  3276. }
  3277. }
  3278. }
  3279. }
  3280. /**
  3281. * @return the user
  3282. */
  3283. public String getUser() {
  3284. return user;
  3285. }
  3286. /**
  3287. * @param user the user to set
  3288. */
  3289. public void setUser(String user) {
  3290. this.user = user;
  3291. }
  3292. }