use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class DownloadPost 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);
}
// Parse the JSON, if supplied
JSONArray json = null;
String contentType = req.getContentType();
if (contentType != null && contentType.indexOf(';') != -1) {
contentType = contentType.substring(0, contentType.indexOf(';'));
}
List<NodeRef> nodes = new LinkedList<NodeRef>();
if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
JSONParser parser = new JSONParser();
try {
json = (JSONArray) parser.parse(req.getContent().getContent());
for (int i = 0; i < json.size(); i++) {
JSONObject obj = (JSONObject) json.get(i);
String nodeRefString = (String) obj.get("nodeRef");
if (nodeRefString != null) {
nodes.add(new NodeRef(nodeRefString));
}
}
} catch (IOException io) {
throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Unexpected IOException", io);
} catch (org.json.simple.parser.ParseException je) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Unexpected ParseException", je);
}
}
if (nodes.size() <= 0) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No nodeRefs provided");
}
NodeRef downloadNode = downloadService.createDownload(nodes.toArray(new NodeRef[nodes.size()]), true);
Map<String, Object> model = new HashMap<String, Object>();
model.put("downloadNodeRef", downloadNode);
return model;
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class DownloadStatusGet 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);
}
DownloadStatus downloadStatus = downloadService.getDownloadStatus(nodeRef);
Map<String, Object> result = new HashMap<String, Object>();
result.put("downloadStatus", downloadStatus);
return result;
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class AbstractSolrFacetConfigAdminWebScript method getNonNegativeIntParameter.
/**
* Retrieves the named parameter as an integer, if the parameter is not present the default value is returned.
*
* @param req The WebScript request
* @param paramName The name of parameter to look for.
* @param defaultValue The default value that should be returned if parameter is not present in request or is negative.
* @return The request parameter or default value
* @throws WebScriptException if the named parameter cannot be converted to int (HTTP rsp 400).
*/
protected int getNonNegativeIntParameter(WebScriptRequest req, String paramName, int defaultValue) {
final String paramString = req.getParameter(paramName);
final int result;
if (paramString != null) {
try {
final int paramInt = Integer.valueOf(paramString);
if (paramInt < 0) {
result = defaultValue;
} else {
result = paramInt;
}
} catch (NumberFormatException e) {
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
}
} else {
result = defaultValue;
}
return result;
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class FacetablePropertiesGet method unprotectedExecuteImpl.
@Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) {
// We use any provided locale to localise some elements of the webscript response, but not all.
final String userLocaleString = req.getParameter(QUERY_PARAM_LOCALE);
final Locale userLocale = (userLocaleString == null) ? Locale.getDefault() : new Locale(userLocaleString);
// There are multiple defined URIs for this REST endpoint. Some define a "classname" template var.
final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
final String contentClassName = templateVars.get(TEMPLATE_VAR_CLASSNAME);
final QName contentClassQName;
try {
contentClassQName = contentClassName == null ? null : QName.createQName(contentClassName, namespaceService);
} catch (NamespaceException e) {
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Unrecognised classname: " + contentClassName, e);
}
// Build an FTL model of facetable properties - this includes normal Alfresco properties and also
// 'synthetic' properties like size and mimetype. See below for more details.
final Map<String, Object> model = new HashMap<>();
final SortedSet<FacetablePropertyFTL<?>> facetableProperties;
if (contentClassQName == null) {
facetableProperties = toFacetablePropertyModel(facetService.getFacetableProperties(), userLocale);
final List<SyntheticPropertyDefinition> facetableSyntheticProperties = facetService.getFacetableSyntheticProperties();
facetableProperties.addAll(toFacetablePropertyModel_(facetableSyntheticProperties, userLocale));
} else {
facetableProperties = toFacetablePropertyModel(facetService.getFacetableProperties(contentClassQName), userLocale);
final List<SyntheticPropertyDefinition> facetableSyntheticProperties = facetService.getFacetableSyntheticProperties(contentClassQName);
facetableProperties.addAll(toFacetablePropertyModel_(facetableSyntheticProperties, userLocale));
}
// Always add some hard-coded facetable "properties"
// Note: TAG and SITE are Solr specials and don’t have namespaces
facetableProperties.add(new SpecialFacetablePropertyFTL("TAG", "Tag"));
facetableProperties.add(new SpecialFacetablePropertyFTL("SITE", "Site"));
// The webscript allows for some further filtering of results:
List<ResultFilter> filters = new ArrayList<>();
// Filter by property QName namespace:
final String namespaceFilter = req.getParameter(QUERY_PARAM_NAMESPACE);
if (namespaceFilter != null) {
filters.add(new ResultFilter() {
@Override
public boolean filter(FacetablePropertyFTL<?> facetableProperty) {
final QName propQName = facetableProperty.getQname();
Collection<String> prefixes = namespaceService.getPrefixes(propQName.getNamespaceURI());
return prefixes.contains(namespaceFilter);
}
});
}
List<FacetablePropertyFTL<?>> filteredFacetableProperties = filter(facetableProperties, filters);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved " + facetableProperties.size() + " available facets; filtered to " + filteredFacetableProperties.size());
}
// Create paging
ScriptPagingDetails paging = new ScriptPagingDetails(getNonNegativeIntParameter(req, QUERY_PARAM_MAX_ITEMS, DEFAULT_MAX_ITEMS_PER_PAGE), getNonNegativeIntParameter(req, QUERY_PARAM_SKIP_COUNT, 0));
model.put(PROPERTIES_KEY, ModelUtil.page(filteredFacetableProperties, paging));
model.put("paging", ModelUtil.buildPaging(paging));
return model;
}
use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.
the class SolrFacetConfigAdminPost method unprotectedExecuteImpl.
@Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) {
try {
SolrFacetProperties fp = parseRequestForFacetProperties(req);
facetService.createFacetNode(fp);
if (logger.isDebugEnabled()) {
logger.debug("Created facet node: " + fp);
}
} catch (Throwable t) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not save the facet configuration.", t);
}
// Needs to be mutable.
return new HashMap<>();
}
Aggregations