Commit 4880b4be authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'drop-obsolete-code' into 'master'

Drop obsolete code for /api/v0

See merge request !16
parents fe5db68a c20b7a71
Pipeline #17854 passed with stage
in 49 seconds
......@@ -16,32 +16,20 @@
package org.genesys2.client.oauth;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.exceptions.OAuthException;
import com.github.scribejava.core.model.OAuth2AccessToken;
......@@ -50,11 +38,9 @@ import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.genesys2.client.oauth.api.GenesysApi;
import org.genesys2.client.oauth.api.accession.AccessionJson;
import org.genesys2.client.oauth.api.images.RepositoryImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -228,8 +214,8 @@ public class GenesysClient {
final OAuthRequest request = new OAuthRequest(method, getApiUrl(url));
if (queryString != null && queryString.size() > 0) {
for (final String key : queryString.keySet()) {
request.addQuerystringParameter(key, queryString.get(key));
for (var entry : queryString.entrySet()) {
request.addQuerystringParameter(entry.getKey(), entry.getValue());
}
}
......@@ -244,7 +230,7 @@ public class GenesysClient {
if (postBody != null) {
// System.err.println("Adding data: " + data);
if (LOG.isTraceEnabled()) {
LOG.trace("Body: {}", new String(postBody));
LOG.trace("Body: {}", new String(postBody, StandardCharsets.UTF_8));
}
request.setPayload(postBody);
}
......@@ -278,7 +264,7 @@ public class GenesysClient {
}
} else {
LOG.error("{} {}", method, request.getCompleteUrl());
LOG.error(new String(postBody));
LOG.error(new String(postBody, StandardCharsets.UTF_8));
LOG.error("HTTP response code: {}", response.getCode());
LOG.error("Response: {}", responseBody);
if (response.getCode() == 504 || responseBody.contains("Deadlock found when trying to get lock; try restarting transaction")
......@@ -331,44 +317,6 @@ public class GenesysClient {
}
}
/**
* Accession exists.
*
* @param instituteCode the inst code
* @param accessionNumber the acce numb
* @param genus the genus
* @return the string
* @throws GenesysApiException the Genesys API exception
*/
public String accessionExists(final String instituteCode, final String accessionNumber, final String genus) throws GenesysApiException {
try {
final HashMap<String, String> queryString = new HashMap<String, String>();
queryString.put("accessionNumber", accessionNumber);
return query(Verb.GET, new URI(null, null, "/acn/exists/" + instituteCode + "/" + genus, null).toString(), queryString, null);
} catch (final URISyntaxException e) {
e.printStackTrace();
return null;
}
}
/**
* Make aid3.
*
* @param instituteCode the inst code
* @param genus the genus
* @param accessionNumber the acce numb
* @return the object node
*/
public static ObjectNode makeAid3(final String instituteCode, final String genus, final String accessionNumber) {
final ObjectNode json = objectMapper.createObjectNode();
json.put("instituteCode", instituteCode);
json.put("accessionNumber", accessionNumber);
json.put("genus", genus);
return json;
}
/**
* Update accessions.
*
......@@ -515,537 +463,4 @@ public class GenesysClient {
return query("/me/profile");
}
/**
* Gets the crop.
*
* @param shortName the short name
* @return the crop
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getCrop(final String shortName) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
if (!shortName.matches("^\\w+$")) {
throw new GenesysApiException("Crop shortname can only contain characters");
}
return query("/crops/" + shortName);
}
/**
* List parameters.
*
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listParameters() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/parameter/list");
}
/**
* Put parameter.
*
* @param node the node
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putParameter(final ObjectNode node) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/kpi/parameter", null, node.toString());
}
/**
* Gets the parameter.
*
* @param name the name
* @return the parameter
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getParameter(final String name) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/parameter/" + name);
}
/**
* List dimensions.
*
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listDimensions() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/dimension/list");
}
/**
* Gets the dimension.
*
* @param id the id
* @return the dimension
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getDimension(final long id) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/dimension/" + id);
}
/**
* Put dimension.
*
* @param node the node
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putDimension(final ObjectNode node) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/kpi/dimension", null, node.toString());
}
/**
* List executions.
*
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listExecutions() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/execution/list");
}
/**
* Gets the execution.
*
* @param name the name
* @return the execution
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getExecution(final String name) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/kpi/execution/" + name);
}
/**
* Put execution.
*
* @param node the node
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putExecution(final ObjectNode node) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/kpi/execution", null, node.toString());
}
/**
* Kpi execute.
*
* @param name the name
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String kpiExecute(final String name) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/kpi/execution/" + name + "/execute", null, null);
}
/**
* Delete dimension.
*
* @param id the id
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String deleteDimension(final long id) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.DELETE, "/kpi/dimension/" + id, null, null);
}
/**
* Delete execution.
*
* @param name the name
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String deleteExecution(final String name) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.DELETE, "/kpi/execution/" + name, null, null);
}
/**
* Delete parameter.
*
* @param name the name
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String deleteParameter(final String name) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.DELETE, "/kpi/parameter/" + name, null, null);
}
/**
* List crops.
*
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listCrops() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/crops");
}
/**
* Put crop.
*
* @param node the node
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putCrop(final ObjectNode node) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/crops", null, node.toString());
}
/**
* Delete crop.
*
* @param shortName the short name
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String deleteCrop(final String shortName) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.DELETE, "/crops/" + shortName, null, null);
}
/**
* Gets the crop rules.
*
* @param shortName the short name
* @return the crop rules
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getCropRules(final String shortName) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/crops/" + shortName + "/rules");
}
/**
* Put crop rules.
*
* @param shortName the short name
* @param currentCropRules the current crop rules
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putCropRules(final String shortName, final ArrayNode currentCropRules) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.PUT, "/crops/" + shortName + "/rules", null, currentCropRules.toString());
}
/**
* Rebuild crop taxa.
*
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String rebuildCropTaxa() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/crops/rebuild", null, null);
}
/**
* Rebuild crop taxa.
*
* @param shortName the short name
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String rebuildCropTaxa(final String shortName) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/crops/" + shortName + "/rebuild", null, null);
}
/**
* List organizations.
*
* @param page the page
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listOrganizations(final int page) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
final Map<String, String> qs = new HashMap<String, String>();
qs.put("page", String.valueOf(page));
return query(Verb.GET, "/org", qs, null);
}
/**
* Gets the organization.
*
* @param slug the slug
* @return the organization
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getOrganization(final String slug) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/org/" + slug);
}
/**
* Update organization.
*
* @param org the org
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String updateOrganization(final ObjectNode org) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.POST, "/org", null, org.toString());
}
/**
* Delete organization.
*
* @param slug the slug
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String deleteOrganization(final String slug) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.DELETE, "/org/" + slug, null, null);
}
/**
* Gets the organization members.
*
* @param slug the slug
* @return the organization members
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getOrganizationMembers(final String slug) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/org/" + slug + "/institutes");
}
/**
* Put organization members.
*
* @param slug the slug
* @param currentMembers the current members
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String putOrganizationMembers(final String slug, final ArrayNode currentMembers) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.PUT, "/org/" + slug + "/set-institutes", null, currentMembers.toString());
}
/**
* Gets the organization blurp.
*
* @param slug the slug
* @param language the language
* @return the organization blurp
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String getOrganizationBlurp(final String slug, final String language) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query("/org/" + slug + "/blurp/" + language);
}
/**
* Update organization blurp.
*
* @param slug the slug
* @param blurp the blurp
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String updateOrganizationBlurp(final String slug, final ObjectNode blurp) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
return query(Verb.PUT, "/org/" + slug + "/blurp", null, blurp.toString());
}
/**
* List observations.
*
* @param executionName the execution name
* @param dimensionFilter the dimension filter
* @param page the page
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listObservations(final String executionName, final String dimensionFilter, final int page)
throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
final Map<String, String> qs = new HashMap<String, String>();
qs.put("page", String.valueOf(page));
return query(Verb.POST, "/kpi/observation/" + executionName + "/", qs, StringUtils.defaultIfBlank(dimensionFilter, ""));
}
/**
* List accessions.
*
* @param instituteCode the inst code
* @param page the page
* @param query the query
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
*/
public String listAccessions(final String instituteCode, final int page, final String query) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
final Map<String, String> params = new HashMap<String, String>();
params.put("page", String.valueOf(page));
params.put("query", query);
return query(Verb.GET, "/acn/" + instituteCode + "/list", params, null);
}
/**
* List existing image galleries for INSTCODE. The response is paginated, provide <code>page</code>
* argument to request a specific page.
*
* @param instituteCode institute code (MCPD INSTCODE)
* @param page 1 for first page
* @return the string
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws HttpRedirectException the http redirect exception
* @throws GenesysApiException the Genesys API exception
*/
public String listGalleries(final String instituteCode, final int page) throws OAuthAuthenticationException, PleaseRetryException, HttpRedirectException, GenesysApiException {
return query(Verb.GET, String.format("/img/%1$s/_galleries", instituteCode), Collections.singletonMap("page", Integer.toString(page)), null);
}
/**
* List UUIDs of images in an existing accession gallery.
*
* @param instituteCode institute code (MCPD INSTCODE)
* @param accessionNumber the acce numb
* @return the list
* @throws OAuthAuthenticationException authentication exception
* @throws PleaseRetryException exception indicating the call shold be re-attempted
* @throws GenesysApiException the Genesys API exception
* @throws JsonParseException the json parse exception
* @throws JsonMappingException the json mapping exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public List<UUID> listGalleryImages(final String instituteCode, final String accessionNumber)
throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonParseException, JsonMappingException, IOException {
final String json = query(Verb.GET, String.format("/img/%1$s/acn/%2$s", instituteCode));
return objectMapper.readValue(json, new TypeReference<List<UUID>>() {
});
}
/**
* Add image to accession.
*
* @param instituteCode institute code (MCPD INSTCODE)
* @param accessionNumber accession number (MCPD ACCENUMB)
* @param file image to upload
* @param contentType the content type
* @return the repository image
* @throws GenesysApiException the Genesys API exception
* @throws IOException if file cannot be read
*/
public RepositoryImage uploadImage(final String instituteCode, final String accessionNumber, final File file, final String contentType) throws GenesysApiException, IOException {
if (StringUtils.isBlank(contentType)) {
throw new GenesysApiException("Content-Type must be provided for the file " + file.getAbsolutePath());
}
LOG.debug("Image content type: {}", contentType);
// PUT file on server
final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/", instituteCode, accessionNumber), Collections.singletonMap("originalFilename", file.getName()), contentType,
FileUtils.readFileToByteArray(file));