added cache for user profiles. Minor bug fixes
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@146507 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
9a5ac5bbf5
commit
993102e663
|
@ -13,6 +13,7 @@ public class CachesManager {
|
|||
public static final CachesManager singleton = new CachesManager();
|
||||
|
||||
public static final String PROFILES_READERS_CACHE = "profile_readers";
|
||||
public static final String PROFILES_USERS_CACHE = "profile_users";
|
||||
|
||||
private CachesManager(){
|
||||
cacheManager = CacheManager.newInstance();
|
||||
|
|
|
@ -299,7 +299,7 @@ public class CatalogueUtils {
|
|||
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject dataset = (JSONObject)parser.parse(json);
|
||||
JSONObject profile = getUserProfile();
|
||||
JSONObject profile = getUserProfile(caller.getClient().getId());
|
||||
|
||||
// check license
|
||||
String licenseId = (String)dataset.get(LICENSE_KEY);
|
||||
|
@ -355,7 +355,7 @@ public class CatalogueUtils {
|
|||
|
||||
// get the metadata profile
|
||||
CustomField metadataTypeCF = null;
|
||||
List<CustomField> customFields = new ArrayList<CustomField>();
|
||||
List<CustomField> customFields = new ArrayList<CustomField>(extrasArrayOriginal.size());
|
||||
Iterator iterator = extrasArrayOriginal.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
JSONObject object = (JSONObject) iterator.next();
|
||||
|
@ -375,7 +375,7 @@ public class CatalogueUtils {
|
|||
if(tagsArrayOriginal == null)
|
||||
tagsArrayOriginal = new JSONArray();
|
||||
|
||||
// fetch the profile by metadatatype TODO EHCache here
|
||||
// fetch the profile by metadatatype
|
||||
MetadataFormat profile = null;
|
||||
String profileNameMatched = null;
|
||||
for (String profileName : profiles) {
|
||||
|
@ -426,8 +426,8 @@ public class CatalogueUtils {
|
|||
List<CustomField> validatedCustomFields = new ArrayList<CustomField>(customFields.size());
|
||||
|
||||
// keep track of mandatory (only) fields
|
||||
Map<String, Integer> fieldsMandatoryLowerBoundMap = new HashMap<String, Integer>();
|
||||
Map<String, Integer> numberFieldsSameKeyMap = new HashMap<String, Integer>();
|
||||
Map<String, Integer> fieldsMandatoryLowerBoundMap = new HashMap<String, Integer>(metadataFields.size());
|
||||
Map<String, Integer> numberFieldsSameKeyMap = new HashMap<String, Integer>(metadataFields.size());
|
||||
|
||||
// now validate fields
|
||||
int metadataIndex = 0;
|
||||
|
@ -457,7 +457,7 @@ public class CatalogueUtils {
|
|||
.next();
|
||||
int lowerBound = entry.getValue();
|
||||
int inserted = numberFieldsSameKeyMap.get(entry.getKey());
|
||||
logger.trace("Field with key " + entry.getKey() + " has been found " + inserted + " times and its lower bound is " + lowerBound);
|
||||
logger.trace("Field with key '" + entry.getKey() + "' has been found " + inserted + " times and its lower bound is " + lowerBound);
|
||||
if(inserted < lowerBound)
|
||||
throw new Exception("Field with key '" + entry.getKey() + "' is mandatory, but it's not present among the provided fields or its cardinality is not respected (" + lowerBound + ").");
|
||||
}
|
||||
|
@ -465,7 +465,7 @@ public class CatalogueUtils {
|
|||
// sort validated custom fields and add to the extrasArrayUpdated json array
|
||||
Collections.sort(validatedCustomFields);
|
||||
|
||||
logger.trace("****** SORTED LIST " + validatedCustomFields);
|
||||
logger.trace("SORTED LIST of custom fields is " + validatedCustomFields);
|
||||
|
||||
// add missing fields with no match (append them at the end, since no metadataIndex or categoryIndex was defined for them)
|
||||
for(CustomField cf : customFields)
|
||||
|
@ -618,7 +618,7 @@ public class CatalogueUtils {
|
|||
logger.debug("JSONArray field is " + groupsArrayOriginal);
|
||||
|
||||
MetadataGrouping grouping = metadataField.getGrouping();
|
||||
if(grouping != null && grouping.getCreate()){
|
||||
if(grouping != null){
|
||||
|
||||
final List<String> groupNames = new ArrayList<String>();
|
||||
|
||||
|
@ -644,18 +644,20 @@ public class CatalogueUtils {
|
|||
groupsArrayOriginal.add(group);
|
||||
}
|
||||
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (String title : groupNames)
|
||||
try {
|
||||
createGroupAsSysAdmin(title, title, "");
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to create group with title " + title, e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
|
||||
// force group creation if needed
|
||||
if(grouping.getCreate()){
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (String title : groupNames)
|
||||
try {
|
||||
createGroupAsSysAdmin(title, title, "");
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to create group with title " + title, e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -957,19 +959,27 @@ public class CatalogueUtils {
|
|||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static JSONObject getUserProfile() throws Exception{
|
||||
public static JSONObject getUserProfile(String userId) throws Exception{
|
||||
|
||||
GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL();
|
||||
String socialServiceUrl = socialService.getServiceBasePath();
|
||||
String url = socialServiceUrl + "2/users/get-profile";
|
||||
try(CloseableHttpClient client = HttpClientBuilder.create().build();){
|
||||
HttpGet getRequest = new HttpGet(url + "?gcube-token=" + SecurityTokenProvider.instance.get());
|
||||
HttpResponse response = client.execute(getRequest);
|
||||
JSONParser parser = new JSONParser();
|
||||
return (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
|
||||
}catch(Exception e){
|
||||
logger.error("error while performing get method " + e.toString());
|
||||
throw e;
|
||||
Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_USERS_CACHE);
|
||||
|
||||
if(profilesCache.isKeyInCache(userId))
|
||||
return (JSONObject) profilesCache.get(userId).getObjectValue();
|
||||
else{
|
||||
GcoreEndpointReaderSNL socialService = new GcoreEndpointReaderSNL();
|
||||
String socialServiceUrl = socialService.getServiceBasePath();
|
||||
String url = socialServiceUrl + "2/users/get-profile";
|
||||
try(CloseableHttpClient client = HttpClientBuilder.create().build();){
|
||||
HttpGet getRequest = new HttpGet(url + "?gcube-token=" + SecurityTokenProvider.instance.get());
|
||||
HttpResponse response = client.execute(getRequest);
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject profile = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
|
||||
profilesCache.put(new Element(userId, profile));
|
||||
return profile;
|
||||
}catch(Exception e){
|
||||
logger.error("error while performing get method " + e.toString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ public class Constants {
|
|||
public final static String PURGE_METHOD = "purge";
|
||||
public final static String DELETE_METHOD = "delete";
|
||||
public final static String PATCH_METHOD = "patch";
|
||||
public static final String LIST_METHOD = "list";
|
||||
|
||||
// this service's resources
|
||||
public final static String USERS = "api/user";
|
||||
|
@ -22,7 +23,6 @@ public class Constants {
|
|||
public static final String PROFILES = "api/profiles";
|
||||
public final static String RESOURCES = "api/resources";
|
||||
public final static String LICENSES = "api/licenses";
|
||||
public static final String LIST_METHOD = "api/list";
|
||||
|
||||
// api rest path CKAN
|
||||
public final static String CKAN_API_PATH = "/api/3/action/";
|
||||
|
|
|
@ -70,17 +70,15 @@ public class WritePostCatalogueManagerThread extends Thread {
|
|||
public void run() {
|
||||
|
||||
try{
|
||||
|
||||
|
||||
// set token and scope
|
||||
ScopeProvider.instance.set(scope);
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
|
||||
logger.info("Started request to write application post "
|
||||
+ "for new product created. Scope is " + scope + " and "
|
||||
+ "token is " + token.substring(0, 10) + "****************");
|
||||
|
||||
// set token and scope
|
||||
ScopeProvider.instance.set(scope);
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
|
||||
// write
|
||||
writeProductPost(
|
||||
productTitle,
|
||||
|
@ -257,15 +255,14 @@ public class WritePostCatalogueManagerThread extends Thread {
|
|||
|
||||
if (entity != null) {
|
||||
try {
|
||||
String jsonString = EntityUtils.toString(response.getEntity());
|
||||
JSONParser parser = new JSONParser();
|
||||
toReturn = (JSONObject)parser.parse(jsonString);
|
||||
return (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
|
||||
}catch(Exception e){
|
||||
logger.error("Failed to read json object", e);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Returning " + toReturn.toJSONString());
|
||||
logger.trace("Returning " + toReturn);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ public class Item {
|
|||
|
||||
// check resources information (name and url must be there)
|
||||
CatalogueUtils.checkResourcesInformation(json, caller);
|
||||
|
||||
|
||||
// Check if there are profiles here
|
||||
List<String> profiles = CatalogueUtils.getProfilesNames();
|
||||
|
||||
|
@ -69,21 +69,26 @@ public class Item {
|
|||
JSONObject obj = (JSONObject)parser.parse(CatalogueUtils.delegatePost(caller, context, Constants.ITEM_CREATE, json, uriInfo));
|
||||
|
||||
// after creation, if it was ok ...
|
||||
if((boolean)obj.get(CatalogueUtils.SUCCESS_KEY))
|
||||
if((boolean)obj.get(CatalogueUtils.SUCCESS_KEY)){
|
||||
|
||||
JSONObject result = (JSONObject)obj.get(CatalogueUtils.RESULT_KEY);
|
||||
|
||||
new PackageCreatePostActions(
|
||||
username,
|
||||
(String)(((JSONObject)obj.get(CatalogueUtils.RESULT_KEY)).get(CatalogueUtils.DATASET_KEY)),
|
||||
(String)(result.get(CatalogueUtils.DATASET_KEY)),
|
||||
context,
|
||||
SecurityTokenProvider.instance.get(),
|
||||
(JSONArray)((JSONObject)obj.get(CatalogueUtils.RESULT_KEY)).get(CatalogueUtils.TAGS_KEY),
|
||||
(String)(((JSONObject)obj.get(CatalogueUtils.RESULT_KEY)).get(CatalogueUtils.TITLE_KEY))
|
||||
(JSONArray)(result.get(CatalogueUtils.TAGS_KEY)),
|
||||
(String)(result.get(CatalogueUtils.TITLE_KEY))
|
||||
).start();
|
||||
|
||||
}
|
||||
|
||||
return obj.toJSONString();
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Something went wrong... ", e);
|
||||
return CatalogueUtils.createJSONOnFailure(e.toString());
|
||||
return CatalogueUtils.createJSONOnFailure(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.gcube.datacatalogue.catalogue.utils.Constants;
|
|||
public class License {
|
||||
|
||||
@GET
|
||||
@Path(Constants.LIST_METHOD)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public String show(@Context UriInfo uriInfo){
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ import javax.ws.rs.Path;
|
|||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
|
@ -55,26 +57,26 @@ public class Resource {
|
|||
@Path(Constants.CREATE_METHOD)
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public String create(
|
||||
public Response create(
|
||||
FormDataMultiPart multiPart, @Context UriInfo uriInfo
|
||||
){
|
||||
|
||||
// see also multipart https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
|
||||
|
||||
List<BodyPart> bodyParts = multiPart.getBodyParts();
|
||||
/*List<BodyPart> bodyParts = multiPart.getBodyParts();
|
||||
for (BodyPart bodyPart : bodyParts) {
|
||||
System.out.println("Body name is " + bodyPart.getContentDisposition().getFileName());
|
||||
}
|
||||
|
||||
Map<String, List<FormDataBodyPart>> fields = multiPart.getFields();
|
||||
System.out.println(fields);
|
||||
System.out.println(fields);*/
|
||||
|
||||
// see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.resource_create
|
||||
// see also multipart https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
|
||||
/*Caller caller = AuthorizationProvider.instance.get();
|
||||
String context = ScopeProvider.instance.get();
|
||||
return CatalogueUtils.delegatePost(caller, context, Constants.RESOURCE_CREATE, json, uriInfo);*/
|
||||
return "{\"test\": \"ok\"}";
|
||||
return CatalogueUtils.delegatePost(caller, context, Constants.RESOURCE_CREATE, multiPart, uriInfo);*/
|
||||
return Response.status(Status.NOT_IMPLEMENTED).build();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -10,4 +10,11 @@
|
|||
<persistence strategy="none" />
|
||||
</cache>
|
||||
|
||||
<cache name="profile_users" maxEntriesLocalHeap="1000" eternal="false"
|
||||
timeToIdleSeconds="3600" timeToLiveSeconds="3600"
|
||||
memoryStoreEvictionPolicy="LFU" transactionalMode="off"
|
||||
overflowToDisk="true" overflowToOffHeap="true">
|
||||
<persistence strategy="none" />
|
||||
</cache>
|
||||
|
||||
</ehcache>
|
Reference in New Issue