mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-1641 (Create Relationship Service)
* Added a new utility class to reduce boilerplate code git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@85481 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -18,9 +18,8 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import static org.alfresco.util.WebScriptUtils.getTemplateVars;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -29,13 +28,7 @@ import org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
@@ -118,151 +111,4 @@ public abstract class AbstractRmWebScript extends DeclarativeWebScript
|
||||
|
||||
return nodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks if the json object contains an entry with the specified name.
|
||||
*
|
||||
* @param json the json object.
|
||||
* @param paramName the name to check for.
|
||||
* @throws WebScriptException if the specified entry is missing.
|
||||
*/
|
||||
protected void checkMandatoryJsonParam(JSONObject json, String paramName)
|
||||
{
|
||||
if (!json.has(paramName))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
|
||||
"Mandatory '" + paramName + "' parameter was not provided in request body");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks if the json object contains entries with the specified names.
|
||||
*
|
||||
* @param json the json object.
|
||||
* @param paramNames the names to check for.
|
||||
* @throws WebScriptException if any of the specified entries are missing.
|
||||
*/
|
||||
protected void checkMandatoryJsonParams(JSONObject json, List<String> paramNames)
|
||||
{
|
||||
for (String name : paramNames)
|
||||
{
|
||||
checkMandatoryJsonParam(json, name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template variable substitutions map
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The template variable substitutions
|
||||
*/
|
||||
protected Map<String, String> getTemplateVars(WebScriptRequest req)
|
||||
{
|
||||
if (req == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The webscript request is null.");
|
||||
}
|
||||
|
||||
if (req.getServiceMatch() == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The matching API Service for the request is null.");
|
||||
}
|
||||
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
if (templateVars == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The template variable substitutions map is null");
|
||||
}
|
||||
|
||||
return templateVars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a request parameter
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @param parameter The request parameter
|
||||
* @return The value of the request parameter
|
||||
*/
|
||||
protected String getRequestParameterValue(WebScriptRequest req, String parameter)
|
||||
{
|
||||
Map<String, String> templateVars = getTemplateVars(req);
|
||||
return templateVars.get(parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the request content as JSON object
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The request content as JSON object
|
||||
*/
|
||||
protected JSONObject getRequestContentAsJsonObject(WebScriptRequest req)
|
||||
{
|
||||
Content reqContent = req.getContent();
|
||||
if (reqContent == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing request body.");
|
||||
}
|
||||
|
||||
String content;
|
||||
try
|
||||
{
|
||||
content = reqContent.getContent();
|
||||
}
|
||||
catch (IOException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get conent from the request.", error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(content))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Content does not exist.");
|
||||
}
|
||||
|
||||
JSONTokener jsonTokener;
|
||||
try
|
||||
{
|
||||
jsonTokener = new JSONTokener(req.getContent().getContent());
|
||||
}
|
||||
catch (IOException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get content.", error);
|
||||
}
|
||||
|
||||
JSONObject json;
|
||||
try
|
||||
{
|
||||
json = new JSONObject(jsonTokener);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request body.", error);
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a given key from a json object
|
||||
*
|
||||
* @param jsonObject The json object from which the value should be retrieved
|
||||
* @param key The key for which the value is requested
|
||||
* @return The value of the given key from the json object
|
||||
*/
|
||||
protected Serializable getJSONObjectValue(JSONObject jsonObject, String key)
|
||||
{
|
||||
Serializable value;
|
||||
|
||||
try
|
||||
{
|
||||
checkMandatoryJsonParam(jsonObject, key);
|
||||
value = (Serializable) jsonObject.get(key);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get value for the key '" + key + "'.", error);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
@@ -18,14 +18,13 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script;
|
||||
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* Base class for custom reference definition classes
|
||||
@@ -48,9 +47,6 @@ public class CustomReferenceDefinitionBase extends AbstractRmWebScript
|
||||
/** Records Management Admin Service */
|
||||
private RecordsManagementAdminService rmAdminService;
|
||||
|
||||
/** Dictionary Service */
|
||||
private DictionaryService dictionaryService;
|
||||
|
||||
/**
|
||||
* Sets the records management admin service
|
||||
*
|
||||
@@ -71,26 +67,6 @@ public class CustomReferenceDefinitionBase extends AbstractRmWebScript
|
||||
return this.rmAdminService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the dictionary service
|
||||
*
|
||||
* @param dictionaryService The dictionary service
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dictionary service instance
|
||||
*
|
||||
* @return The dictionary service instance
|
||||
*/
|
||||
protected DictionaryService getDictionaryService()
|
||||
{
|
||||
return this.dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the QName for the given custom reference id
|
||||
*
|
||||
@@ -121,22 +97,7 @@ public class CustomReferenceDefinitionBase extends AbstractRmWebScript
|
||||
*/
|
||||
protected CustomReferenceType getCustomReferenceType(JSONObject requestContent)
|
||||
{
|
||||
String referenceType = (String) getJSONObjectValue(requestContent, REFERENCE_TYPE);
|
||||
if (StringUtils.isBlank(referenceType))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Reference type is missing.");
|
||||
}
|
||||
String referenceType = getStringValueFromJSONObject(requestContent, REFERENCE_TYPE);
|
||||
return CustomReferenceType.getEnumFromString(referenceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the service path from the webscript request
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The service path
|
||||
*/
|
||||
protected String getServicePath(WebScriptRequest req)
|
||||
{
|
||||
return req.getServicePath();
|
||||
}
|
||||
}
|
||||
|
@@ -18,11 +18,13 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script;
|
||||
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestContentAsJsonObject;
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
@@ -49,7 +51,7 @@ public class CustomReferenceDefinitionPost extends CustomReferenceDefinitionBase
|
||||
QName customReference = addCustomReference(requestContent, customReferenceType);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
String servicePath = getServicePath(req);
|
||||
String servicePath = req.getServicePath();
|
||||
Map<String, Object> customReferenceData = getCustomReferenceData(customReferenceType, customReference, servicePath);
|
||||
model.putAll(customReferenceData);
|
||||
|
||||
@@ -69,28 +71,13 @@ public class CustomReferenceDefinitionPost extends CustomReferenceDefinitionBase
|
||||
|
||||
if (CustomReferenceType.PARENT_CHILD.equals(customReferenceType))
|
||||
{
|
||||
String source = (String) getJSONObjectValue(requestContent, SOURCE);
|
||||
if (StringUtils.isBlank(source))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Source is blank.");
|
||||
}
|
||||
|
||||
String target = (String) getJSONObjectValue(requestContent, TARGET);
|
||||
if (StringUtils.isBlank(target))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Target is blank.");
|
||||
}
|
||||
|
||||
String source = getStringValueFromJSONObject(requestContent, SOURCE);
|
||||
String target = getStringValueFromJSONObject(requestContent, TARGET);
|
||||
referenceQName = getRmAdminService().addCustomChildAssocDefinition(source, target);
|
||||
}
|
||||
else if (CustomReferenceType.BIDIRECTIONAL.equals(customReferenceType))
|
||||
{
|
||||
String label = (String) getJSONObjectValue(requestContent, LABEL);
|
||||
if (StringUtils.isBlank(label))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Label is blank.");
|
||||
}
|
||||
|
||||
String label = getStringValueFromJSONObject(requestContent, LABEL);
|
||||
referenceQName = getRmAdminService().addCustomAssocDefinition(label);
|
||||
}
|
||||
else
|
||||
|
@@ -18,11 +18,14 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script;
|
||||
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestContentAsJsonObject;
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestParameterValue;
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
@@ -46,33 +49,17 @@ public class CustomReferenceDefinitionPut extends CustomReferenceDefinitionBase
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
JSONObject requestContent = getRequestContentAsJsonObject(req);
|
||||
String referenceId = getReferenceId(req);
|
||||
String referenceId = getRequestParameterValue(req, REF_ID);
|
||||
updateCustomReference(requestContent, referenceId);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
String servicePath = getServicePath(req);
|
||||
String servicePath = req.getServicePath();
|
||||
Map<String, Object> customReferenceData = getCustomReferenceData(servicePath, referenceId);
|
||||
model.putAll(customReferenceData);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reference id from the webscript request
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The reference id
|
||||
*/
|
||||
private String getReferenceId(WebScriptRequest req)
|
||||
{
|
||||
String referenceId = getRequestParameterValue(req, REF_ID);
|
||||
if (StringUtils.isBlank(referenceId))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Reference id is blank.");
|
||||
}
|
||||
return referenceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the custom reference
|
||||
*
|
||||
@@ -86,28 +73,13 @@ public class CustomReferenceDefinitionPut extends CustomReferenceDefinitionBase
|
||||
|
||||
if (CustomReferenceType.PARENT_CHILD.equals(customReferenceType))
|
||||
{
|
||||
String source = (String) getJSONObjectValue(requestContent, SOURCE);
|
||||
if (StringUtils.isBlank(source))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Source is blank.");
|
||||
}
|
||||
|
||||
String target = (String) getJSONObjectValue(requestContent, TARGET);
|
||||
if (StringUtils.isBlank(target))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Target is blank.");
|
||||
}
|
||||
|
||||
String source = getStringValueFromJSONObject(requestContent, SOURCE);
|
||||
String target = getStringValueFromJSONObject(requestContent, TARGET);
|
||||
getRmAdminService().updateCustomChildAssocDefinition(referenceQName, source, target);
|
||||
}
|
||||
else if (CustomReferenceType.BIDIRECTIONAL.equals(customReferenceType))
|
||||
{
|
||||
String label = (String) getJSONObjectValue(requestContent, LABEL);
|
||||
if (StringUtils.isBlank(label))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Label is blank.");
|
||||
}
|
||||
|
||||
String label = getStringValueFromJSONObject(requestContent, LABEL);
|
||||
getRmAdminService().updateCustomAssocDefinition(referenceQName, label);
|
||||
}
|
||||
else
|
||||
|
@@ -18,6 +18,8 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script;
|
||||
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestParameterValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -26,6 +28,7 @@ import java.util.Map.Entry;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
@@ -41,13 +44,36 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
*/
|
||||
public class CustomReferenceDefinitionsGet extends CustomReferenceDefinitionBase
|
||||
{
|
||||
/** Dictionary Service */
|
||||
private DictionaryService dictionaryService;
|
||||
|
||||
/**
|
||||
* Sets the dictionary service
|
||||
*
|
||||
* @param dictionaryService The dictionary service
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dictionary service instance
|
||||
*
|
||||
* @return The dictionary service instance
|
||||
*/
|
||||
protected DictionaryService getDictionaryService()
|
||||
{
|
||||
return this.dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
String referenceId = getRequestParameterValue(req, REF_ID);
|
||||
String referenceId = getRequestParameterValue(req, REF_ID, false);
|
||||
Map<QName, AssociationDefinition> customReferenceDefinitions = getCustomReferenceDefinitions(referenceId);
|
||||
List<Map<String, String>> customReferenceData = getCustomReferenceData(customReferenceDefinitions);
|
||||
|
||||
|
@@ -18,7 +18,9 @@
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.slingshot;
|
||||
|
||||
import java.io.IOException;
|
||||
import static org.alfresco.util.WebScriptUtils.getRequestContentAsJsonObject;
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -26,12 +28,9 @@ import org.alfresco.module.org_alfresco_module_rm.script.AbstractRmWebScript;
|
||||
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionPolicy;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
@@ -65,30 +64,8 @@ public class RecordedVersionConfigPost extends AbstractRmWebScript implements Re
|
||||
*/
|
||||
private RecordableVersionPolicy getRecordableVersionPolicy(WebScriptRequest req)
|
||||
{
|
||||
String recordedVersion = getRecordedVersion(req);
|
||||
JSONObject requestContent = getRequestContentAsJsonObject(req);
|
||||
String recordedVersion = getStringValueFromJSONObject(requestContent, RECORDED_VERSION);
|
||||
return RecordableVersionPolicy.valueOf(recordedVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the recorded version parameter value from the request
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The recorded version parameter value
|
||||
*/
|
||||
private String getRecordedVersion(WebScriptRequest req)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Convert the request content to JSON
|
||||
String content = req.getContent().getContent();
|
||||
JSONObject jsonObject = new JSONObject(new JSONTokener(content));
|
||||
checkMandatoryJsonParam(jsonObject, RECORDED_VERSION);
|
||||
return jsonObject.getString(RECORDED_VERSION);
|
||||
}
|
||||
catch (JSONException | IOException ex)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
|
||||
"Could not parse JSON from req.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
295
rm-server/source/java/org/alfresco/util/WebScriptUtils.java
Normal file
295
rm-server/source/java/org/alfresco/util/WebScriptUtils.java
Normal file
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.util;
|
||||
|
||||
import static org.alfresco.util.ParameterCheck.mandatory;
|
||||
import static org.alfresco.util.ParameterCheck.mandatoryString;
|
||||
import static org.apache.commons.lang.StringUtils.isBlank;
|
||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* Utility class for handling webscript requests
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.3
|
||||
*/
|
||||
public final class WebScriptUtils
|
||||
{
|
||||
private WebScriptUtils()
|
||||
{
|
||||
// Will not be called
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template variable substitutions map
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The template variable substitutions
|
||||
*/
|
||||
public static Map<String, String> getTemplateVars(WebScriptRequest req)
|
||||
{
|
||||
mandatory("req", req);
|
||||
|
||||
if (req.getServiceMatch() == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The matching API Service for the request is null.");
|
||||
}
|
||||
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
if (templateVars == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The template variable substitutions map is null");
|
||||
}
|
||||
|
||||
return templateVars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a request parameter
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @param parameter The request parameter
|
||||
* @return The value of the request parameter
|
||||
*/
|
||||
public static String getRequestParameterValue(WebScriptRequest req, String parameter)
|
||||
{
|
||||
mandatory("req", req);
|
||||
mandatoryString("parameter", parameter);
|
||||
|
||||
return getRequestParameterValue(req, parameter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a request parameter
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @param parameter The request parameter
|
||||
* @param checkValue FIXME!!!
|
||||
* @return The value of the request parameter
|
||||
*/
|
||||
public static String getRequestParameterValue(WebScriptRequest req, String parameter, boolean checkValue)
|
||||
{
|
||||
mandatory("req", req);
|
||||
mandatoryString("parameter", parameter);
|
||||
|
||||
Map<String, String> templateVars = getTemplateVars(req);
|
||||
String value = templateVars.get(parameter);
|
||||
|
||||
if (checkValue && isBlank(value))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_NOT_FOUND, "The value for the parameter '" + parameter + "' is blank.");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the request content as JSON object
|
||||
*
|
||||
* @param req The webscript request
|
||||
* @return The request content as JSON object
|
||||
*/
|
||||
public static JSONObject getRequestContentAsJsonObject(WebScriptRequest req)
|
||||
{
|
||||
mandatory("req", req);
|
||||
|
||||
Content reqContent = req.getContent();
|
||||
if (reqContent == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing request body.");
|
||||
}
|
||||
|
||||
String content;
|
||||
try
|
||||
{
|
||||
content = reqContent.getContent();
|
||||
}
|
||||
catch (IOException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get content from the request.", error);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(content))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Content does not exist.");
|
||||
}
|
||||
|
||||
JSONTokener jsonTokener = new JSONTokener(content);
|
||||
|
||||
JSONObject json;
|
||||
try
|
||||
{
|
||||
json = new JSONObject(jsonTokener);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unable to parse request body.", error);
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the json object contains an entry with the specified parameter name
|
||||
*
|
||||
* @param jsonObject The json object
|
||||
* @param paramName The parameter name to check for
|
||||
*/
|
||||
public static void checkMandatoryJsonParam(JSONObject jsonObject, String paramName)
|
||||
{
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatoryString("paramName", paramName);
|
||||
|
||||
if (!jsonObject.has(paramName))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The json object does not contain en entry with parameter '" + paramName + "'.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the json object contains entries with the specified parameter names
|
||||
*
|
||||
* @param jsonObject The json object.
|
||||
* @param paramNames The parameter names to check for
|
||||
*/
|
||||
public static void checkMandatoryJsonParams(JSONObject jsonObject, List<String> paramNames)
|
||||
{
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatory("paramNames", paramNames);
|
||||
|
||||
for (String name : paramNames)
|
||||
{
|
||||
checkMandatoryJsonParam(jsonObject, name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link String} value of a given key from a json object
|
||||
*
|
||||
* @param jsonObject The json object
|
||||
* @param key The key
|
||||
* @return The {@link String} value of the given key from the json object
|
||||
*/
|
||||
public static String getStringValueFromJSONObject(JSONObject jsonObject, String key)
|
||||
{
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatoryString("key", key);
|
||||
|
||||
return getStringValueFromJSONObject(jsonObject, key, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link String} value of a given key from a json object
|
||||
*
|
||||
* @param jsonObject The json object
|
||||
* @param key The key
|
||||
* @param checkKey Determines if the existence of the key should be checked
|
||||
* @param checkValue Determines if the value should be checked if it is blank or not
|
||||
* @return The {@link String} value of the given key from the json object
|
||||
*/
|
||||
public static String getStringValueFromJSONObject(JSONObject jsonObject, String key, boolean checkKey, boolean checkValue)
|
||||
{
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatoryString("key", key);
|
||||
|
||||
if (checkKey)
|
||||
{
|
||||
checkMandatoryJsonParam(jsonObject, key);
|
||||
}
|
||||
|
||||
String value;
|
||||
|
||||
try
|
||||
{
|
||||
value = jsonObject.getString(key);
|
||||
if (checkValue && isBlank(value))
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The vale is missing for the key '" + key + "'.");
|
||||
}
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get value for the key '" + key + "'.", error);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the given key and value to the json object
|
||||
*
|
||||
* @param jsonObject The json object
|
||||
* @param key The key
|
||||
* @param value The value
|
||||
*/
|
||||
public static void putValuetoJSONObject(JSONObject jsonObject, String key, Object value)
|
||||
{
|
||||
mandatory("jsonObject", jsonObject);
|
||||
mandatoryString("key", key);
|
||||
mandatory("value", value);
|
||||
|
||||
try
|
||||
{
|
||||
jsonObject.put(key, value);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not put the key '" + key + "' with the value '" + value + "' to the json object.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of an element from a json array at the given index
|
||||
*
|
||||
* @param jsonArray The json array
|
||||
* @param index The index
|
||||
* @return The value of the element
|
||||
*/
|
||||
public static Object getJSONArrayValue(JSONArray jsonArray, int index)
|
||||
{
|
||||
mandatory("jsonArray", jsonArray);
|
||||
|
||||
Object value;
|
||||
|
||||
try
|
||||
{
|
||||
value = jsonArray.get(index);
|
||||
}
|
||||
catch (JSONException error)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get value for the index '" + index + "' from the JSON Array.", error);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user