Search in sources :

Example 41 with WebScriptException

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

the class StreamACP method execute.

/**
 * @see org.springframework.extensions.webscripts.WebScript#execute(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
 */
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    File tempACPFile = null;
    try {
        NodeRef[] nodeRefs = null;
        String contentType = req.getContentType();
        if (MULTIPART_FORMDATA.equals(contentType)) {
            // get nodeRefs parameter from form
            nodeRefs = getNodeRefs(req.getParameter(PARAM_NODE_REFS));
        } else {
            // presume the request is a JSON request so get nodeRefs from JSON body
            nodeRefs = getNodeRefs(new JSONObject(new JSONTokener(req.getContent().getContent())));
        }
        // setup the ACP parameters
        ExporterCrawlerParameters params = new ExporterCrawlerParameters();
        params.setCrawlSelf(true);
        params.setCrawlChildNodes(true);
        params.setExportFrom(new Location(nodeRefs));
        // create an ACP of the nodes
        tempACPFile = createACP(params, ACPExportPackageHandler.ACP_EXTENSION, false);
        // stream the ACP back to the client as an attachment (forcing save as)
        streamContent(req, res, tempACPFile, true, tempACPFile.getName(), null);
    } catch (IOException ioe) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", ioe);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    } finally {
        // try and delete the temporary file
        if (tempACPFile != null) {
            if (logger.isDebugEnabled())
                logger.debug("Deleting temporary archive: " + tempACPFile.getAbsolutePath());
            tempACPFile.delete();
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ExporterCrawlerParameters(org.alfresco.service.cmr.view.ExporterCrawlerParameters) JSONException(org.json.JSONException) IOException(java.io.IOException) File(java.io.File) Location(org.alfresco.service.cmr.view.Location)

Example 42 with WebScriptException

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

the class DictionaryWebServiceBase method getFullNamespaceURI.

/**
 * @param classname     the class name as cm_person
 * @return String       the full name in the following format {namespaceuri}shorname
 */
public String getFullNamespaceURI(String classname) {
    try {
        String result = null;
        String prefix = this.getPrefix(classname);
        String url = this.namespaceService.getNamespaceURI(prefix);
        String name = this.getShortName(classname);
        result = "{" + url + "}" + name;
        return result;
    } catch (Exception e) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "The exact classname - " + classname + "  parameter has not been provided in the URL");
    }
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException)

Example 43 with WebScriptException

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

the class AbstractDiscussionWebScript method renderTopic.

/*
     * Was topicpost.lib.js getTopicPostData / getTopicPostDataFromTopicAndPosts
     * 
     * TODO Switch the FTL to prefer the Info object rather than the ScriptNode
     */
protected Map<String, Object> renderTopic(TopicInfo topic, SiteInfo site) {
    // Fetch the primary post
    PostInfo primaryPost = discussionService.getPrimaryPost(topic);
    if (primaryPost == null) {
        throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
    }
    // Fetch the most recent reply
    PostInfo mostRecentPost = discussionService.getMostRecentPost(topic);
    // Find out how many replies there are
    int numReplies;
    if (mostRecentPost.getNodeRef().equals(primaryPost.getNodeRef())) {
        // Only the one post in the topic
        mostRecentPost = null;
        numReplies = 0;
    } else {
        // Use this trick to get the number of posts in the topic,
        // but without needing to get lots of data and objects
        PagingRequest paging = new PagingRequest(1);
        paging.setRequestTotalCountMax(MAX_QUERY_ENTRY_COUNT);
        PagingResults<PostInfo> posts = discussionService.listPosts(topic, paging);
        // The primary post is in the list, so exclude from the reply count
        numReplies = posts.getTotalResultCount().getFirst() - 1;
    }
    // Build the details
    Map<String, Object> item = new HashMap<String, Object>();
    item.put(KEY_IS_TOPIC_POST, true);
    item.put(KEY_TOPIC, topic.getNodeRef());
    item.put(KEY_POST, primaryPost.getNodeRef());
    item.put(KEY_CAN_EDIT, canUserEditPost(primaryPost, site));
    item.put(KEY_AUTHOR, buildPerson(topic.getCreator()));
    // The reply count is one less than all posts (first is the primary one)
    item.put("totalReplyCount", numReplies);
    // Add the topic site
    item.put("site", topic.getShortSiteName());
    // We want details on the most recent post
    if (mostRecentPost != null) {
        item.put("lastReply", mostRecentPost.getNodeRef());
        item.put("lastReplyBy", buildPerson(mostRecentPost.getCreator()));
    }
    // Include the tags
    item.put("tags", topic.getTags());
    // All done
    return item;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) JSONObject(org.json.simple.JSONObject) PagingRequest(org.alfresco.query.PagingRequest)

Example 44 with WebScriptException

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

the class ForumPostPut method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    // Did they want to change a reply or the whole topic?
    if (post != null) {
        // Update the specified post
        doUpdatePost(post, post.getTopic(), req, json);
        // Add the activity entry for the reply change
        addActivityEntry("reply", "updated", post.getTopic(), post, site, req, json);
        // Build the JSON for just this post
        model.put(KEY_POSTDATA, renderPost(post, site));
    } else if (topic != null) {
        // Update the primary post of the topic
        post = discussionService.getPrimaryPost(topic);
        if (post == null) {
            throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
        }
        doUpdatePost(post, topic, req, json);
        // Add the activity entry for the topic change
        addActivityEntry("post", "updated", topic, null, site, req, json);
        // Build the JSON for the whole topic
        model.put(KEY_POSTDATA, renderTopic(topic, site));
    } else {
        String error = "Node was of the wrong type, only Topic and Post are supported";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject)

Example 45 with WebScriptException

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

the class ForumTopicsFilteredGet method executeImpl.

/**
 * @param site SiteInfo
 * @param nodeRef Not required. It is only included because it is overriding the parent class.
 * @param topic Not required. It is only included because it is overriding the parent class.
 * @param post Not required. It is only included because it is overriding the parent class.
 * @param req WebScriptRequest
 * @param status Not required. It is only included because it is overriding the parent class.
 * @param cache Not required. It is only included because it is overriding the parent class.
 *
 * @return Map
 */
@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);
    }
    // Set search filter to users topics or all topics
    String pAuthor = req.getParameter("topics");
    String author = DEFAULT_TOPIC_AUTHOR;
    if (pAuthor != null) {
        author = pAuthor;
    }
    // Set the number of days in the past to search from
    String pDaysAgo = req.getParameter("history");
    int daysAgo = DEFAULT_TOPIC_LATEST_POST_DAYS_AGO;
    if (pDaysAgo != null) {
        try {
            daysAgo = Integer.parseInt(pDaysAgo);
        } catch (NumberFormatException e) {
        // do nothing. history has already been preset to the default value.
        }
    }
    // Get the complete search query
    Pair<String, String> searchQuery = getSearchQuery(site, author, daysAgo);
    // Get the filtered topics
    PagingRequest paging = buildPagingRequest(req);
    PagingResults<TopicInfo> topics = doSearch(searchQuery, false, paging);
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    // 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) PagingRequest(org.alfresco.query.PagingRequest) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo)

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