Search in sources :

Example 46 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class ForumTopicsGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // They shouldn't be trying to list of an existing Post or Topic
    if (topic != null || post != null) {
        String error = "Can't list Topics inside an existing Topic or Post";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Do we need to list or search?
    boolean tagSearch = false;
    String tag = req.getParameter("tag");
    if (tag != null && tag.length() > 0) {
        tagSearch = true;
        // Tags can be full unicode strings, so decode
        tag = URLDecoder.decode(tag);
    }
    // Get the topics
    boolean oldestTopicsFirst = false;
    PagingResults<TopicInfo> topics = null;
    PagingRequest paging = buildPagingRequest(req);
    if (tagSearch) {
        // Tag based is a search rather than a listing
        if (site != null) {
            topics = discussionService.findTopics(site.getShortName(), null, tag, oldestTopicsFirst, paging);
        } else {
            topics = discussionService.findTopics(nodeRef, null, tag, oldestTopicsFirst, paging);
        }
    } else {
        if (site != null) {
            topics = discussionService.listTopics(site.getShortName(), oldestTopicsFirst, paging);
        } else {
            topics = discussionService.listTopics(nodeRef, oldestTopicsFirst, buildPagingRequest(req));
        }
    }
    // been created yet, use the site for the permissions checking
    if (site != null && nodeRef == null) {
        nodeRef = site.getNodeRef();
    }
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    model.put("forum", nodeRef);
    // Have the topics rendered
    model.put("data", renderTopics(topics, paging, site));
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) PagingRequest(org.alfresco.query.PagingRequest)

Example 47 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class ForumTopicsRecentGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // They shouldn't be trying to list of an existing Post or Topic
    if (topic != null || post != null) {
        String error = "Can't list Topics inside an existing Topic or Post";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Grab the date range to search over
    String numDaysS = req.getParameter("numdays");
    int numDays = RECENT_SEARCH_PERIOD_DAYS;
    if (numDaysS != null) {
        numDays = Integer.parseInt(numDaysS);
    }
    Date now = new Date();
    Date from = new Date(now.getTime() - numDays * ONE_DAY_MS);
    Date to = new Date(now.getTime() + ONE_DAY_MS);
    // Get the recent topics, newest first
    PagingResults<TopicInfo> topics = null;
    PagingRequest paging = buildPagingRequest(req);
    if (site != null) {
        topics = discussionService.listTopics(site.getShortName(), from, to, false, paging);
    } else {
        topics = discussionService.listTopics(nodeRef, from, to, false, paging);
    }
    // been created yet, use the site for the permissions checking
    if (site != null && nodeRef == null) {
        nodeRef = site.getNodeRef();
    }
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    model.put("forum", nodeRef);
    // Have the topics rendered
    model.put("data", renderTopics(topics, paging, site));
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) Date(java.util.Date) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) PagingRequest(org.alfresco.query.PagingRequest)

Example 48 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class AbstractDocLink method parseNodeRefFromTemplateArgs.

protected NodeRef parseNodeRefFromTemplateArgs(Map<String, String> templateVars) {
    if (templateVars == null) {
        return null;
    }
    String storeTypeArg = templateVars.get(PARAM_STORE_TYPE);
    String storeIdArg = templateVars.get(PARAM_STORE_ID);
    String idArg = templateVars.get(PARAM_ID);
    if (storeTypeArg != null) {
        ParameterCheck.mandatoryString("storeTypeArg", storeTypeArg);
        ParameterCheck.mandatoryString("storeIdArg", storeIdArg);
        ParameterCheck.mandatoryString("idArg", idArg);
        /*
             * NodeRef based request
             * <url>URL_BASE/{store_type}/{store_id}/{id}</url>
             */
        return new NodeRef(storeTypeArg, storeIdArg, idArg);
    } else {
        String siteArg = templateVars.get(PARAM_SITE);
        String containerArg = templateVars.get(PARAM_CONTAINER);
        String pathArg = templateVars.get(PARAM_PATH);
        if (siteArg != null) {
            ParameterCheck.mandatoryString("siteArg", siteArg);
            ParameterCheck.mandatoryString("containerArg", containerArg);
            /*
                 * Site based request <url>URL_BASE/{site}/{container}</url> or
                 * <url>URL_BASE/{site}/{container}/{path}</url>
                 */
            SiteInfo site = siteService.getSite(siteArg);
            PropertyCheck.mandatory(this, "site", site);
            NodeRef node = siteService.getContainer(site.getShortName(), containerArg);
            if (node == null) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid 'container' variable");
            }
            if (pathArg != null) {
                // <url>URL_BASE/{site}/{container}/{path}</url>
                StringTokenizer st = new StringTokenizer(pathArg, "/");
                while (st.hasMoreTokens()) {
                    String childName = st.nextToken();
                    node = nodeService.getChildByName(node, ContentModel.ASSOC_CONTAINS, childName);
                    if (node == null) {
                        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid 'path' variable");
                    }
                }
            }
            return node;
        }
    }
    return null;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) StringTokenizer(java.util.StringTokenizer) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 49 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class DocLinksDelete method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    NodeRef destinationNodeRef = null;
    /* Parse the template vars */
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    destinationNodeRef = parseNodeRefFromTemplateArgs(templateVars);
    /* Delete links */
    DeleteLinksStatusReport report;
    try {
        report = documentLinkService.deleteLinksToDocument(destinationNodeRef);
    } catch (IllegalArgumentException ex) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid Arguments: " + ex.getMessage());
    } catch (AccessDeniedException e) {
        throw new WebScriptException(Status.STATUS_FORBIDDEN, "You don't have permission to perform this operation");
    }
    /* Build response */
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("total_count", report.getTotalLinksFoundCount());
    model.put("deleted_count", report.getDeletedLinksCount());
    Map<String, String> errorDetails = new HashMap<String, String>();
    Iterator<Entry<NodeRef, Throwable>> it = report.getErrorDetails().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<NodeRef, Throwable> pair = it.next();
        Throwable th = pair.getValue();
        errorDetails.put(pair.getKey().toString(), th.getMessage());
    }
    model.put("error_details", errorDetails);
    return model;
}
Also used : AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) HashMap(java.util.HashMap) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Entry(java.util.Map.Entry) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) DeleteLinksStatusReport(org.alfresco.service.cmr.repository.DeleteLinksStatusReport) Map(java.util.Map) HashMap(java.util.HashMap)

Example 50 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class DownloadDelete method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    if (templateVars == null) {
        String error = "No parameters supplied";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    if (!(templateVars.containsKey("store_type") && templateVars.containsKey("store_id") && templateVars.containsKey("node_id"))) {
        String error = "Missing template variables (store_type, store_id or node_id).";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    StoreRef store = new StoreRef(templateVars.get("store_type"), templateVars.get("store_id"));
    NodeRef nodeRef = new NodeRef(store, templateVars.get("node_id"));
    if (!nodeService.exists(nodeRef)) {
        String error = "Could not find node: " + nodeRef;
        throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
    }
    downloadService.cancelDownload(nodeRef);
    status.setCode(HttpServletResponse.SC_OK);
    return new HashMap<String, Object>();
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap)

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