Updated pom version at 2.10.0

Task #16895 see https://support.d4science.org/issues/16895#note-9

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@179846 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2019-06-14 15:01:57 +00:00
parent d81560d9fe
commit cf00b74ad6
6 changed files with 300 additions and 104 deletions

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<ReleaseNotes>
<Changeset
component="org.gcube.data-catalogue.ckan-util-library.2-10-0"
date="${buildDate}">
<Change>[Task #16895] Try to patch some methods of the jackan library</Change>
</Changeset>
<Changeset
component="org.gcube.data-catalogue.ckan-util-library.2-9-0"
date="${buildDate}">
@ -49,7 +54,8 @@
<Change>Minor fix for delegates</Change>
<Change>Added support for statistics (requires solrj)</Change>
<Change>Added method for retrieving main landing pages</Change>
</Changeset>Added title on updated of records refs #16395
</Changeset>
Added title on updated of records refs #16395
<Changeset
component="org.gcube.data-catalogue.ckan-util-library.2-4-0"
date="2017-11-01">

View File

@ -10,7 +10,7 @@
</parent>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>ckan-util-library</artifactId>
<version>2.9.1-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CKan utility library</name>

View File

@ -3,24 +3,46 @@
*/
package org.gcube.datacatalogue.ckanutillibrary.server;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Arrays;
import javax.annotation.Nullable;
import org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import eu.trentorise.opendata.jackan.CheckedCkanClient;
import eu.trentorise.opendata.jackan.exceptions.CkanException;
import eu.trentorise.opendata.jackan.exceptions.JackanException;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.fluent.Request;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.fluent.Response;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
import eu.trentorise.opendata.jackan.model.CkanResponse;
/**
* The Class ExtendCkanClient.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Jun 27, 2018
*
* Jun 14, 2019
*/
public class ExtendCkanClient extends CheckedCkanClient{
public class ExtendCkanClient extends CheckedCkanClient implements PatchedCkan{
private static final Logger logger = LoggerFactory.getLogger(ExtendCkanClient.class);
@Nullable
private static ObjectMapper objectMapper;
private String catalogueURL;
private String ckanToken;
@ -113,8 +135,107 @@ public class ExtendCkanClient extends CheckedCkanClient{
public int getTimeout() {
return extendTimeout;
}
/* (non-Javadoc)
* @see eu.trentorise.opendata.jackan.CkanClient#getOrganization(java.lang.String)
*/
public synchronized CkanOrganization getOrganization(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
logger.info("Patched read organization for id/name: {}", idOrName);
return getHttp(OrganizationResponse.class, "/api/3/action/organization_show", "id", idOrName,
"include_datasets", "false", "include_users", "true").result;
}
/* (non-Javadoc)
* @see eu.trentorise.opendata.jackan.CkanClient#getGroup(java.lang.String)
*/
public synchronized CkanGroup getGroup(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
logger.info("Patched read group for id/name: {}", idOrName);
return getHttp(GroupResponse.class, "/api/3/action/group_show", "id", idOrName, "include_datasets",
"false", "include_users", "true").result;
}
/**
* Retrieves the Jackson object mapper for reading operations. Internally,
* Object mapper is initialized at first call.
*
* @return the object mapper
*/
static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
configureObjectMapper(objectMapper);
}
return objectMapper;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan#getHttp(java.lang.Class, java.lang.String, java.lang.Object[])
*/
public <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params) {
checkNotNull(responseType);
checkNotNull(path);
String fullUrl = calcFullUrl(path, params);
T ckanResponse;
String returnedText;
try {
logger.debug("getting {}", fullUrl);
Request request = Request.Get(fullUrl);
configureRequest(request);
Response response = request.execute();
InputStream stream = response.returnResponse()
.getEntity()
.getContent();
try (InputStreamReader reader = new InputStreamReader(stream, Charsets.UTF_8)) {
returnedText = CharStreams.toString(reader);
}
logger.trace("returnedText {}", returnedText);
} catch (Exception ex) {
throw new CkanException("Error while performing GET. Request url was: " + fullUrl, this, ex);
}
try {
ckanResponse = getObjectMapper().readValue(returnedText, responseType);
} catch (Exception ex) {
throw new CkanException(
"Couldn't interpret json returned by the server! Returned text was: " + returnedText, this, ex);
}
if (!ckanResponse.isSuccess()) {
throwCkanException("Error while performing GET. Request url was: " + fullUrl, ckanResponse);
}
return ckanResponse;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan#calcFullUrl(java.lang.String, java.lang.Object[])
*/
public String calcFullUrl(String path, Object[] params) {
checkNotNull(path);
try {
StringBuilder sb = new StringBuilder().append(catalogueURL)
.append(path);
for (int i = 0; i < params.length; i += 2) {
sb.append(i == 0 ? "?" : "&")
.append(URLEncoder.encode(params[i].toString(), "UTF-8"))
.append("=")
.append(URLEncoder.encode(params[i + 1].toString(), "UTF-8"));
}
return sb.toString();
} catch (Exception ex) {
throw new JackanException("Error while building url to perform GET! \n path: " + path + " \n params: "
+ Arrays.toString(params), ex);
}
}
}

View File

@ -9,6 +9,7 @@ import java.util.Arrays;
import javax.annotation.Nullable;
import org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,7 +34,7 @@ import eu.trentorise.opendata.jackan.model.CkanResponse;
*
* Jun 7, 2019
*/
public class SimpleExtendCkanClient extends CkanClient{
public class SimpleExtendCkanClient extends CkanClient implements PatchedCkan{
private String catalogueURL;
@ -52,26 +53,9 @@ public class SimpleExtendCkanClient extends CkanClient{
super(catalogueURL);
this.catalogueURL = catalogueURL;
}
/**
* Gets the catalogue url.
*
* @return the catalogueURL
*/
public String getCatalogueURL() {
return catalogueURL;
}
/**
* Returns a Ckan organization.
*
* @param idOrName either the name of organization (i.e. culture-and-education)
* or the alphanumerical id (i.e.
* 232cad97-ecf2-447d-9656-63899023887f). Do not pass it a group
* id.
* @return the organization
* @throws JackanException on error
/* (non-Javadoc)
* @see eu.trentorise.opendata.jackan.CkanClient#getOrganization(java.lang.String)
*/
public synchronized CkanOrganization getOrganization(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
@ -81,15 +65,9 @@ public class SimpleExtendCkanClient extends CkanClient{
}
/**
* Returns a Ckan group. Do not pass an organization id, to get organization
* use {@link #getOrganization(java.lang.String) } instead.
*
* @param idOrName either the group name (i.e. hospitals-in-trento-district) or
* the group alphanumerical id (i.e.
* 55bb5fbd-7a7c-4eb8-8b1a-1192a5504421)
* @return the group
* @throws JackanException on error
/* (non-Javadoc)
* @see eu.trentorise.opendata.jackan.CkanClient#getGroup(java.lang.String)
*/
public synchronized CkanGroup getGroup(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
@ -100,67 +78,36 @@ public class SimpleExtendCkanClient extends CkanClient{
/**
* The Class OrganizationResponse.
* Retrieves the Jackson object mapper for reading operations. Internally,
* Object mapper is initialized at first call.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 7, 2019
* @return the object mapper
*/
static class OrganizationResponse extends CkanResponse {
public CkanOrganization result;
}
/**
* The Class GroupResponse.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 7, 2019
*/
static class GroupResponse extends CkanResponse {
public CkanGroup result;
/**
* Instantiates a new group response.
*/
public GroupResponse(){
}
static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
configureObjectMapper(objectMapper);
}
return objectMapper;
}
/**
* Performs HTTP GET on server. If {@link CkanResponse#isSuccess()} is false
* throws {@link CkanException}.
*
* @param <T> the generic type
* @param responseType a descendant of CkanResponse
* @param path something like /api/3/package_show
* @param params list of key, value parameters. They must be not be url
* encoded. i.e. "id","laghi-monitorati-trento"
* @return the http
* @throws CkanException on error
/* (non-Javadoc)
* @see org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan#getHttp(java.lang.Class, java.lang.String, java.lang.Object[])
*/
private <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params) {
public <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params) {
checkNotNull(responseType);
checkNotNull(path);
String fullUrl = calcFullUrl(path, params);
T ckanResponse;
String returnedText;
try {
logger.debug("getting {}", fullUrl);
Request request = Request.Get(fullUrl);
configureRequest(request);
Response response = request.execute();
InputStream stream = response.returnResponse()
.getEntity()
.getContent();
@ -185,31 +132,12 @@ public class SimpleExtendCkanClient extends CkanClient{
}
return ckanResponse;
}
/**
* Retrieves the Jackson object mapper for reading operations. Internally,
* Object mapper is initialized at first call.
*
* @return the object mapper
/* (non-Javadoc)
* @see org.gcube.datacatalogue.ckanutillibrary.server.patch.PatchedCkan#calcFullUrl(java.lang.String, java.lang.Object[])
*/
static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
configureObjectMapper(objectMapper);
}
return objectMapper;
}
/**
* Calculates a full url out of the provided params.
*
* @param path something like /api/3/package_show
* @param params list of key, value parameters. They must be not be url
* encoded. i.e. "id","laghi-monitorati-trento"
* @return the full url to be called.
* @throws JackanException if there is any error building the url
*/
private String calcFullUrl(String path, Object[] params) {
public String calcFullUrl(String path, Object[] params) {
checkNotNull(path);
try {
@ -227,5 +155,15 @@ public class SimpleExtendCkanClient extends CkanClient{
+ Arrays.toString(params), ex);
}
}
/**
* Gets the catalogue url.
*
* @return the catalogueURL
*/
public String getCatalogueURL() {
return catalogueURL;
}
}

View File

@ -0,0 +1,113 @@
package org.gcube.datacatalogue.ckanutillibrary.server.patch;
import eu.trentorise.opendata.jackan.CkanClient;
import eu.trentorise.opendata.jackan.exceptions.CkanException;
import eu.trentorise.opendata.jackan.exceptions.JackanException;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
import eu.trentorise.opendata.jackan.model.CkanResponse;
/**
* The Interface PatchedCkan.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 14, 2019
*/
public interface PatchedCkan {
/**
*
* This Class is private in {@link CkanClient} so I need to rewrite it
*
* The Class OrganizationResponse.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 7, 2019
*/
static class OrganizationResponse extends CkanResponse {
public CkanOrganization result;
}
/**
*
*
* This Class is private in {@link CkanClient} so I need to rewrite it
*
*
* The Class GroupResponse.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 7, 2019
*/
static class GroupResponse extends CkanResponse {
public CkanGroup result;
/**
* Instantiates a new group response.
*/
public GroupResponse() {
}
}
/**
*
* This method is private in {@link CkanClient} so I need to rewrite it
*
*
*
* Performs HTTP GET on server. If {@link CkanResponse#isSuccess()} is false
* throws {@link CkanException}.
*
* @param <T> the generic type
* @param responseType a descendant of CkanResponse
* @param path something like /api/3/package_show
* @param params list of key, value parameters. They must be not be url
* encoded. i.e. "id","laghi-monitorati-trento"
* @return the http
* @throws CkanException on error
*/
public <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params);
/**
*
* This method is private in {@link CkanClient} so I need to rewrite it
*
*
* Calculates a full url out of the provided params.
*
* @param path something like /api/3/package_show
* @param params list of key, value parameters. They must be not be url encoded.
* i.e. "id","laghi-monitorati-trento"
* @return the full url to be called.
* @throws JackanException if there is any error building the url
*/
public String calcFullUrl(String path, Object[] params);
// WE NEED TO OVERRIDE THE FOLLOWING METHODS
/**
* Gets the organization.
*
* @param idOrName the id or name
* @return the organization
*/
public CkanOrganization getOrganization(String idOrName);
/**
* Gets the group.
*
* @param idOrName the id or name
* @return the group
*/
public CkanGroup getGroup(String idOrName);
}

View File

@ -36,7 +36,8 @@ public class TestDataCatalogueLib {
private DataCatalogueFactory factory;
private String scope = "/gcube/devNext/NextNext";
private String testUser = "costantino_perciante";
//private String testUser = "costantino_perciante";
private String testUser = "francesco_mangiacrapa";
String subjectId = "aa_father4";
String objectId = "bb_son4";
@ -86,6 +87,23 @@ public class TestDataCatalogueLib {
}
}
//@Test
public void getRoleOfUserInOrganization() throws Exception{
String orgName = "nextnext";
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
CkanOrganization org = utils.getOrganizationByName(orgName);
String apiKey = utils.getApiKeyFromUsername(testUser);
String role = utils.getRoleOfUserInOrganization(testUser, org.getName(), apiKey);
logger.debug("The user "+testUser+" in the org "+org.getName() + " has the role "+role);
}
//@Test
public void getGroupForName() throws Exception{