Search in sources :

Example 51 with WebScriptException

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;
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) LinkedList(java.util.LinkedList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 52 with WebScriptException

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;
}
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) DownloadStatus(org.alfresco.service.cmr.download.DownloadStatus)

Example 53 with WebScriptException

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;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Example 54 with WebScriptException

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;
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) SpecialFacetablePropertyFTL(org.alfresco.repo.web.scripts.facet.FacetablePropertyFTL.SpecialFacetablePropertyFTL) StandardFacetablePropertyFTL(org.alfresco.repo.web.scripts.facet.FacetablePropertyFTL.StandardFacetablePropertyFTL) SyntheticFacetablePropertyFTL(org.alfresco.repo.web.scripts.facet.FacetablePropertyFTL.SyntheticFacetablePropertyFTL) SpecialFacetablePropertyFTL(org.alfresco.repo.web.scripts.facet.FacetablePropertyFTL.SpecialFacetablePropertyFTL) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) SyntheticPropertyDefinition(org.alfresco.repo.search.impl.solr.facet.SolrFacetService.SyntheticPropertyDefinition) NamespaceException(org.alfresco.service.namespace.NamespaceException) Collection(java.util.Collection) ScriptPagingDetails(org.alfresco.util.ScriptPagingDetails)

Example 55 with WebScriptException

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<>();
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties)

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