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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations