Search in sources :

Example 91 with WebScriptException

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

the class BaseHold method getItemNodeRefs.

/**
 * Helper method to get the {@link NodeRef}s for the items(s) (record(s) / record folder(s)) which will be added to the hold(s)
 *
 * @param json The request content as JSON object
 * @return List of item {@link NodeRef}s which will be added to the hold(s)
 */
protected List<NodeRef> getItemNodeRefs(JSONObject json) {
    List<NodeRef> nodeRefs = new ArrayList<NodeRef>();
    try {
        JSONArray nodeRefsArray = json.getJSONArray("nodeRefs");
        for (int i = 0; i < nodeRefsArray.length(); i++) {
            NodeRef nodeReference = new NodeRef(nodeRefsArray.getString(i));
            checkItemNodeRef(nodeReference);
            nodeRefs.add(nodeReference);
        }
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not get information from the json array.", je);
    }
    return nodeRefs;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 92 with WebScriptException

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

the class RMSavedSearchesGet 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) {
    // create model object with the lists model
    Map<String, Object> model = new HashMap<String, Object>(13);
    // 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.");
    }
    // Get the saved search details
    List<SavedSearchDetails> details = recordsManagementSearchService.getSavedSearches(siteId);
    List<Item> items = new ArrayList<Item>();
    for (SavedSearchDetails savedSearchDetails : details) {
        String name = savedSearchDetails.getName();
        String description = savedSearchDetails.getDescription();
        String query = savedSearchDetails.getCompatibility().getQuery();
        String params = savedSearchDetails.getCompatibility().getParams();
        String sort = savedSearchDetails.getCompatibility().getSort();
        Item item = new Item(name, description, query, params, sort);
        items.add(item);
    }
    model.put("savedSearches", items);
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SavedSearchDetails(org.alfresco.module.org_alfresco_module_rm.search.SavedSearchDetails)

Example 93 with WebScriptException

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

the class RmClassesGet method executeImpl.

/**
 * Execute custom Java logic
 *
 * @param req  Web Script request
 * @param isRM  indicates whether the request comes from an RM site or not
 * @return custom service model
 */
private Map<String, Object> executeImpl(WebScriptRequest req, boolean isRM) {
    String classFilter = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_CLASS_FILTER));
    String namespacePrefix = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX));
    String name = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_NAME));
    String className = null;
    Map<QName, ClassDefinition> classdef = new HashMap<QName, ClassDefinition>();
    Map<QName, Collection<PropertyDefinition>> propdef = new HashMap<QName, Collection<PropertyDefinition>>();
    Map<QName, Collection<AssociationDefinition>> assocdef = new HashMap<QName, Collection<AssociationDefinition>>();
    Map<String, Object> model = new HashMap<String, Object>();
    List<QName> qnames = new ArrayList<QName>();
    QName classQname = null;
    QName myModel = null;
    // if classfilter is not given, then it defaults to all
    if (classFilter == null) {
        classFilter = "all";
    }
    // validate classfilter
    if (!isValidClassFilter(classFilter)) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the classfilter - " + classFilter + " provided in the URL");
    }
    // name alone has no meaning without namespaceprefix
    if (namespacePrefix == null && name != null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Missing namespaceprefix parameter in the URL - both combination of name and namespaceprefix is needed");
    }
    // validate the namespaceprefix and name parameters => if namespaceprefix is given, then name has to be validated along with it
    if (namespacePrefix != null) {
        // validate name parameter if present along with the namespaceprefix
        if (name != null) {
            className = namespacePrefix + "_" + name;
            if (!isValidClassname(className)) {
                throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the name - " + name + "parameter in the URL");
            }
            classQname = QName.createQName(getFullNamespaceURI(className));
            classdef.put(classQname, this.dictionaryservice.getClass(classQname));
            propdef.put(classQname, this.dictionaryservice.getClass(classQname).getProperties().values());
            assocdef.put(classQname, this.dictionaryservice.getClass(classQname).getAssociations().values());
        } else {
            // if name is not given then the model is extracted from the namespaceprefix, there can be more than one model associated with one namespaceprefix
            String namespaceUri = namespaceService.getNamespaceURI(namespacePrefix);
            for (QName qnameObj : this.dictionaryservice.getAllModels()) {
                if (qnameObj.getNamespaceURI().equals(namespaceUri)) {
                    name = qnameObj.getLocalName();
                    myModel = QName.createQName(getFullNamespaceURI(namespacePrefix + "_" + name));
                    // check the classfilter to pull out either all or type or aspects
                    if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE1)) {
                        qnames.addAll(this.dictionaryservice.getAspects(myModel));
                        qnames.addAll(this.dictionaryservice.getTypes(myModel));
                    } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE3)) {
                        qnames.addAll(this.dictionaryservice.getTypes(myModel));
                    } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE2)) {
                        qnames.addAll(this.dictionaryservice.getAspects(myModel));
                    }
                }
            }
        }
    }
    // if namespacePrefix is null, then check the class filter to pull out either all, type or aspects
    if (myModel == null) {
        if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE1)) {
            qnames.addAll(getAspects(isRM));
            qnames.addAll(getTypes(isRM));
        } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE3)) {
            qnames.addAll(getTypes(isRM));
        } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE2)) {
            qnames.addAll(getAspects(isRM));
        }
    }
    if (classdef.isEmpty()) {
        for (QName qnameObj : qnames) {
            classdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj));
            propdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getProperties().values());
            assocdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getAssociations().values());
        }
    }
    List<ClassDefinition> classDefinitions = new ArrayList<ClassDefinition>(classdef.values());
    Collections.sort(classDefinitions, new DictionaryComparators.ClassDefinitionComparator(dictionaryservice));
    model.put(MODEL_PROP_KEY_CLASS_DEFS, classDefinitions);
    model.put(MODEL_PROP_KEY_PROPERTY_DETAILS, propdef.values());
    model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, assocdef.values());
    model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, dictionaryservice);
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Collection(java.util.Collection)

Example 94 with WebScriptException

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

the class DynamicAuthoritiesGet method getParentNodeRefParameter.

/**
 * Get parentNodeRef parameter from the request
 *
 * @param req
 * @return
 */
protected NodeRef getParentNodeRefParameter(WebScriptRequest req) {
    String parentNodeRefStr = req.getParameter(PARAM_PARENT_NODE_REF);
    NodeRef parentNodeRef = null;
    if (StringUtils.isNotBlank(parentNodeRefStr)) {
        parentNodeRef = new NodeRef(parentNodeRefStr);
        if (!nodeService.exists(parentNodeRef)) {
            String message = MessageFormat.format(MESSAGE_NODE_REF_DOES_NOT_EXIST_TEMPLATE, parentNodeRef.toString());
            logger.info(message);
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, message);
        }
    }
    return parentNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 95 with WebScriptException

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

the class EmailMapPost 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) {
    Map<String, Object> model = new HashMap<String, Object>(1);
    try {
        // Get the data from the content
        JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Add custom mapping
        customEmailMappingService.addCustomMapping(json.getString("from"), json.getString("to"));
        // Add the lists of custom mappings to the model
        model.put("emailmap", customEmailMappingService.getCustomMappings());
    } 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);
    } catch (AlfrescoRuntimeException are) {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, are.getMessage(), are);
    }
    return model;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) JSONException(org.json.JSONException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

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