Search in sources :

Example 81 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project records-management by Alfresco.

the class WebScriptUtils method putValueToJSONObject.

/**
 * 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.", error);
    }
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONException(org.json.JSONException)

Example 82 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project records-management by Alfresco.

the class WebScriptUtils method getJSONArrayFromJSONObject.

/**
 * Gets the {@link JSONArray} value of a given key from a json object
 *
 * @param jsonObject The json object
 * @param key The key
 * @return The {@link JSONArray} value of the given key from the json object
 */
public static JSONArray getJSONArrayFromJSONObject(JSONObject jsonObject, String key) {
    JSONArray jsonArray;
    mandatory("jsonObject", jsonObject);
    mandatory("key", key);
    try {
        jsonArray = jsonObject.getJSONArray(key);
    } catch (JSONException error) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get the json array for the key '" + key + "'.", error);
    }
    return jsonArray;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 83 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project records-management by Alfresco.

the class WebScriptUtils method createJSONObject.

/**
 * Creates a json object from the given {@link String}
 *
 * @param json The json object as {@link String}
 * @return The json object created from the given {@link String}
 */
public static JSONObject createJSONObject(String json) {
    mandatory("json", json);
    JSONObject jsonObject;
    try {
        jsonObject = new JSONObject(json);
    } catch (JSONException error) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot create a json object from the given string '" + json + "'.", error);
    }
    return jsonObject;
}
Also used : JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONException(org.json.JSONException)

Example 84 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project records-management by Alfresco.

the class WebScriptUtils method createJSONArray.

/**
 * Creates a json array from the given {@link String}
 *
 * @param json The json array as {@link String}
 * @return The json array created from the given {@link String}
 */
public static JSONArray createJSONArray(String json) {
    mandatory("json", json);
    JSONArray jsonArray;
    try {
        jsonArray = new JSONArray(json);
    } catch (JSONException error) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot create a json array from the given string '" + json + "'.", error);
    }
    return jsonArray;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 85 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project records-management by Alfresco.

the class AuditLogPost method getDestination.

/**
 * Helper method to get the destination from the json object
 *
 * @param json The json object which was created from the request content
 * @return {@link NodeRef} The destination of the audit log
 */
private NodeRef getDestination(JSONObject json) {
    if (!json.has(PARAM_DESTINATION)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory parameter '" + PARAM_DESTINATION + "' has not been supplied.");
    }
    String destinationParam;
    try {
        destinationParam = json.getString(PARAM_DESTINATION);
    } catch (JSONException error) {
        throw new AlfrescoRuntimeException("Error extracting 'destination' from parameter.", error);
    }
    if (StringUtils.isBlank(destinationParam)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Please select a record folder.");
    }
    NodeRef destination = new NodeRef(destinationParam);
    if (!nodeService.exists(destination)) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Selected node does not exist");
    }
    // ensure the node is a record folder
    if (!RecordsManagementModel.TYPE_RECORD_FOLDER.equals(nodeService.getType(destination))) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Selected node is not a record folder");
    }
    // ensure the record folder is not closed
    if (recordFolderService.isRecordFolderClosed(destination)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot file into a closed folder.");
    }
    // ensure the record folder is not cut off
    if (dispositionService.isDisposableItemCutoff(destination)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot file into a cut off folder.");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Filing audit trail as record in record folder: '" + destination + "'.");
    }
    return destination;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONException(org.json.JSONException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

WebScriptException (org.springframework.extensions.webscripts.WebScriptException)204 HashMap (java.util.HashMap)94 NodeRef (org.alfresco.service.cmr.repository.NodeRef)67 IOException (java.io.IOException)60 JSONException (org.json.JSONException)48 JSONObject (org.json.JSONObject)44 ArrayList (java.util.ArrayList)32 QName (org.alfresco.service.namespace.QName)31 JSONTokener (org.json.JSONTokener)29 JSONObject (org.json.simple.JSONObject)25 JSONArray (org.json.JSONArray)18 Map (java.util.Map)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)11 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)11 StoreRef (org.alfresco.service.cmr.repository.StoreRef)10 File (java.io.File)9 Date (java.util.Date)8 JSONParser (org.json.simple.parser.JSONParser)8 Serializable (java.io.Serializable)7 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)7