Task #16895: Trying to patch getOrganization.getUsers and getGroups.getUsers

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@179737 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2019-06-07 16:26:47 +00:00
parent 0e7f08de7c
commit 6d7d329091
4 changed files with 235 additions and 4 deletions

View File

@ -12,6 +12,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
@ -24,5 +25,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

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

View File

@ -105,7 +105,7 @@ public class DataCatalogueImpl implements DataCatalogue{
private static final String CATALOGUE_TAB_ENDING_URL = "/catalogue";
// ckan client
private CkanClient client;
private SimpleExtendCkanClient client;
// hashmap for ckan api keys
private ConcurrentHashMap<String, CKANTokenBean> apiKeysMap;
@ -152,7 +152,7 @@ public class DataCatalogueImpl implements DataCatalogue{
logger.debug("Plain db password first 3 chars are " + CKAN_DB_PASSWORD.substring(0, 3));
// build the client
client = new CkanClient(CKAN_CATALOGUE_URL);
client = new SimpleExtendCkanClient(CKAN_CATALOGUE_URL);
// init map
apiKeysMap = new ConcurrentHashMap<String, CKANTokenBean>();
@ -2686,7 +2686,7 @@ public class DataCatalogueImpl implements DataCatalogue{
return client.getOrganization(ckanName);
}catch(Exception e){
logger.warn("Failed to retrieve the organization with name" +name+ " on the ckan: "+client.getCatalogUrl());
logger.warn("Failed to retrieve the organization with name " +name+ " on the ckan: "+client.getCatalogUrl(), e);
//OLD PRINTING. FIXING Task #12500
//logger.error("Failed to retrieve the organization with name" + name, e);
}

View File

@ -0,0 +1,229 @@
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.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.CkanClient;
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 SimpleExtendCkanClient.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Jun 7, 2019
*/
public class SimpleExtendCkanClient extends CkanClient{
private String catalogueURL;
@Nullable
private static ObjectMapper objectMapper;
private static final Logger logger = LoggerFactory.getLogger(SimpleExtendCkanClient.class);
/**
* Instantiates a new simple extend ckan client.
*
* @param catalogueURL the catalogue URL
*/
public SimpleExtendCkanClient(String catalogueURL) {
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
*/
public synchronized CkanOrganization getOrganization(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
return getHttp(OrganizationResponse.class, "/api/3/action/organization_show", "id", idOrName,
"include_datasets", "false", "include_users", "true").result;
}
/**
* 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
*/
public synchronized CkanGroup getGroup(String idOrName) {
checkNotNull(idOrName, "Need a valid id or name!");
return getHttp(GroupResponse.class, "/api/3/action/group_show", "id", idOrName, "include_datasets",
"false", "include_users", "true").result;
}
/**
* 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;
}
/**
* 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(){
}
}
/**
* 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
*/
private <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.debug("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;
}
/**
* 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;
}
/**
* 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) {
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);
}
}
}