Search in sources :

Example 21 with WebScriptException

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

the class RatingPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    NodeRef nodeRefToBeRated = parseRequestForNodeRef(req);
    JSONObject json = null;
    try {
        // read request json
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Check mandatory parameters.
        if (json.has(RATING) == false) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "rating parameter missing when applying rating");
        }
        if (json.has(RATING_SCHEME) == false) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "schemeName parameter missing when applying rating");
        }
        // Check that the scheme name actually exists
        final String schemeName = json.getString(RATING_SCHEME);
        RatingScheme scheme = ratingService.getRatingScheme(schemeName);
        if (scheme == null) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unknown scheme name: " + schemeName);
        }
        // Range checking of the rating score will be done within the RatingService.
        // So we can just apply the rating.
        final float rating = (float) json.getDouble(RATING);
        ratingService.applyRating(nodeRefToBeRated, rating, schemeName);
        // We'll return the URL to the ratings of the just-rated node.
        String ratedNodeUrlFragment = nodeRefToBeRated.toString().replace("://", "/");
        String ratedNodeUrl = MessageFormat.format(NODE_RATINGS_URL_FORMAT, ratedNodeUrlFragment);
        model.put(RATED_NODE, ratedNodeUrl);
        model.put(RATING, rating);
        model.put(RATING_SCHEME, schemeName);
        model.put(AVERAGE_RATING, ratingService.getAverageRating(nodeRefToBeRated, schemeName));
        model.put(RATINGS_TOTAL, ratingService.getTotalRating(nodeRefToBeRated, schemeName));
        model.put(RATINGS_COUNT, ratingService.getRatingsCount(nodeRefToBeRated, schemeName));
    } 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);
    }
    return model;
}
Also used : JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RatingScheme(org.alfresco.service.cmr.rating.RatingScheme) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 22 with WebScriptException

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

the class ReplicationDefinitionGet method buildModel.

@Override
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    // Which definition did they ask for?
    String replicationDefinitionName = req.getServiceMatch().getTemplateVars().get("replication_definition_name");
    ReplicationDefinition replicationDefinition = replicationService.loadReplicationDefinition(replicationDefinitionName);
    // Does it exist?
    if (replicationDefinition == null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "No Replication Definition found with that name");
    }
    // Have it turned into simple models
    return modelBuilder.buildDetails(replicationDefinition);
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition)

Example 23 with WebScriptException

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

the class ReplicationDefinitionPut method buildModel.

@Override
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    // Which definition did they ask for?
    String replicationDefinitionName = req.getServiceMatch().getTemplateVars().get("replication_definition_name");
    ReplicationDefinition replicationDefinition = replicationService.loadReplicationDefinition(replicationDefinitionName);
    // Does it exist?
    if (replicationDefinition == null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "No Replication Definition found with that name");
    }
    // Grab the JSON, and prepare to update
    try {
        JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Are they trying to rename?
        if (json.has("name")) {
            String jsonName = json.getString("name");
            if (!jsonName.equals(replicationDefinitionName)) {
                // Name has changed, ensure the new name is spare
                if (replicationService.loadReplicationDefinition(jsonName) != null) {
                    throw new WebScriptException(Status.STATUS_BAD_REQUEST, "The specified new name is already in use");
                }
                // Rename it
                replicationService.renameReplicationDefinition(replicationDefinitionName, jsonName);
                // And grab the updated version post-rename
                replicationDefinition = replicationService.loadReplicationDefinition(jsonName);
            }
        }
        // Update everything else
        updateDefinitionProperties(replicationDefinition, json);
        // Save the changes
        replicationService.saveReplicationDefinition(replicationDefinition);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from request.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from request.", je);
    }
    // Return the new details on it
    return modelBuilder.buildDetails(replicationDefinition);
}
Also used : JSONTokener(org.json.JSONTokener) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 24 with WebScriptException

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

the class ReplicationDefinitionsPost method buildModel.

@Override
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    // Create our definition
    ReplicationDefinition replicationDefinition = null;
    JSONObject json;
    try {
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Check for the required parameters
        if (!json.has("name"))
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "name is required but wasn't supplied");
        if (!json.has("description"))
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "description is required but wasn't supplied");
        // Ensure one doesn't already exist with that name
        String name = json.getString("name");
        if (replicationService.loadReplicationDefinition(name) != null) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "A replication definition already exists with that name");
        }
        // Create the definition
        replicationDefinition = replicationService.createReplicationDefinition(name, json.getString("description"));
        // Set the extra parts
        updateDefinitionProperties(replicationDefinition, json);
        // Save the changes
        replicationService.saveReplicationDefinition(replicationDefinition);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from request.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from request.", je);
    }
    // Return the details on it
    return modelBuilder.buildDetails(replicationDefinition);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 25 with WebScriptException

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

the class AbstractRuleWebScript method parseRequestForNodeRef.

/**
 * Parses the request and providing it's valid returns the NodeRef.
 *
 * @param req The webscript request
 * @return The NodeRef passed in the request
 */
protected NodeRef parseRequestForNodeRef(WebScriptRequest req) {
    // get the parameters that represent the NodeRef, we know they are present
    // otherwise this webscript would not have matched
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String storeType = templateVars.get("store_type");
    String storeId = templateVars.get("store_id");
    String nodeId = templateVars.get("id");
    // create the NodeRef and ensure it is valid
    StoreRef storeRef = new StoreRef(storeType, storeId);
    NodeRef nodeRef = new NodeRef(storeRef, nodeId);
    if (!this.nodeService.exists(nodeRef)) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: " + nodeRef.toString());
    }
    return nodeRef;
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

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