Search in sources :

Example 76 with WebScriptException

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

the class CapabilitiesGet method executeImpl.

/**
 * @see org.alfresco.repo.web.scripts.content.StreamContent#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) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String storeType = templateVars.get("store_type");
    String storeId = templateVars.get("store_id");
    String nodeId = templateVars.get("id");
    NodeRef nodeRef = null;
    if (StringUtils.isNotBlank(storeType) && StringUtils.isNotBlank(storeId) && StringUtils.isNotBlank(nodeId)) {
        nodeRef = new NodeRef(new StoreRef(storeType, storeId), nodeId);
    } else {
        // we are talking about the file plan node
        // TODO we are making the assumption there is only one file plan here!
        nodeRef = filePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID);
        if (nodeRef == null) {
            throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "The default file plan node could not be found.");
        }
    }
    boolean grouped = false;
    String groupedString = req.getParameter("grouped");
    if (StringUtils.isNotBlank(groupedString)) {
        grouped = Boolean.parseBoolean(groupedString);
    }
    Map<String, Object> model = new TreeMap<String, Object>();
    if (grouped) {
        // Construct the map which is needed to build the model
        Map<String, GroupedCapabilities> groupedCapabilitiesMap = new TreeMap<String, GroupedCapabilities>();
        List<Group> groups = capabilityService.getGroups();
        for (Group group : groups) {
            String capabilityGroupTitle = group.getTitle();
            if (StringUtils.isNotBlank(capabilityGroupTitle)) {
                String capabilityGroupId = group.getId();
                List<Capability> capabilities = capabilityService.getCapabilitiesByGroupId(capabilityGroupId);
                for (Capability capability : capabilities) {
                    String capabilityName = capability.getName();
                    String capabilityTitle = capability.getTitle();
                    if (groupedCapabilitiesMap.containsKey(capabilityGroupId)) {
                        groupedCapabilitiesMap.get(capabilityGroupId).addCapability(capabilityName, capabilityTitle);
                    } else {
                        GroupedCapabilities groupedCapabilities = new GroupedCapabilities(capabilityGroupId, capabilityGroupTitle, capabilityName, capabilityTitle);
                        groupedCapabilities.addCapability(capabilityName, capabilityTitle);
                        groupedCapabilitiesMap.put(capabilityGroupId, groupedCapabilities);
                    }
                }
            }
        }
        model.put("groupedCapabilities", groupedCapabilitiesMap);
    } else {
        boolean includePrivate = false;
        String includePrivateString = req.getParameter("includeAll");
        if (StringUtils.isNotBlank(includePrivateString)) {
            includePrivate = Boolean.parseBoolean(includePrivateString);
        }
        Map<Capability, AccessStatus> map = capabilityService.getCapabilitiesAccessState(nodeRef, includePrivate);
        List<String> list = new ArrayList<String>(map.size());
        for (Map.Entry<Capability, AccessStatus> entry : map.entrySet()) {
            AccessStatus accessStatus = entry.getValue();
            if (!AccessStatus.DENIED.equals(accessStatus)) {
                Capability capability = entry.getKey();
                list.add(capability.getName());
            }
        }
        model.put("capabilities", list);
    }
    return model;
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) Group(org.alfresco.module.org_alfresco_module_rm.capability.Group) Capability(org.alfresco.module.org_alfresco_module_rm.capability.Capability) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) AccessStatus(org.alfresco.service.cmr.security.AccessStatus) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 77 with WebScriptException

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

the class RMSavedSearchesPost method executeImpl.

/**
 * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
 */
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    // Get the site id and confirm it is valid
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String siteId = templateVars.get("site");
    if (siteId == null || siteId.length() == 0) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Site id not provided.");
    }
    if (siteService.getSite(siteId) == null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Site not found.");
    }
    try {
        // Parse the JSON passed in the request
        JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Get the details of the saved search
        if (!json.has("name")) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'name' parameter was not provided in request body");
        }
        String name = json.getString("name");
        String description = null;
        if (json.has("description")) {
            description = json.getString("description");
        }
        boolean isPublic = true;
        if (json.has("public")) {
            isPublic = json.getBoolean("public");
        }
        // NOTE: we do not need to worry about the query
        if (!json.has("params")) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'params' parameter was not provided in request body");
        }
        String params = json.getString("params");
        String sort = null;
        if (json.has("sort")) {
            sort = json.getString("sort");
        }
        // Use the compatibility class to create a saved search details and save
        String search = SavedSearchDetailsCompatibility.getSearchFromParams(params);
        if (search == null) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'terms' was not provided in 'params' parameter found in the request body");
        }
        RecordsManagementSearchParameters searchParameters = SavedSearchDetailsCompatibility.createSearchParameters(params, sort, namespaceService);
        recordsManagementSearchService.saveSearch(siteId, name, description, search, searchParameters, isPublic);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    }
    // Indicate success in the model
    Map<String, Object> model = new HashMap<String, Object>(1);
    model.put("success", true);
    return model;
}
Also used : JSONTokener(org.json.JSONTokener) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) RecordsManagementSearchParameters(org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 78 with WebScriptException

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

the class WebScriptUtils method getRequestParameterValue.

/**
 * Gets the value of a request parameter
 *
 * @param req The webscript request
 * @param parameter The request parameter
 * @param checkValue Determines if the value of the parameter should be checked or not
 * @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;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ParameterCheck.mandatoryString(org.alfresco.util.ParameterCheck.mandatoryString)

Example 79 with WebScriptException

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

the class WebScriptUtils method getRequestContentAsJSONObject.

/**
 * 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 (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;
}
Also used : JSONTokener(org.json.JSONTokener) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) JSONException(org.json.JSONException) ParameterCheck.mandatoryString(org.alfresco.util.ParameterCheck.mandatoryString) IOException(java.io.IOException)

Example 80 with WebScriptException

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

the class WebScriptUtils method getStringValueFromJSONObject.

/**
 * 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 = null;
    try {
        value = jsonObject.getString(key);
        if (checkValue && isBlank(value)) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The value is missing for the key '" + key + "'.");
        }
    } catch (JSONException error) {
        if (checkValue) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get value for the key '" + key + "'.", error);
        }
    }
    return value;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONException(org.json.JSONException) ParameterCheck.mandatoryString(org.alfresco.util.ParameterCheck.mandatoryString)

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