/core2/src/main/java/org/synbiohub/frontend/SynBioHubFrontend.java
Java | 3733 lines | 2213 code | 441 blank | 1079 comment | 130 complexity | cbd916f2495ee2d5415914759869196c MD5 | raw file
Possible License(s): Apache-2.0
Large files files are truncated, but you can click here to view the full file
- package org.synbiohub.frontend;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.StringWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.URI;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import org.apache.commons.io.IOUtils;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.methods.HttpRequestBase;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.mime.HttpMultipartMode;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.sbolstandard.core2.SBOLDocument;
- import org.sbolstandard.core2.SBOLReader;
- import org.sbolstandard.core2.SBOLValidationException;
- import org.sbolstandard.core2.SBOLWriter;
- import org.sbolstandard.core2.TopLevel;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- /**
- * Provides a Java API to SynBioHub instances.
- * @author James McLaughlin
- * @author Chris Myers
- *
- */
- public class SynBioHubFrontend
- {
- PoolingHttpClientConnectionManager connectionManager;
- HttpClient client;
- String backendUrl;
- String uriPrefix;
- String user = "";
- String username = null;
- /**
- * Creates an instance of the SynBioHub API.
- * @param backendUrl - URL for the SynBioHub instance.
- * @param uriPrefix - prefix for all URIs stored in this repository
- */
- public SynBioHubFrontend(String backendUrl, String uriPrefix)
- {
- this.backendUrl = backendUrl;
- this.uriPrefix = uriPrefix;
- connectionManager = new PoolingHttpClientConnectionManager();
- client = HttpClients.custom().setConnectionManager(connectionManager).build();
- }
- /**
- * Creates an instance of the SynBioHub API.
- * @param backendUrl - URL for the SynBioHub instance.
- */
- public SynBioHubFrontend(String backendUrl)
- {
- this.backendUrl = backendUrl;
- this.uriPrefix = backendUrl;
- connectionManager = new PoolingHttpClientConnectionManager();
- client = HttpClients.custom().setConnectionManager(connectionManager).build();
- }
-
- /**
- * Creates an instance of the SynBioHub API.
- * @param backendUrl - URL for the SynBioHub instance.
- * @param timeout - timeout for connections in seconds
- */
- public SynBioHubFrontend(String backendUrl,int timeout)
- {
- this.backendUrl = backendUrl;
- this.uriPrefix = backendUrl;
- connectionManager = new PoolingHttpClientConnectionManager();
- // client = HttpClients.custom().setConnectionManager(connectionManager).build();
- RequestConfig config = RequestConfig.custom()
- .setConnectTimeout(timeout * 1000)
- .setConnectionRequestTimeout(timeout * 1000)
- .setSocketTimeout(timeout * 1000).build();
- client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
- }
- /**
- * Creates an instance of the SynBioHub API.
- * @param backendUrl - URL for the SynBioHub instance.
- * @param uriPrefix - prefix for all URIs stored in this repository
- * @param timeout - timeout for connections in seconds
- */
- public SynBioHubFrontend(String backendUrl, String uriPrefix, int timeout)
- {
- this.backendUrl = backendUrl;
- this.uriPrefix = uriPrefix;
- connectionManager = new PoolingHttpClientConnectionManager();
- RequestConfig config = RequestConfig.custom()
- .setConnectTimeout(timeout * 1000)
- .setConnectionRequestTimeout(timeout * 1000)
- .setSocketTimeout(timeout * 1000).build();
- client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
- }
- /**
- * Returns the URL for the SynBioHub instance.
- * @return the URL for the SynBioHub instance.
- */
- public String getBackendUrl()
- {
- return this.backendUrl;
- }
- /**
- * Return the total number of objects of a specified type in the repository.
- *
- * @return the total number of objects of a specified type in the repository.
- *
- * @param objectType The object type to count
- * (Collection, ComponentDefinition, Sequence, ModuleDefinition, Model, etc.).
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public int getCount(String objectType) throws SynBioHubException
- {
- return fetchCount(backendUrl + "/" + objectType + "/count");
- }
-
- /**
- * Retrieve SBOL TopLevel object from a SynBioHub instance using its URI.
- *
- * @param topLevelUri The URI of the SBOL TopLevel
- *
- * @return A libSBOLj TopLevel instance corresponding to the TopLevel
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public SBOLDocument getSBOL(URI topLevelUri) throws SynBioHubException
- {
- return getSBOL(topLevelUri,true);
- }
-
- /**
- * Retrieve SBOL TopLevel object from a SynBioHub instance using its URI.
- *
- * @param topLevelUri The URI of the SBOL TopLevel
- * @param recursive indicates if the complete SBOL document should be fetched recursively
- *
- * @return A libSBOLj TopLevel instance corresponding to the TopLevel
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public SBOLDocument getSBOL(URI topLevelUri,boolean recursive) throws SynBioHubException
- {
- if (topLevelUri==null) return null;
- if (!topLevelUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelUri + "/sbol";
- if (!recursive) {
- url = topLevelUri + "/sbolnr";
- }
- url = url.replace(uriPrefix, backendUrl);
- try {
- SBOLDocument document = fetchFromSynBioHub(url);
- return document;
- } catch (NotFoundException e) {
- return null;
- }
- }
-
- /**
- * Retrieve a GFF3 version of a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getGFF3(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/gff";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve a GFF3 version of a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getGFF3(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/gff";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getGenBank(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/gb";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getGenBank(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/gb";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve a FASTA version of a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getFASTA(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/fasta";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve a GenBank version of a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getFASTA(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/fasta";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve a JSON version of a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getJSON(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/summary";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve a JSON version of a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getJSON(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/summary";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve metadata for a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getMetadata(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/metadata";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve metadata for a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getMetadata(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/metadata";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve a COMBINE Archive version of a topLevel object from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getArchive(URI topLevelURI, String path) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/omex";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve a COMBINE Archive version of a topLevel object from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param topLevelURI The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getArchive(URI topLevelURI, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!topLevelURI.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelURI + "/omex";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Retrieve an attachment from a SynBioHub instance using its URI,
- * and save to the path provided.
- *
- * @param attachmentUri The URI of the SBOL Attachment object
- * @param path The path to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getAttachment(URI attachmentUri, String path) throws SynBioHubException, IOException
- {
- if (!attachmentUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = attachmentUri + "/download";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,null,path);
- }
-
- /**
- * Retrieve an attachment from a SynBioHub instance using its URI,
- * and save into the provided output stream.
- *
- * @param attachmentUri The URI of the SBOL Attachment object
- * @param outputStream The output stream to store the downloaded attachment
- * @return the name of the file being downloaded
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- * @throws IOException if there is an I/O error
- */
- public String getAttachment(URI attachmentUri, OutputStream outputStream) throws SynBioHubException, IOException
- {
- if (!attachmentUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = attachmentUri + "/download";
- url = url.replace(uriPrefix, backendUrl);
- return fetchContentSaveToFile(url,outputStream,null);
- }
-
- /**
- * Remove SBOL Collection from a SynBioHub instance using its URI.
- *
- * @param topLevelUri The URI of the SBOL Collection
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void removeCollection(URI topLevelUri) throws SynBioHubException
- {
- if (!topLevelUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelUri + "/removeCollection";
- url = url.replace(uriPrefix, backendUrl);
- removeFromSynBioHub(url);
- }
-
- /**
- * Remove SBOL TopLevel object from a SynBioHub instance using its URI.
- *
- * @param topLevelUri The URI of the SBOL TopLevel
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void removeSBOL(URI topLevelUri) throws SynBioHubException
- {
- if (!topLevelUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelUri + "/remove";
- url = url.replace(uriPrefix, backendUrl);
- removeFromSynBioHub(url);
- }
-
- /**
- * Remove SBOL TopLevel object from a SynBioHub instance using its URI,
- * but leave references to this object, since it is going to be replaced.
- *
- * @param topLevelUri The URI of the SBOL TopLevel
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void replaceSBOL(URI topLevelUri) throws SynBioHubException
- {
- if (!topLevelUri.toString().startsWith(uriPrefix)) {
- throw new SynBioHubException("Object URI does not start with correct URI prefix for this repository.");
- }
- String url = topLevelUri + "/replace";
- url = url.replace(uriPrefix, backendUrl);
- removeFromSynBioHub(url);
- }
- /**
- * Search the default store for ComponentDefinition instances matching a name and/or a set of roles
- *
- * @param name The dcterms:title to search for, or null
- * @param roles A set of role URIs to search for, or null
- * @param types A set of type URIs to search for, or null
- * @param collections A set of Collection URIs to search for, or null
- * @param offset The offset of the results to begin at, or null to begin at 0
- * @param limit The maximum number of results to return, or null to return all results
- *
- * @return An ArrayList of ComponentDefinitionMetaData objects with a summary of all matching ComponentDefinitions.
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public ArrayList<IdentifiedMetadata> getMatchingComponentDefinitionMetadata(String name, Set<URI> roles,
- Set<URI> types, Set<URI> collections, Integer offset, Integer limit)
- throws SynBioHubException
- {
- SearchQuery query = new SearchQuery();
- query.setOffset(offset);
- query.setLimit(limit);
- SearchCriteria objectCriteria = new SearchCriteria();
- objectCriteria.setKey("objectType");
- objectCriteria.setValue("ComponentDefinition");
- query.addCriteria(objectCriteria);
- if (roles != null) {
- for(URI uri : roles)
- {
- SearchCriteria roleCriteria = new SearchCriteria();
- roleCriteria.setKey("role");
- roleCriteria.setValue(uri.toString());
- query.getCriteria().add(roleCriteria);
- }
- }
-
- if (types != null) {
- for(URI uri : types)
- {
- SearchCriteria typeCriteria = new SearchCriteria();
- typeCriteria.setKey("type");
- typeCriteria.setValue(uri.toString());
- query.getCriteria().add(typeCriteria);
- }
- }
-
- if (collections != null) {
- for(URI uri : collections)
- {
- SearchCriteria collectionCriteria = new SearchCriteria();
- collectionCriteria.setKey("collection");
- collectionCriteria.setValue(uri.toString());
- query.getCriteria().add(collectionCriteria);
- }
- }
- if(name != null)
- {
- SearchCriteria nameCriteria = new SearchCriteria();
- nameCriteria.setKey("name");
- nameCriteria.setValue(name);
- query.getCriteria().add(nameCriteria);
- }
- return search(query);
- }
-
- private String constructQueryURL(String url,SearchQuery query) {
- //query.offset = offset;
- //query.limit = limit;
- String textQuery = "";
- boolean first = true;
- for (SearchCriteria criteria : query.getCriteria()) {
- if (criteria.getKey().equals("objectType")) {
- url += encodeUri(criteria.getKey()+"="+criteria.getValue()+"&");
- continue;
- }
- if (criteria.getKey().equals("name")) {
- if (first) first = false;
- else textQuery = " ";
- textQuery = criteria.getValue();
- continue;
- }
- if (criteria.getKey().startsWith("http")) {
- url += encodeUri("<" + criteria.getKey() + ">=");
- } else {
- url += encodeUri(criteria.getKey()+"=");
- }
- if (criteria.getValue().startsWith("http")) {
- url += encodeUri("<"+criteria.getValue()+">&");
- } else {
- url += encodeUri("'"+criteria.getValue()+"'&");
- }
- }
- url += encodeUri(textQuery);
- if (query.getOffset()!=null && query.getLimit()!=null) {
- url += "/?offset="+query.getOffset() + "&" + "limit="+query.getLimit();
- } else if (query.getOffset()!=null) {
- url += "/?offset="+query.getOffset();
- } else if (query.getLimit()!=null) {
- url += "/?limit="+query.getLimit();
- }
-
- return url;
- }
-
- /**
- * Search this SynBioHub instance for objects matching a search query
- *
- * @param query the search query
- *
- * @return An ArrayList of MetaData for objects that match the specified search query
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public ArrayList<IdentifiedMetadata> search(SearchQuery query) throws SynBioHubException
- {
- String url = backendUrl + "/search/";
- //query.offset = offset;
- //query.limit = limit;
- url = constructQueryURL(url,query);
-
- //System.out.println(url);
- Gson gson = new Gson();
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
- new InputStreamReader(inputStream),
- new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
-
- return metadataList;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Search this SynBioHub instance for objects matching a search query
- * and return the number of matches
- *
- * @param query the search query
- *
- * @return the number of objects matching a search query
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public int searchCount(SearchQuery query) throws SynBioHubException
- {
- String url = backendUrl + "/searchCount/";
- //query.offset = offset;
- //query.limit = limit;
- url = constructQueryURL(url,query);
- return fetchCount(url);
- }
-
- /**
- * Search the default store for Collections that are not members of any other Collections
- *
- * @return An ArrayList of CollectionMetaData objects with a summary of all matching Collections.
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public ArrayList<IdentifiedMetadata> getRootCollectionMetadata()
- throws SynBioHubException
- {
- String url = backendUrl + "/rootCollections";
- Gson gson = new Gson();
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
- new InputStreamReader(inputStream),
- new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
-
- return metadataList;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Search the default store for submissions from the specified user
- *
- * @return An ArrayList of MetaData for all submissions for the specified user.
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public ArrayList<IdentifiedMetadata> getSubmissionsMetadata()
- throws SynBioHubException
- {
- String url = backendUrl + "/manage";
- Gson gson = new Gson();
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
- new InputStreamReader(inputStream),
- new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
-
- return metadataList;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Search the default store for objects that are shared with the specified user
- *
- * @return An ArrayList of MetaData for all shared objects.
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public ArrayList<IdentifiedMetadata> getSharedMetadata()
- throws SynBioHubException
- {
- String url = backendUrl + "/shared";
- Gson gson = new Gson();
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- ArrayList<IdentifiedMetadata> metadataList = gson.fromJson(
- new InputStreamReader(inputStream),
- new TypeToken<ArrayList<IdentifiedMetadata>>(){}.getType());
-
- return metadataList;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get configuration of a SynBioHub
- *
- * @return A JSON object of the configuration of a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONObject getConfig() throws SynBioHubException
- {
- return getConfig("");
- }
-
- /**
- * Get graphs in a SynBioHub
- *
- * @return A JSON array of the graphs in a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONArray getGraphs() throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to get the graphs.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/graphs";
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- JSONParser parser = new JSONParser();
- JSONArray config = (JSONArray) parser.parse(inputStreamToString(inputStream));
-
- return config;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get graphs in a SynBioHub
- *
- * @return A JSONObject of the graphs in a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONArray getLogs() throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to get the logs.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/log";
- HttpGet request = new HttpGet(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- InputStream inputStream = response.getEntity().getContent();
- JSONParser parser = new JSONParser();
- JSONArray config = (JSONArray) parser.parse(inputStreamToString(inputStream));
-
- return config;
- }
- catch (Exception e)
- {
- throw new SynBioHubException(e);
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get mail configuration of a SynBioHub
- *
- * @return A JSON object of the mail configuration of a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONObject getMailConfig() throws SynBioHubException
- {
- return getConfig("mail");
- }
-
- /**
- * Set the mail configuration for a SynBioHub
- *
- * @param key SendGrid API Key
- * @param fromEmail SendGrid from Email
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void setMailConfig(String key, String fromEmail) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to set the mail configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/mail";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("key", key));
- arguments.add(new BasicNameValuePair("fromEmail", fromEmail));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get plugin configuration for a SynBioHub
- *
- * @return A JSON object of the plugin configuration for a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONObject getPluginsConfig() throws SynBioHubException
- {
- return getConfig("plugins");
- }
-
- /**
- * Edit a plugin configuration in a SynBioHub
- *
- * @param id Id of plugin
- * @param category Type of plugin (rendering, submit, download)
- * @param name Name of the plugin
- * @param pluginURL URL for the plugin
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void editPluginConfig(int id, String category, String name, String pluginURL) throws SynBioHubException
- {
- id = id + 1;
- savePluginConfig(Integer.toString(id), category, name, pluginURL);
- }
-
- /**
- * Add a plugin configuration to a SynBioHub
- *
- * @param category Type of plugin (rendering, submit, download)
- * @param name Name of the plugin
- * @param pluginURL URL for the plugin
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void addPluginConfig(String category, String name, String pluginURL) throws SynBioHubException
- {
- savePluginConfig("New", category, name, pluginURL);
- }
-
- /**
- * Save a plugin configuration to a SynBioHub
- *
- * @param id Id of plugin, if adding a plugin, id should be New
- * @param category Type of plugin (rendering, submit, download)
- * @param name Name of the plugin
- * @param pluginURL URL for the plugin
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- private void savePluginConfig(String id, String category, String name, String pluginURL) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to add a plugin configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/savePlugin";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("id", id));
- arguments.add(new BasicNameValuePair("category", category));
- arguments.add(new BasicNameValuePair("name", name));
- arguments.add(new BasicNameValuePair("url", pluginURL));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
- /**
- * Delete a plugin configuration to a SynBioHub
- *
- * @param id Id of plugin
- * @param category Type of plugin (rendering, submit, download)
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void deletePluginConfig(int id, String category) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to delete a plugin configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/deletePlugin";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- id = id + 1;
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("id", Integer.toString(id)));
- arguments.add(new BasicNameValuePair("category", category));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get registries configuration for a SynBioHub
- *
- * @return A JSON object of the registries configuration for a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONObject getRegistriesConfig() throws SynBioHubException
- {
- return getConfig("registries");
- }
-
- /**
- * Save a registry configuration for a SynBioHub
- *
- * @param uriPrefix URI prefix for the registry
- * @param registryURL URL for the registry
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void saveRegistryConfig(String uriPrefix, String registryURL) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to save a registry configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/saveRegistry";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("uri", uriPrefix));
- arguments.add(new BasicNameValuePair("url", registryURL));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
- /**
- * Delete a registry configuration in a SynBioHub
- *
- * @param uriPrefix URI prefix of the registry to delete
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void deleteRegistryConfig(String uriPrefix) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to remove a registry configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/deleteRegistry";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("uri", uriPrefix));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Update the administrator email configuration for a SynBioHub
- *
- * @param administratorEmail Administrator email address
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void updateAdministratorEmailConfig(String administratorEmail) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to update the administrator email configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/setAdministratorEmail";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("administratorEmail", administratorEmail));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Retrieve update from Web-of-Registries for a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void retreiveUpdateFromWebOfRegistries() throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to retrieve update from Web-of-Registries.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/retrieveFromWebOfRegistries";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- try
- {
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Send request to join Web-of-Registries for a SynBioHub
- *
- * @param administratorEmail Administrator email address
- * @param webOfRegistries URL for the Web-of-Registries
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void requestToJoinWebOfRegistries(String administratorEmail, String webOfRegistries) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to send request to join Web-of-Registries.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/federate";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Accept", "text/plain");
-
- List<NameValuePair> arguments = new ArrayList<>(4);
- arguments.add(new BasicNameValuePair("administratorEmail", administratorEmail));
- arguments.add(new BasicNameValuePair("webOfRegistries", webOfRegistries));
-
- try
- {
- request.setEntity(new UrlEncodedFormEntity(arguments));
- HttpResponse response = client.execute(request);
- checkResponseCode(response);
- }
- catch (Exception e)
- {
- //e.printStackTrace();
- throw new SynBioHubException(e);
-
- }
- finally
- {
- request.releaseConnection();
- }
- }
-
- /**
- * Get remotes configuration for a SynBioHub
- *
- * @return A JSON object of the remotes configuration for a SynBioHub
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public JSONObject getRemotesConfig() throws SynBioHubException
- {
- return getConfig("remotes");
- }
-
- /**
- * Save a Benchling remote configuration for a SynBioHub
- *
- * @param id Id of the Benchling remote
- * @param remoteURL URL for the Benchling remote
- * @param benchlingApiToken API token for the Benchling remote
- * @param rejectUnauthorized Check SSL certificate?
- * @param folderPrefix Prefix to use for folders on Benchling
- * @param sequenceSuffix Suffix to use for sequences found on Benchling
- * @param defaultFolderId Default folder on Benchling to access
- * @param isPublic Should the remote be visible publicly?
- * @param rootCollectionDisplayId Display id for the root collection on the remote
- * @param rootCollectionName Name for the root collection on the remote
- * @param rootCollectionDescription Description for the root collection on the remote
- *
- * @throws SynBioHubException if there was an error communicating with the SynBioHub
- */
- public void saveBenchlingRemoteConfig(String id, String remoteURL, String benchlingApiToken,
- boolean rejectUnauthorized, String folderPrefix, String sequenceSuffix, String defaultFolderId,
- boolean isPublic, String rootCollectionDisplayId, String rootCollectionName,
- String rootCollectionDescription) throws SynBioHubException
- {
- if (user.equals("")) {
- Exception e = new Exception("Must be logged in to save a Benchling remote configuration.");
- throw new SynBioHubException(e);
- }
- String url = backendUrl + "/admin/saveRemote";
-
- HttpPost request = new HttpPost(url);
- request.setHeader("X-authorization", user);
- request.setHeader("Ac…
Large files files are truncated, but you can click here to view the full file