RM-5416 - revert auto formatting of BaseAPI class

This commit is contained in:
Ana Bozianu
2017-09-18 21:25:27 +03:00
parent dd61bc3f8b
commit 57ec2362f3

View File

@@ -24,7 +24,6 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.core.v0; package org.alfresco.rest.core.v0;
import java.io.IOException; import java.io.IOException;
@@ -118,15 +117,13 @@ public abstract class BaseAPI
} }
/** /**
* Helper method to extract property values from request result and put them in map as a list that corresponds to a * Helper method to extract property values from request result and put them in map as a list that corresponds to a unique property value.
* unique property value.
* *
* @param requestResult the request response * @param requestResult the request response
* @return a map containing information about multiple properties values that correspond to a unique one * @return a map containing information about multiple properties values that correspond to a unique one
* @throws RuntimeException for malformed response * @throws RuntimeException for malformed response
*/ */
protected Map<String, List<String>> getPropertyValuesByUniquePropertyValue(JSONObject requestResult, protected Map<String, List<String>> getPropertyValuesByUniquePropertyValue(JSONObject requestResult, String uniqueProperty, List<String> otherProperties)
String uniqueProperty, List<String> otherProperties)
{ {
Map<String, List<String>> valuesByUniqueProperty = new HashMap<>(); Map<String, List<String>> valuesByUniqueProperty = new HashMap<>();
try try
@@ -157,8 +154,9 @@ public abstract class BaseAPI
* *
* @param username the username * @param username the username
* @param password the password * @param password the password
* @param path the path to the container eg. in case of a category it would be the category name, in case of a * @param path the path to the container eg. in case of a category it would be the category name,
* folder it would be /categoryName/folderName when trying to get File Plan, the path would be "" * in case of a folder it would be /categoryName/folderName
* when trying to get File Plan, the path would be ""
* @return the container nodeRef * @return the container nodeRef
*/ */
public String getItemNodeRef(String username, String password, String path) public String getItemNodeRef(String username, String password, String path)
@@ -180,8 +178,7 @@ public abstract class BaseAPI
try try
{ {
object = contentService.getCMISSession(username, password).getObjectByPath(path); object = contentService.getCMISSession(username, password).getObjectByPath(path);
} } catch (CmisObjectNotFoundException notFoundError)
catch (CmisObjectNotFoundException notFoundError)
{ {
return null; return null;
} }
@@ -196,19 +193,22 @@ public abstract class BaseAPI
* @param parameters if the request has parameters * @param parameters if the request has parameters
* @return result object (see API reference for more details), null for any errors * @return result object (see API reference for more details), null for any errors
*/ */
protected JSONObject facetedRequest(String username, String password, List<NameValuePair> parameters, protected JSONObject facetedRequest(String username, String password, List<NameValuePair> parameters, String requestURI)
String requestURI)
{ {
String requestURL; String requestURL;
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
if (parameters == null || parameters.isEmpty()) if (parameters == null || parameters.isEmpty())
{ {
requestURL = MessageFormat.format(requestURI, client.getAlfrescoUrl()); requestURL = MessageFormat.format(
requestURI,
client.getAlfrescoUrl());
} }
else else
{ {
requestURL = MessageFormat.format(requestURI, client.getAlfrescoUrl(), requestURL = MessageFormat.format(
requestURI,
client.getAlfrescoUrl(),
URLEncodedUtils.format(parameters, "UTF-8")); URLEncodedUtils.format(parameters, "UTF-8"));
} }
client.close(); client.close();
@@ -217,17 +217,21 @@ public abstract class BaseAPI
/** /**
* Helper method for GET requests * Helper method for GET requests
*
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected JSONObject doGetRequest(String adminUser, String adminPassword, String urlTemplate, protected JSONObject doGetRequest(String adminUser,
String adminPassword,
String urlTemplate,
String ... urlTemplateParams) String ... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams); String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
client.close(); client.close();
try try
@@ -242,17 +246,21 @@ public abstract class BaseAPI
/** /**
* Helper method for Delete requests * Helper method for Delete requests
*
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected JSONObject doDeleteRequest(String adminUser, String adminPassword, String urlTemplate, protected JSONObject doDeleteRequest(String adminUser,
String adminPassword,
String urlTemplate,
String ... urlTemplateParams) String ... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams); String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
client.close(); client.close();
try try
@@ -267,18 +275,23 @@ public abstract class BaseAPI
/** /**
* Helper method for PUT requests * Helper method for PUT requests
*
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected JSONObject doPutRequest(String adminUser, String adminPassword, JSONObject requestParams, protected JSONObject doPutRequest(String adminUser,
String urlTemplate, String... urlTemplateParams) String adminPassword,
JSONObject requestParams,
String urlTemplate,
String ... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams); String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
client.close(); client.close();
try try
@@ -293,20 +306,27 @@ public abstract class BaseAPI
/** /**
* Helper method for POST requests * Helper method for POST requests
*
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected JSONObject doPostRequest(String adminUser, String adminPassword, JSONObject requestParams, protected JSONObject doPostRequest(String adminUser,
String urlTemplate, String... urlTemplateParams) String adminPassword,
JSONObject requestParams,
String urlTemplate,
String ... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams); String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
client.close(); client.close();
LOGGER.debug("REQUEST ***** ", requestUrl);
System.out.println("REQUEST ***** " + requestUrl);
System.out.println("REQUEST PARAMS ***** " + requestParams);
try try
{ {
return doRequest(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); return doRequest(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
@@ -326,11 +346,17 @@ public abstract class BaseAPI
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected boolean doPostJsonRequest(String adminUser, String adminPassword, JSONObject requestParams, protected boolean doPostJsonRequest(String adminUser,
String urlTemplate, String... urlTemplateParams) String adminPassword,
JSONObject requestParams,
String urlTemplate,
String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
String requestUrl = MessageFormat.format(urlTemplate, client.getApiUrl(), urlTemplateParams); String requestUrl = MessageFormat.format(
urlTemplate,
client.getApiUrl(),
urlTemplateParams);
client.close(); client.close();
try try
@@ -345,7 +371,6 @@ public abstract class BaseAPI
/** /**
* Helper method for handling generic HTTP requests * Helper method for handling generic HTTP requests
*
* @param requestType request type (a subclass of {@link HttpRequestBase}) * @param requestType request type (a subclass of {@link HttpRequestBase})
* @param requestUrl URL the request is to be sent to * @param requestUrl URL the request is to be sent to
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
@@ -355,8 +380,12 @@ public abstract class BaseAPI
* @throws IllegalAccessException for invalid <i>requestType</i> * @throws IllegalAccessException for invalid <i>requestType</i>
* @throws InstantiationException for invalid <i>requestType</i> * @throws InstantiationException for invalid <i>requestType</i>
*/ */
private <T extends HttpRequestBase> JSONObject doRequest(Class<T> requestType, String requestUrl, String adminUser, private <T extends HttpRequestBase> JSONObject doRequest(
String adminPassword, JSONObject requestParams) throws InstantiationException, IllegalAccessException Class<T> requestType,
String requestUrl,
String adminUser,
String adminPassword,
JSONObject requestParams) throws InstantiationException, IllegalAccessException
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
T request = requestType.newInstance(); T request = requestType.newInstance();
@@ -429,8 +458,12 @@ public abstract class BaseAPI
return returnValues; return returnValues;
} }
private <T extends HttpRequestBase> boolean doRequestJson(Class<T> requestType, String requestUrl, String adminUser, private <T extends HttpRequestBase> boolean doRequestJson(
String adminPassword, JSONObject requestParams) throws InstantiationException, IllegalAccessException Class<T> requestType,
String requestUrl,
String adminUser,
String adminPassword,
JSONObject requestParams) throws InstantiationException, IllegalAccessException
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
T request = requestType.newInstance(); T request = requestType.newInstance();
@@ -445,8 +478,7 @@ public abstract class BaseAPI
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(requestParams.toString())); ((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(requestParams.toString()));
} }
return client.execute(adminUser, adminPassword, request).getStatusLine() return client.execute(adminUser, adminPassword, request).getStatusLine().getStatusCode() == HttpStatus.SC_OK;
.getStatusCode() == HttpStatus.SC_OK;
} }
catch (UnsupportedEncodingException | URISyntaxException error1) catch (UnsupportedEncodingException | URISyntaxException error1)
{ {
@@ -465,16 +497,36 @@ public abstract class BaseAPI
} }
/** /**
* Used to set RM items properties including records, categories and folders * Used to set RM items properties
* including records, categories and folders
*/ */
public enum RMProperty public enum RMProperty
{ {
NAME, TITLE, CONTENT, DESCRIPTION, AUTHOR, PHYSICAL_SIZE, NUMBER_OF_COPIES, STORAGE_LOCATION, SHELF, BOX, FILE, NAME,
TITLE,
CONTENT,
DESCRIPTION,
AUTHOR,
PHYSICAL_SIZE,
NUMBER_OF_COPIES,
STORAGE_LOCATION,
SHELF,
BOX,
FILE,
} }
public enum RETENTION_SCHEDULE public enum RETENTION_SCHEDULE
{ {
NAME, DESCRIPTION, RETENTION_AUTHORITY, RETENTION_INSTRUCTIONS, RETENTION_PERIOD, RETENTION_LOCATION, RETENTION_PERIOD_PROPERTY, RETENTION_GHOST, RETENTION_ELIGIBLE_FIRST_EVENT, RETENTION_EVENTS, NAME,
DESCRIPTION,
RETENTION_AUTHORITY,
RETENTION_INSTRUCTIONS,
RETENTION_PERIOD,
RETENTION_LOCATION,
RETENTION_PERIOD_PROPERTY,
RETENTION_GHOST,
RETENTION_ELIGIBLE_FIRST_EVENT,
RETENTION_EVENTS,
} }
@@ -483,8 +535,11 @@ public abstract class BaseAPI
*/ */
public enum RM_ACTIONS public enum RM_ACTIONS
{ {
EDIT_DISPOSITION_DATE("editDispositionActionAsOfDate"), CUT_OFF("cutoff"), UNDO_CUT_OFF("undoCutoff"), TRANSFER( EDIT_DISPOSITION_DATE("editDispositionActionAsOfDate"),
"transfer"), DESTROY("destroy"); CUT_OFF("cutoff"),
UNDO_CUT_OFF("undoCutoff"),
TRANSFER("transfer"),
DESTROY("destroy");
String action; String action;
private RM_ACTIONS(String action) private RM_ACTIONS(String action)
@@ -500,7 +555,10 @@ public abstract class BaseAPI
public enum PermissionType public enum PermissionType
{ {
SET_READ, REMOVE_READ, SET_READ_AND_FILE, REMOVE_READ_AND_FILE, SET_READ,
REMOVE_READ,
SET_READ_AND_FILE,
REMOVE_READ_AND_FILE,
} }
/** /**
@@ -513,7 +571,10 @@ public abstract class BaseAPI
public <K extends Enum<?>> String getPropertyValue(Map<K, String> properties, Enum<?> property) public <K extends Enum<?>> String getPropertyValue(Map<K, String> properties, Enum<?> property)
{ {
String value = properties.get(property); String value = properties.get(property);
if (value == null) { return ""; } if (value == null)
{
return "";
}
return value; return value;
} }
@@ -526,8 +587,7 @@ public abstract class BaseAPI
* @param property the property in the property map to check value for * @param property the property in the property map to check value for
* @return the json object used in request with the property with its value added if that is not null or empty * @return the json object used in request with the property with its value added if that is not null or empty
*/ */
protected <K extends Enum<?>> JSONObject addPropertyToRequest(JSONObject requestParams, String propertyRequestValue, protected <K extends Enum<?>> JSONObject addPropertyToRequest(JSONObject requestParams, String propertyRequestValue, Map<K, String> itemProperties, Enum<?> property) throws JSONException
Map<K, String> itemProperties, Enum<?> property) throws JSONException
{ {
String propertyValue = getPropertyValue(itemProperties, property); String propertyValue = getPropertyValue(itemProperties, property);
@@ -543,9 +603,9 @@ public abstract class BaseAPI
* *
* @param username the username with whom the delete is performed * @param username the username with whom the delete is performed
* @param password the user's password * @param password the user's password
* @param itemPath the path to the item eg. in case of a category it would be the "/" + category name, in case of a * @param itemPath the path to the item eg. in case of a category it would be the "/" + category name,
* folder or subCategory it would be /categoryName/folderName or /categoryName/subCategoryName/ in case * in case of a folder or subCategory it would be /categoryName/folderName or /categoryName/subCategoryName/
* of a record /categoryName/folderName/recordName * in case of a record /categoryName/folderName/recordName
* @return true if the deletion has been successful * @return true if the deletion has been successful
*/ */
protected boolean deleteItem(String username, String password, String itemPath) protected boolean deleteItem(String username, String password, String itemPath)