Added createOrganization method to create a ckan organization (it uses the ckan-connector)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@129183 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-20 14:05:29 +00:00
parent 2dc35c2424
commit e088102978
2 changed files with 59 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package org.gcube.datacatalogue.ckanutillibrary;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -11,6 +13,8 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HttpsURLConnection;
import net.htmlparser.jericho.Renderer;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;
@ -134,14 +138,14 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
String apiToReturn = null;
// the connection
Connection connection = null;
try{
connection = getConnection();
String query = "SELECT \"apikey\" FROM \"user\" WHERE \"name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, ckanUsername);
@ -157,9 +161,9 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
}catch(Exception e){
logger.error("Unable to retrieve key for user " + ckanUsername, e);
}finally{
closeConnection(connection);
}
return apiToReturn;
@ -169,12 +173,12 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
public CKanUserWrapper getUserFromApiKey(String apiKey) {
logger.debug("Request user whose api key is = " + apiKey);
CKanUserWrapper user = new CKanUserWrapper();
// the connection
Connection connection = null;
try{
connection = getConnection();
String query = "SELECT * FROM \"user\" WHERE \"apikey\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
@ -200,9 +204,9 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
}catch(Exception e){
logger.error("Unable to retrieve user with api key " + apiKey, e);
}finally{
closeConnection(connection);
}
return user;
}
@ -224,7 +228,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
Connection connection = null;
try{
connection = getConnection();
List<String> organizationIds = getOrganizationsIds();
@ -776,7 +780,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
//the connection
Connection connection = null;
try{
connection = getConnection();
// user id
@ -833,4 +837,39 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
logger.debug("Closing connection pool on finalize()");
connectionPool.close();
}
@Override
public boolean createOrganization(String orgName, String token)throws Exception{
logger.debug("Create organization method call for creation of an organization named " + orgName);
// we invoke the ckan connector to create this organization
boolean result = false;
if(token == null || token.trim().isEmpty()){
throw new IllegalArgumentException("Gcube Token is missing here!");
}
if(orgName == null || orgName.trim().isEmpty()){
throw new IllegalArgumentException("Organization name is missing here!");
}
try{
String callUrl = CKAN_CATALOGUE_URL + "/ckan-connector/organization/" + orgName + "?gcube-token=" + token;
URL url = new URL(callUrl);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("PUT");
logger.debug("Response code is " + connection.getResponseCode() + " and message is " + connection.getResponseMessage());
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
logger.info("CKan organization created [" + connection.getResponseMessage() + "]");
result = true;
}
}catch(Exception e){
logger.error("Unable to create the organization", e);
}
return result;
}
}

View File

@ -157,4 +157,13 @@ public interface CKanUtilsInterface {
*/
public boolean checkRole(String username, String organizationName,
RolesIntoOrganization correspondentRoleToCheck);
/**
* The method tries to create an organization with name orgName
* @param orgName the name of the organization
* @param token the gcube token to perform the operation
* @return true on success, false otherwise
* @throws Exception
*/
public boolean createOrganization(String orgName, String token) throws Exception;
}