moved shared http utils code among ws implementations in a new HttpUtils class

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vo-management/usermanagement-core@151206 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-07-23 11:27:30 +00:00
parent f8a8a6b832
commit 875e16b1e7
4 changed files with 78 additions and 129 deletions

View File

@ -11,15 +11,10 @@ import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementNameException;
@ -27,6 +22,7 @@ import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalExcep
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.VirtualGroupNotExistingException;
import org.gcube.vomanagement.usermanagement.impl.ws.utils.HttpUtils;
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
@ -105,7 +101,7 @@ public class LiferayWSGroupManager implements GroupManager {
*/
private void retrieveCompanyId() throws Exception {
String json = executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
String json = HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
if(json != null){
JSONParser parser = new JSONParser();
@ -117,36 +113,6 @@ public class LiferayWSGroupManager implements GroupManager {
}
/**
* Execute an http GET request and returns the JSON response
* @param requestPath
* @return a JSON string on success, null otherwise
*/
private static String executeHTTPGETRequest(String requestPath, CredentialsProvider credsProvider, HttpClientContext localContext, HttpHost target){
try{
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpget = new HttpGet(requestPath);
logger.debug("Executing request " + httpget.getRequestLine() + " to target " + target);
CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
try {
String result = EntityUtils.toString(response.getEntity());
logger.debug("Request result is " + result);
return result;
} finally {
response.close();
}
}catch(Exception e){
logger.error("Exception while performing GET request", e);
}
return null;
}
/**
* Map a json object representing a group to a GCubeGroup object
* @param jsonGroup
@ -254,7 +220,7 @@ public class LiferayWSGroupManager implements GroupManager {
List<String> jsonChildren = new ArrayList<String>();
try{
String jsonGroups =
executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_PARENT_ID.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_PARENT_ID", String.valueOf(groupId))
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_PARENT_ID.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_PARENT_ID", String.valueOf(groupId))
.replace("$SITE", Boolean.toString(true)),
credsProvider, localContext, target);
@ -322,7 +288,7 @@ public class LiferayWSGroupManager implements GroupManager {
throws UserManagementSystemException, GroupRetrievalFault {
try {
String jsonGroup =
executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_ID.replace("$GROUP_ID", String.valueOf(groupId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_ID.replace("$GROUP_ID", String.valueOf(groupId)),
credsProvider, localContext, target);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(jsonGroup);
@ -338,7 +304,7 @@ public class LiferayWSGroupManager implements GroupManager {
throws UserManagementSystemException, GroupRetrievalFault {
try{
String jsonGroup =
executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_NAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_NAME", groupName),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_NAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_NAME", groupName),
credsProvider, localContext, target);
if(jsonGroup != null){
@ -359,7 +325,7 @@ public class LiferayWSGroupManager implements GroupManager {
throws UserManagementSystemException, GroupRetrievalFault {
try{
String jsonGroup =
executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_ID.replace("$GROUP_ID", String.valueOf(groupId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUP_BY_ID.replace("$GROUP_ID", String.valueOf(groupId)),
credsProvider, localContext, target);
if(jsonGroup != null){
@ -388,7 +354,7 @@ public class LiferayWSGroupManager implements GroupManager {
try {
String jsonCustomFields =
executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$CUSTOM_KEY", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).replace("$GROUP_ID", String.valueOf(actualGroupId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$CUSTOM_KEY", CustomAttributeKeys.VIRTUAL_GROUP.getKeyName()).replace("$GROUP_ID", String.valueOf(actualGroupId)),
credsProvider, localContext, target);
if(jsonCustomFields != null){
@ -539,7 +505,7 @@ public class LiferayWSGroupManager implements GroupManager {
List<GCubeGroup> toReturn = new ArrayList<GCubeGroup>();
try{
String jsonGroups = // TODO evaluate the max number of groups to return before, somehow
executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_USERID.replace("$USER_ID", String.valueOf(userId)).replace("$MAX_GROUP", String.valueOf(1000)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_USERID.replace("$USER_ID", String.valueOf(userId)).replace("$MAX_GROUP", String.valueOf(1000)),
credsProvider, localContext, target);
if(jsonGroups != null){
@ -564,7 +530,7 @@ public class LiferayWSGroupManager implements GroupManager {
List<GCubeGroup> toReturn = new ArrayList<GCubeGroup>();
try{
String jsonGroups = // TODO evaluate the max number of groups to return before, somehow
executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_USERID.replace("$USER_ID", String.valueOf(userId)).replace("$MAX_GROUP", String.valueOf(1000)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_USERID.replace("$USER_ID", String.valueOf(userId)).replace("$MAX_GROUP", String.valueOf(1000)),
credsProvider, localContext, target);
if(jsonGroups != null){
@ -655,7 +621,7 @@ public class LiferayWSGroupManager implements GroupManager {
throws GroupRetrievalFault {
String result =
executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_ID", String.valueOf(groupId)).replace("$CUSTOM_KEY", attributeKey),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUP_CUSTOM_FIELDS.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_ID", String.valueOf(groupId)).replace("$CUSTOM_KEY", attributeKey),
credsProvider, localContext, target);
logger.debug("Data is " + result);
@ -689,7 +655,7 @@ public class LiferayWSGroupManager implements GroupManager {
try{
String jsonGroups =
executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_PARENT_ID.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_PARENT_ID", String.valueOf(0))
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_GROUPS_BY_PARENT_ID.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$GROUP_PARENT_ID", String.valueOf(0))
.replace("$SITE", Boolean.toString(true)),
credsProvider, localContext, target);

View File

@ -8,21 +8,17 @@ import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.TeamRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.ws.utils.HttpUtils;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
import org.gcube.vomanagement.usermanagement.model.GCubeTeam;
import org.json.simple.JSONArray;
@ -92,7 +88,7 @@ public class LiferayWSRoleManager implements RoleManager{
*/
private void retrieveCompanyId() throws Exception {
String json = executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
String json = HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
if(json != null){
JSONParser parser = new JSONParser();
@ -104,36 +100,6 @@ public class LiferayWSRoleManager implements RoleManager{
}
/**
* Execute an http GET request and returns the JSON response
* @param requestPath
* @return a JSON string on success, null otherwise
*/
private static String executeHTTPGETRequest(String requestPath, CredentialsProvider credsProvider, HttpClientContext localContext, HttpHost target){
try{
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpget = new HttpGet(requestPath);
logger.debug("Executing request " + httpget.getRequestLine() + " to target " + target);
CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
try {
String result = EntityUtils.toString(response.getEntity());
logger.debug("Request result is " + result);
return result;
} finally {
response.close();
}
}catch(Exception e){
logger.error("Exception while performing GET request", e);
}
return null;
}
//simple role mapping
protected static GCubeRole mapLRRole(String jsonRole) throws PortalException, SystemException, ParseException {
logger.debug("Json object for role is " + jsonRole);
@ -242,7 +208,7 @@ public class LiferayWSRoleManager implements RoleManager{
public long getRoleIdByName(String roleName) throws RoleRetrievalFault {
String jsonRole =
executeHTTPGETRequest(API_BASE_URL + GET_ROLE_BY_NAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$NAME", roleName),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_ROLE_BY_NAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$NAME", roleName),
credsProvider, localContext, target);
logger.debug("Json returned is " + jsonRole);
@ -277,7 +243,7 @@ public class LiferayWSRoleManager implements RoleManager{
List<GCubeRole> toReturn = new ArrayList<GCubeRole>();
String jsonRoles =
executeHTTPGETRequest(API_BASE_URL + GET_USER_ROLES_IN_GROUP.replace("$GROUP_ID", String.valueOf(groupId)).replace("$USER_ID", String.valueOf(userId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_ROLES_IN_GROUP.replace("$GROUP_ID", String.valueOf(groupId)).replace("$USER_ID", String.valueOf(userId)),
credsProvider, localContext, target);
if(jsonRoles != null){

View File

@ -17,15 +17,10 @@ import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
@ -33,6 +28,7 @@ import org.gcube.vomanagement.usermanagement.exception.TeamRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.ws.utils.HttpUtils;
import org.gcube.vomanagement.usermanagement.model.Email;
import org.gcube.vomanagement.usermanagement.model.GCubeMembershipRequest;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
@ -128,7 +124,7 @@ public class LiferayWSUserManager implements UserManager{
*/
private void retrieveCompanyId() throws Exception {
String json = executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
String json = HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_COMPANY_ID, credsProvider, localContext, target);
if(json != null){
JSONParser parser = new JSONParser();
@ -140,37 +136,6 @@ public class LiferayWSUserManager implements UserManager{
}
/**
* Execute an http GET request and returns the JSON response
* @param requestPath
* @return a JSON string on success, null otherwise
*/
private static String executeHTTPGETRequest(String requestPath, CredentialsProvider credsProvider, HttpClientContext localContext, HttpHost target){
try{
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpget = new HttpGet(requestPath);
logger.debug("Executing request " + httpget.getRequestLine() + " to target " + target);
CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
try {
String result = EntityUtils.toString(response.getEntity());
logger.debug("Request result is " + result);
return result;
} finally {
response.close();
}
}catch(Exception e){
logger.error("Exception while performing GET request with path " + requestPath, e);
}
return null;
}
/**
* Maps the JSON user to the GCubeUser.class object
* @param json
@ -266,7 +231,7 @@ public class LiferayWSUserManager implements UserManager{
*/
private String getContactJson(long contactId) {
return executeHTTPGETRequest(API_BASE_URL + GET_CONTACT_BY_USER_ID.replace("$CONTACT_ID", String.valueOf(contactId)),
return HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_CONTACT_BY_USER_ID.replace("$CONTACT_ID", String.valueOf(contactId)),
credsProvider, localContext, target);
}
@ -321,7 +286,7 @@ public class LiferayWSUserManager implements UserManager{
throws UserManagementSystemException, UserRetrievalFault {
String jsonUser =
executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_USERNAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$USER_ID", username),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_USERNAME.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$USER_ID", username),
credsProvider, localContext, target);
if(jsonUser != null){
@ -342,7 +307,7 @@ public class LiferayWSUserManager implements UserManager{
public GCubeUser getUserByEmail(String email)
throws UserManagementSystemException, UserRetrievalFault {
String jsonUser =
executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_EMAIL.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$EMAIL", email),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_EMAIL.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$EMAIL", email),
credsProvider, localContext, target);
if(jsonUser != null){
@ -357,7 +322,7 @@ public class LiferayWSUserManager implements UserManager{
throws UserManagementSystemException, UserRetrievalFault {
String jsonUser =
executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_ID.replace("$USER_ID", String.valueOf(userId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_BY_ID.replace("$USER_ID", String.valueOf(userId)),
credsProvider, localContext, target);
if(jsonUser != null){
@ -410,7 +375,7 @@ public class LiferayWSUserManager implements UserManager{
try{
List<GCubeUser> toReturn = new ArrayList<GCubeUser>();
String jsonUsers =
executeHTTPGETRequest(API_BASE_URL + GET_USERS_BY_GROUP.replace("$GROUP_ID", String.valueOf(groupId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USERS_BY_GROUP.replace("$GROUP_ID", String.valueOf(groupId)),
credsProvider, localContext, target);
if(jsonUsers != null){
@ -527,7 +492,7 @@ public class LiferayWSUserManager implements UserManager{
long userId = gCubeUser.getUserId();
String userRoles =
executeHTTPGETRequest(API_BASE_URL + GET_ROLES_IN_GROUP_BY_USER.replace("$GROUP_ID", String.valueOf(groupId)).replace("$USER_ID", String.valueOf(userId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_ROLES_IN_GROUP_BY_USER.replace("$GROUP_ID", String.valueOf(groupId)).replace("$USER_ID", String.valueOf(userId)),
credsProvider, localContext, target);
boolean userHasRole = false;
@ -647,7 +612,7 @@ public class LiferayWSUserManager implements UserManager{
String toReturn = null;
try{
String jsonCustomField =
executeHTTPGETRequest(API_BASE_URL + GET_USER_CUSTOM_FIELD_BY_KEY.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$CUSTOM_FIELD_KEY", attributeKey).replace("$USER_ID", String.valueOf(userId)),
HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_CUSTOM_FIELD_BY_KEY.replace("$COMPANY_ID", String.valueOf(companyId)).replace("$CUSTOM_FIELD_KEY", attributeKey).replace("$USER_ID", String.valueOf(userId)),
credsProvider, localContext, target);
if(jsonCustomField != null){
logger.debug("Trying to parse custom field in json object");
@ -676,7 +641,7 @@ public class LiferayWSUserManager implements UserManager{
List<GCubeUser> toReturn = null;
try{
String listIds = executeHTTPGETRequest(API_BASE_URL + GET_IDS_USERS_HAVING_GLOBAL_ROLE.replace("$ROLE_ID", String.valueOf(roleId)),
String listIds = HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_IDS_USERS_HAVING_GLOBAL_ROLE.replace("$ROLE_ID", String.valueOf(roleId)),
credsProvider, localContext, target);
if(listIds != null){
@ -711,7 +676,7 @@ public class LiferayWSUserManager implements UserManager{
try{
String listIds = executeHTTPGETRequest(API_BASE_URL + GET_USER_ID_BY_GROUP.replace("$GROUP_ID", String.valueOf(groupId)),
String listIds = HttpUtils.executeHTTPGETRequest(API_BASE_URL + GET_USER_ID_BY_GROUP.replace("$GROUP_ID", String.valueOf(groupId)),
credsProvider, localContext, target);
if(listIds != null){

View File

@ -0,0 +1,52 @@
package org.gcube.vomanagement.usermanagement.impl.ws.utils;
import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;
/**
* Http utils methods for web services
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class HttpUtils {
// logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HttpUtils.class);
/**
* Execute an http GET request and returns the JSON response
* @param requestPath
* @return a JSON string on success, null otherwise
*/
public static String executeHTTPGETRequest(String requestPath, CredentialsProvider credsProvider, HttpClientContext localContext, HttpHost target){
try{
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpget = new HttpGet(requestPath);
logger.debug("Executing request " + httpget.getRequestLine() + " to target " + target);
CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
try {
String result = EntityUtils.toString(response.getEntity());
logger.debug("Request result is " + result);
return result;
} finally {
response.close();
}
}catch(Exception e){
logger.error("Exception while performing GET request", e);
}
return null;
}
}